2012-04-16 8 views
0

ここではXSL変換を非常に新しくしています。私は、このxmlファイルがある場合:XSLTを使用して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> 

そして、問題がある限り、私はレベルが行くことができるか深いかわからないですが、それは同じレベルにあり、重複があるので、削除します。 これは可能ですか?私は一日中これを修正しようとしており、手がかりを得ていません。 ご協力いただければ幸いです。

歓声、 ジョン

答えて

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> 

をまた、私はあなたが「同じレベルに」何を意味するかわからないんだけど、要素名にも一致する必要があれば、このテンプレートを使用します(注:このテンプレートは、サクソン9.3で働いていたが、Xalanのかサクソン6.5.5にはなかった)

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

UPDATE:ここでは、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> 
+0

ありがとうございました!それは今働く – John

関連する問題