0

私はXSL変換に非常に慣れていないので、ここで質問します。このxmlファイルがある場合:

    <root> 
        <node id="a">
            <section id="a_1">
               <item id="0">
                    <attributes>
                        <color>Red</color>
                    </attributes>
               </item>
            </section>
            <section id="a_2">
               <item id="0">
                    <attributes>
                        <color>Red</color>
                    </attributes>
               </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></attribute>
                </user>

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

そして、私は出力を次のようにしたいと思います:

<root> 
        <node id="a">
            <section id="a_1">
               <item id="0">
                    <attributes>
                        <color>Red</color>
                    </attributes>
               </item>
            </section>
            <section id="a_2">
               <item id="0">
                    <attributes>
                        <color>Red</color>
                    </attributes>
               </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></attribute>
                </user>

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

問題は、レベルがどれだけ深くなるかわからないことですが、同じレベルにあり、重複している限り、それを削除します。これを行うことは可能ですか。私は一日中これを修正しようとしてきましたが、手がかりがありません。どんな助けでも大歓迎です。

乾杯、ジョン

4

1 に答える 1

2

これを試して:

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

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

  <!--If you want to remove any duplicate element (not just user,
  change the match to: match="*[@id = preceding::*/@id]"-->
  <xsl:template match="user[@id = preceding::user/@id]"/>

</xsl:stylesheet>

また、「同じレベルで」とはどういう意味かわかりませんが、要素名も一致させる必要がある場合は、次のテンプレートを使用してください: .5.)

<xsl:template match="*[@id = preceding::*[name() = name(current())]/@id]"/>

更新: Xalan および Saxon 6.5.5 で動作するように見えるテンプレートを次に示します。

  <xsl:template match="*[@id = preceding::*/@id]">
    <xsl:if test="not(@id = preceding::*[name() = name(current())]/@id)">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>
于 2012-04-16T15:37:00.887 に答える