XSLT删除不在列表中的节点,同时保留列表中的任何子节点

时间:2013-05-01 22:42:19

标签: xslt-1.0

我有一个id列表,我想用它来“过滤”提供的xml文件。应保留列表中的每个节点,并且不删除列表中的每个节点。

我遇到的困难是当我删除一个节点(不在IDList中)时,我最终还是删除了一个可能在IDList中的子节点。

最后,我需要保持节点的嵌套(参见下面的示例)。

这是我的ID列表:

IDList = 2,3,5,6

这是我的输入xml是:

<Associations>
    <Association ASSOCIATION_ID="1">
        Elmwood Association
        <Homes/>
    </Association>
    <Association ASSOCIATION_ID="2">
        Oakwood Association
        <Homes/>
        <Association ASSOCIATION_ID="4">
            Oakwood Sub Association A
            <Homes/>
            <Association ASSOCIATION_ID="6">
                Oakwood Sub Sub Association
                <Homes/>
            </Association>
        </Association>
        <Association ASSOCIATION_ID="5">
            Oakwood Sub Association B
            <Homes/>
        </Association>
    </Association>
    <Association ASSOCIATION_ID="3">
        Cedarwood Association
        <Homes/>
    </Association>
</Associations>

这是我想要的输出:

<Associations>
    <Association ASSOCIATION_ID="2">
        Oakwood Association
        <Homes/>
        <Association ASSOCIATION_ID="6">
            Oakwood Sub Sub Association
            <Homes/>
        </Association>
        <Association ASSOCIATION_ID="5">
            Oakwood Sub Association B
            <Homes/>
        </Association>
    </Association>
    <Association ASSOCIATION_ID="3">
        Cedarwood Association
        <Homes/>
    </Association>
</Associations>

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

<强>予。 XSLT 2.0解决方案:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pIds" select="2, 3, 5, 6" as="xs:integer+"/>

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

 <xsl:template priority="2" match=
  "Association[not(@ASSOCIATION_ID = $pIds)
             and not(.//Association[@ASSOCIATION_ID = $pIds])]"/>

 <xsl:template match=
  "Association[@ASSOCIATION_ID = $pIds]
        //Association[not(@ASSOCIATION_ID = $pIds)]
  |
   *[not(self::Association[@ASSOCIATION_ID = $pIds])]
      /node()[not(descendant-or-self::Association[@ASSOCIATION_ID = $pIds])]
  ">
  <xsl:apply-templates/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时:

<Associations>
    <Association ASSOCIATION_ID="1">
        Elmwood Association
        <Homes/>
    </Association>
    <Association ASSOCIATION_ID="2">
        Oakwood Association
        <Homes/>
        <Association ASSOCIATION_ID="4">
            Oakwood Sub Association A
            <Homes/>
            <Association ASSOCIATION_ID="6">
                Oakwood Sub Sub Association
                <Homes/>
            </Association>
        </Association>
        <Association ASSOCIATION_ID="5">
            Oakwood Sub Association B
            <Homes/>
        </Association>
    </Association>
    <Association ASSOCIATION_ID="3">
        Cedarwood Association
        <Homes/>
    </Association>
</Associations>

生成想要的正确结果

<Associations>
   <Association ASSOCIATION_ID="2">
        Oakwood Association
        <Homes/>
      <Association ASSOCIATION_ID="6">
                Oakwood Sub Sub Association
                <Homes/>
      </Association>
      <Association ASSOCIATION_ID="5">
            Oakwood Sub Association B
            <Homes/>
      </Association>
   </Association>
   <Association ASSOCIATION_ID="3">
        Cedarwood Association
        <Homes/>
   </Association>
</Associations>

<强> II。 XSLT 1.0解决方案:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <my:ids>
   <id>2</id>
   <id>3</id>
   <id>5</id>
   <id>6</id>
 </my:ids>

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

 <xsl:template priority="2" match=
  "Association[not(@ASSOCIATION_ID = document('')/*/my:ids/*)
             and not(.//Association[@ASSOCIATION_ID = document('')/*/my:ids/*])]"/>

 <xsl:template match=
  "Association[@ASSOCIATION_ID = document('')/*/my:ids/*]
        //Association[not(@ASSOCIATION_ID = document('')/*/my:ids/*)]
  |
   *[not(self::Association[@ASSOCIATION_ID = document('')/*/my:ids/*])]
      /node()[not(descendant-or-self::Association
                         [@ASSOCIATION_ID = document('')/*/my:ids/*])]
  ">
  <xsl:apply-templates/>
 </xsl:template>
</xsl:stylesheet>