是否可以跳过节点中的元素?例如,我们将节点设为Test
,它具有子元素x
,y
,z
。我想复制整个Test
节点,但不希望在最终结果中使用z
元素。我们可以在not()
中使用copy-of select
吗?我试过但它没有用。
感谢。
答案 0 :(得分:3)
不,<xsl:copy-of>
让你无法控制你正在复制的内容。这就是具有选择性遗漏的身份模板的用途:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<!-- Identity template -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:apply-templates select="Test" />
</xsl:template>
<!-- Omit z from the results-->
<xsl:template match="z" />
</xsl:stylesheet>
应用于此XML时:
<n>
<Test>
<x>Hello</x>
<y>Heeelo</y>
<z>Hullo</z>
</Test>
</n>
结果是:
<Test>
<x>Hello</x>
<y>Heeelo</y>
</Test>