从XML标签附加变量值,并使用xslt

时间:2018-08-17 11:14:49

标签: xml xslt

我将以下XML作为输出,并想将xml中存在的值动态地连接到xml文件名。 XML包含单个ID值,并希望在运行时使用XSLT以XML文件名传递它。

输出1个XMl:

文件名:Output.xml

<Accounts>

<customer>

<ID>1234</ID>

</customer>

</Accounts>

输出2个XMl:

文件名:Output.xml

<Accounts>

<customer>

<ID>4096</ID>

</customer>

</Accounts>

预期的输出文件名:Output_1234.xml

<Accounts>

<customer>

<ID>1234</ID>

</customer>

</Accounts>

期望的文件名:Output_4096.xml

<Accounts>

<customer>

<ID>4096</ID>

</customer>

</Accounts>

1 个答案:

答案 0 :(得分:0)

使用以下代码实现所需的输出:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

   <xsl:template match="/">
        <xsl:result-document href="{concat(substring-before(tokenize(base-uri(.),'/')[last()], '.xml'),'_', /Accounts/customer/ID,'.xml')}">
            <xsl:copy-of select="Accounts"/>
        </xsl:result-document>
    </xsl:template>

</xsl:stylesheet>