我是XSL/XSLT的新手.我想将一个xml文档复制到另一个文件中,但是要替换一些命名空间标记和一些具有某些特殊属性的标记.例如:
<root>
<ext:foo>Test</ext:foo>
<bar>Bar</bar>
<baz id="baz" x="test">
<something/>
</baz>
</root>
Run Code Online (Sandbox Code Playgroud)
应该改写成:
<root>
--Test--
<bar>Bar</bar>
xxx<baz id="baz">
<something/>
</baz>xxx
</root>
Run Code Online (Sandbox Code Playgroud)
是否可以复制整个XML,然后应用一些规则来替换我想要替换的标签?
您可以复制某些节点并使用不同的规则重新编写其他节点.为了保持<root>
和<bar>
相同,并重写<baz>
,尝试这个(未经测试)作为一个起点:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<!-- Match <baz> and re-write a little -->
<xsl:template match="baz">
xxx<baz id="{@id}">
<xsl:apply-templates />
</baz>xxx
</xsl:template>
<!-- By default, copy all elements, attributes, and text -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)