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が同じである限り、追加のメソッドがある場合でも、1つのセクションIDと見なされます。したがって、「メソッド作成」が含まれている2番目のセクションID(b_1)のユーザーID(b_1a)を削除します。これは本当に私を苛立たせます、そして私は方法を省略することができませんでした。ですので、どんな助けでも大歓迎です。セクションIDb_2を見ると、同じユーザーID b_1と同じ「John」がありますが、セクションIDが異なるため、削除しません。したがって、基本的にはセクションIDに基づいて比較します。

PS:要素は、必ずしもユーザーまたはセクションである必要はありませんが、IDが同じである限り何でもかまいません。

どうもありがとう。

よろしく、ジョン

4

1 に答える 1

1

要件については完全には明確ではありませんが、要素をIDと含まれているセクションIDでグループ化することをお勧めします。これは、 xsl:keyを使用して要素を検索できる可能性があることを意味します

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

ここでは、セクションIDと独自のIDに基づいて要素(任意の要素)を検索しています。次に、ルックアップに一致するIDを持つ別の要素が存在するセクションの要素を無視する場合です。

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

(これは事実上、この要素がルックアップの最初の要素であると言っています。そうでない場合は無視してください)

これが完全な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>

サンプル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_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 に答える