【发布时间】:2015-04-22 16:42:16
【问题描述】:
我正在使用 xslt 来编辑多个文件。我的输入文件是我的设置文件,它看起来像这样:
<root>
<file>
<folderin>/var/in/</folderin>
<in>10</in>
<folderout>/var/out</folderout>
<out>20</out>
<name>First file</name>
</file>
</root>
示例文件:
<root>
<element1 id1="10">
...
</element1>
<element2 id2="10" attribute="xyz">
...
</element2>
</root>
还有我的 xslt 文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template name="CopyXml" match="/*">
<xsl:for-each select="file">
<xsl:variable name="inputfile" select="concat(folderin, in, '.xml')"/>
<xsl:variable name="outputfile" select="concat(folderout, out, '.xml')"/>
<xsl:result-document method="xml" href="{$outputfile}">
<xsl:copy-of select="document($inputfile)/*"/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
在 /var/in/ 中有几百个文件,我只想编辑其中的几个(在本例中 /var/in/10.xml 带有退出文件 /var/out/20.xml)。在新文件中,我想将 id1 和 id2 属性值更改为我的“out”参数
我已经设法用我的 xslt 复制了那些具有适当名称的特定文件,但是我在应用任何模板时遇到问题,因此我可以更改这些文件。我尝试使用应用模板,但是我无法将这些模板应用到 $outputFile,它要么不做任何事情,要么将 $outputFile 与设置文件中的副本相附加;/ 有谁知道该怎么做?
编辑: 此示例文件的输出:
<root>
<element1 id1="20">
...
</element1>
<element2 id2="20" attribute="xyz">
...
</element2>
</root>
【问题讨论】:
-
请编辑您的问题并显示您要为您显示的“示例文件”创建的结果。我也没有在您的代码中看到“out”参数,所以我不明白应该从哪里获取值。
-
我已经添加了我想要的结果。现在“out”参数在:select="concat(folderout, out, '.xml')"。我尝试添加应用模板,其中没有参数,但它始终只应用于我的设置文件,而不是 $inputfile
-
那么任何具有
in元素中值的属性都需要获取out元素中给出的新值?属性名有什么限制吗?