【发布时间】:2021-12-28 13:26:36
【问题描述】:
我见过很多xsl:copy-of 示例,例如这个,其中包含的父节点是硬编码的。即<output> 但有没有办法使用xsl:copy-of 并保留父节点? <library> 在这种情况下。
XML:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<category name="dogs">
<book>
<name>All about dogs</name>
<author>Someone</author>
</book>
</category>
<category name="cats">
<book>
<name>All about cats</name>
<author>Someone</author>
</book>
</category>
</library>
XSL:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<output>
<xsl:copy-of select="library/category[@name='cats']" />
</output>
</xsl:template>
</xsl:stylesheet>
当前输出:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<category name="cats">
<book>
<name>All about cats</name>
<author>Someone</author>
</book>
</category>
</output>
【问题讨论】:
-
在 XSLT 3 中,您可以使用
<xsl:copy-of select="snapshot(library/category[@name='cats'])/.."/>。 -
你能发布一个期望输出的例子吗?