【问题标题】:Filter the elements of an xml with xslt使用 xslt 过滤 xml 的元素
【发布时间】:2015-05-22 11:38:40
【问题描述】:

我有以下 xml 文件:

<?xml version="1.0" encoding="UTF-8"? xmlns:xn="whatever.xsd>
    <rootNode>
        <fileHeader />
        <xn:child1>
            <xn:child2 id="1">
                ....
            </xn:child2>
            <xn:child2 id="2">
                ....
            </xn:child2>
            <xn:child2 id="3">
                ....
            </xn:child2>
                ....
        </xn:child1>
</rootNode>

我想根据 child2 标记的 id 属性过滤输出 xml。为此,我有以下 xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xn="genericNrm.xsd">
    <!-- identity template -->
    <xsl:template match="@* | node()">
        <xsl:copy>
              <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <!-- override the above template for certain MeContext elements; output nothing. -->
    <xsl:template match="xn:child1[not(xn:child2/@id != '1') and not(xn:child2/@id != '3')]">
    </xsl:template>
</xsl:stylesheet>

所以,输出应该是这样的:

<?xml version="1.0" encoding="UTF-8"? xmlns:xn="whatever.xsd>
    <rootNode>
        <fileHeader />
        <xn:child1>
            <xn:child2 id="1">
                ....
            </xn:child2>
            <xn:child2 id="3">
                ....
            </xn:child2>
                ....
        </xn:child1>
</rootNode>

但我无法得到它,我的 xslt 有什么问题?

编辑:更新 xslt,因为它有错字

【问题讨论】:

  • 需要根据 child2 标签的 id 属性过滤什么?标签本身?还是父母?
  • 我需要过滤 child2 标签,如输出中所述。

标签: xml xslt


【解决方案1】:

这个:

<xsl:template match="xn:child[not(xn:child2/@id != '1') and not(xn:child2/@id != '3')]">

匹配名为xn:child 的元素。您的 XML 中没有这样的元素。也许你的意思是:

<xsl:template match="xn:child2[@id != '1' and @id != '3']"/>

--
注意:命名空间声明不能出现在 XML 声明中。此外,如果您希望前缀与给定命名空间中的节点匹配,则必须将其绑定到与 XML 输入使用的命名空间 URI 相同的命名空间 URI(前缀本身可以是任何东西)。

【讨论】:

  • 非常感谢@michael.hor257k!它有效,你统治!
猜你喜欢
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-29
相关资源
最近更新 更多