【问题标题】:Identity transform strange behavior when use with another template与另一个模板一起使用时的身份转换奇怪行为
【发布时间】:2016-07-08 20:49:11
【问题描述】:

我看到了奇怪的行为,奇怪的意思是它的行为与我们通常的情况相反。 以下是详细信息:

XSLT 代码

<?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>

源 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>

我的结果是:

<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 处理器的链接:Sample Code

【问题讨论】:

    标签: xslt transform xslt-2.0 identity


    【解决方案1】:

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

    您的第一个模板的优先级为 0.5,而您的第二个 (identity transform) 模板的优先级为 -0.5。

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

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

    【讨论】:

    • 谢谢@michael,这就是我要找的:)
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多