1

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

对我来说,困难在于当我删除一个节点(不在 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>

任何帮助将非常感激!

4

1 回答 1

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>

二、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>
于 2013-05-02T02:14:10.463 回答