0

如果我有这个 xml 文件:

<root> 
        <node id="a">
            <section id="a_1">
               <item id="0">
                    <attribute>
                        <color>Red</color>
                    </attribute>
               </item>
            </section>

            <section id="a_2">
               <item id="0">
                    <attribute>
                        <color>Red</color>
                    </attribute>
               </item>
            </section>            
        </node>

        <node id="b">
            <section id="b_1">

                <user id="b_1a">
                    <attribute>
                        <name>John</name>
                    </attribute>
                </user>

                <user id="b_1b">
                    <attribute>a</attribute>
                </user>

            </section>

            <section id="b_1" method="create">

                <user id="b_1a">
                    <attribute>
                        <name>John</name>
                    </attribute>
                </user>

                <user id="b_1c">
                    <attribute>a</attribute>
                </user>

            </section>

            <section id="b_2">                
                <user id="b_1a">
                    <attribute>
                        <name>John</name>
                    </attribute>
                </user>

            </section>
        </node>
 </root>

我希望输出是这样的:

    <root> 
        <node id="a">
            <section id="a_1">
               <item id="0">
                    <attribute>
                        <color>Red</color>
                    </attribute>
               </item>
            </section>

            <section id="a_2">
               <item id="0">
                    <attribute>
                        <color>Red</color>
                    </attribute>
               </item>
            </section>            
        </node>

        <node id="b">
            <section id="b_1">

                <user id="b_1a">
                    <attribute>
                        <name>John</name>
                    </attribute>
                </user>

                <user id="b_1b">
                    <attribute>a</attribute>
                </user>

            </section>

            <section id="b_1" method="create">

                <user id="b_1c">
                    <attribute>a</attribute>
                </user>

            </section>

            <section id="b_2">                
                <user id="b_1a">
                    <attribute>
                        <name>John</name>
                    </attribute>
                </user>

            </section>
        </node>
 </root>

正如我们所看到的,只要 id 相同,它就会被视为一个部分 id,即使它上面有其他方法。因此,我们删除了第二部分 id (b_1) 中包含“方法创建”的用户 id (b_1a)。这真的让我很沮丧,我无法省略该方法。所以任何帮助将不胜感激。如果我们查看部分 id b_2,它也有相同的用户 id b_1 和相同的“John”,但我们不会删除它,因为它在不同的部分 id 中。所以基本上我们根据section id来比较它。

PS:元素可以是任何东西,不总是用户或部分,但只要 id 相同。

非常感谢。

亲切的问候,约翰

4

1 回答 1

1

Although I am not entirely clear on the requirements, I think you may want to group elements by their id and the containing section id. This means you may be able to use an xsl:key to look-up elements

<xsl:key 
   name="lookup" 
   match="section//*[@id]" use="concat(ancestor::section[1]/@id, '|', @id)" />

Here we are looking up elements (any element) based on their section ID and their own ID. Then it is just a case of ignoring elements in sections where there is another element with a matching id that exists in the lookup.

<xsl:template 
   match="section//*[@id]
     [generate-id() 
     != generate-id(key('lookup', concat(ancestor::section[1]/@id, '|', @id))[1])]" />

(This is effectively saying, is this element the first element in the look-up. If not, ignore it)

Here is the full XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:key name="lookup" match="section//*[@id]" use="concat(ancestor::section[1]/@id, '|', @id)" />

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

   <xsl:template match="section//*[@id][generate-id() != generate-id(key('lookup', concat(ancestor::section[1]/@id, '|', @id))[1])]" />
</xsl:stylesheet>

When applied to your sample XML, the following is output

<root>
   <node id="a">
      <section id="a_1">
         <item id="0">
            <attribute>
               <color>Red</color>
            </attribute>
         </item>
      </section>
      <section id="a_2">
         <item id="0">
            <attribute>
               <color>Red</color>
            </attribute>
         </item>
      </section>
   </node>
   <node id="b">
      <section id="b_1">
         <user id="b_1a">
            <attribute>
               <name>John</name>
            </attribute>
         </user>
         <user id="b_1b">
            <attribute>a</attribute>
         </user>
      </section>
      <section id="b_1" method="create">
         <user id="b_1c">
            <attribute>a</attribute>
         </user>
      </section>
      <section id="b_2">
         <user id="b_1a">
            <attribute>
               <name>John</name>
            </attribute>
         </user>
      </section>
   </node>
</root>
于 2012-04-17T08:05:41.707 回答