与另一个模板一起使用时身份转换奇怪的行为

Identity transform strange behavior when use with another template

我见过奇怪的行为,奇怪的意思是它的行为与我们正常情况下的情况相反。 以下是详细信息:

XSLT Code

<?xml version="1.0" encoding="UTF-8" ?>

<xsl:template match="child[@include='1']"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

Source XML

<?xml version="1.0" encoding="UTF-8"?>
<Parent>
    <child include='1'>
        <Attribute>Attribute1</Attribute>
    </child>
    <child include='1'>
        <Attribute>Attribute2</Attribute>
    </child>
    <child include='0'>
        <Attribute>Attribute3</Attribute>
    </child>
    <child include='0'>
        <Attribute>Attribute4</Attribute>
    </child>
</Parent>

And my result is:

<Parent>
  <child include="0">
        <Attribute>Attribute3</Attribute>
  </child>
  <child include="0">
        <Attribute>Attribute4</Attribute>
  </child>
</Parent>

根据正常条件我们应用结果应该如下根据条件

<xsl:template match="child[@include='1']"/>
<Parent>
  <child include="1">
        <Attribute>Attribute3</Attribute>
  </child>
  <child include="1">
        <Attribute>Attribute4</Attribute>
  </child>
</Parent>

希望我已经详细解释了: 这是代码和 xslt 处理器的 link:Sample Code

我不确定你的期望是基于什么。

您的第一个模板的优先级为 0.5,而您的第二个(身份转换)模板的优先级为 -0.5。

因此,应用于所有 include 属性为 1child 元素的模板是第一个模板。 此模板为空,因此不输出任何内容。因此,输出中不会出现 include 属性为 1child 元素。

所有其他节点都与第二个模板匹配,该模板将它们(并通过递归,它们的后代)复制到输出。