2016-07-29 32 views
0

对于我的一个项目,我有一个包含模板的XML文件,它应该为GUI生成图。用户可以编写自己的XML模板并将其应用于当前的加载数据。使用XSLT进行数据驱动的XML生成

简化XML模板文件看起来是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<Templates> 
    <Template name="template"> 
    <PlotWindow name="PlotWindow"> 
     <Title>My title</Title> 
     <For> 
     <Var>%Variable%</Var> 
     <Plot name="%Variable%"> 
      <Item>f(%Variable%)</Item> 
     </Plot> 
     </For> 
    </PlotWindow> 
    </Template> 
</Templates> 

For - 标签应包含在%Variable%的所有数据被替换。数据本身是在第二个XML文件中定义的。

<?xml version="1.0" encoding="utf-8"?> 
<Data> 
    <Var name="%Variable%"> 
    <Item>Test</Item> 
    <Item>MyVar</Item> 
    <Item>ABC</Item> 
    </Var> 
</Data> 

%Variable%应通过TestMyVarABC迭代。期望的输出应该是下面显示的第三个XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<Result> 
    <PlotWindow name="PlotWindow"> 
    <Title>My title</Title> 
    <Plot name="Test"> 
     <Item>f(Test)</Item> 
    </Plot> 
    <Plot name="MyVar"> 
     <Item>f(MyVar)</Item> 
    </Plot> 
    <Plot name="ABC"> 
     <Item>f(ABC)</Item> 
    </Plot> 
    </PlotWindow> 
</Result> 

应该可以通过某种XSLT文件得到这样的结果。

此文件将是什么样子?

+0

正如你所知,建议已接近于焦点话题,没有人会通过为你构建它来向你展示“xslt的外观如何” - 但恕我直言,它应该可以用xslt。 –

+0

感谢您的建议。刚刚删除了我的问题的最后一部分。 – Aleph0

回答

1

我不知道你的例子如何具体是(能有在Data多个Var元素?在PlotWindowFor元素?),但考虑到目前的内容这可能是一个解决方案。

假设XML的样子:

<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <Data> 
     <Var name="%Variable%"> 
      <Item>Test</Item> 
      <Item>MyVar</Item> 
      <Item>ABC</Item> 
     </Var> 
    </Data> 
    <Templates> 
     <Template name="template"> 
      <PlotWindow name="PlotWindow"> 
       <Title>My title</Title> 
       <For> 
        <Var>%Variable%</Var> 
        <Plot name="%Variable%"> 
         <Item>f(%Variable%)</Item> 
        </Plot> 
       </For> 
      </PlotWindow> 
     </Template> 
    </Templates> 
</root> 

(我猜Data是在不同的文件如果是这样,使用document()功能在该行<xsl:for-each select="//Data">加载它。)

XSL:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes" method="xml" /> 

    <xsl:template match="Templates"> 
     <Result> 
      <xsl:apply-templates/> 
     </Result> 
    </xsl:template> 

    <xsl:template match="PlotWindow|Title"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="For"> 
     <xsl:variable name="name" select="./Var/text()"/> 

     <xsl:for-each select="//Data"> 
       <xsl:for-each select="Var[@name=$name]"> 
        <xsl:for-each select="Item"> 
         <xsl:variable name="plotname" select="./text()"/> 
         <Plot name="{$plotname}"> 
          <Item>f(<xsl:value-of select="$plotname"/>)</Item> 
         </Plot> 
        </xsl:for-each> 
       </xsl:for-each> 
     </xsl:for-each> 
    </xsl:template> 

    <xsl:template match="Data|Var|Item"></xsl:template> 

</xsl:stylesheet> 

结果:

<?xml version="1.0" encoding="UTF-8"?> 
<Result> 
    <PlotWindow name="PlotWindow"> 
     <Title>My title</Title> 
     <Plot name="Test"> 
      <Item>f(Test)</Item> 
     </Plot> 
     <Plot name="MyVar"> 
      <Item>f(MyVar)</Item> 
     </Plot> 
     <Plot name="ABC"> 
      <Item>f(ABC)</Item> 
     </Plot> 
    </PlotWindow> 
</Result>