2017-11-02 2 views
2

を追加しながら、私は順番にelemetsをソートして、私はまた、特定のノード 例の後にスペースを追加したいのxslを持っている:XSLのソートもスペースに

<?xml version="1.0"?> 
<catalog> 
    <fruits criteria="XXX"> 
     <color>XXX</color> 
     <type>XXX</type> 
     <taste>XXX</taste> 
    </fruits> 
    <veggies> 
     <carrot>XXXX</carrot> 
     <beetroot>XXX</beetroot> 
     <pumpkin>XXX</pumpkin> 
    </veggies> 
    <something> 
     <xxx>42343</xxx> 
    </something> 
</catalog> 

XSLは

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="fruits"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:apply-templates select="type"/> 
      <xsl:apply-templates select="taste"/> 
      <xsl:apply-templates select="color"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="veggies/* | fruits/* | something/*"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
     <xsl:if test="following-sibling::*"> 
      <xsl:text>&#x0A;</xsl:text> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 
です

期待する出力:

<?xml version="1.0"?> 
<catalog> 
    <fruits criteria="XXX"> 
     <type>XXX</type> 
     <taste>XXX</taste> 
     <color>XXX</color>  
    </fruits> 

    <veggies> 
     <carrot>XXXX</carrot> 
     <beetroot>XXX</beetroot> 
     <pumpkin>XXX</pumpkin> 
    </veggies> 

    <something> 
     <xxx>42343</xxx> 
    </something> 
</catalog> 

これはスペースを追加しませんが、 g xslテンプレート。ソートとスペーシングの両方が同時に機能しません。思考?私はあなたが間違ったマッチパターンのテンプレートにし、一般的なものに作成するホワイトスペースを持っていると思う

答えて

1

あなたはxsl:next-matchと低い優先順位のテンプレートにタスクを委任することができXSLT 1.0に簡単です:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes"/> 

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

    <xsl:template match="fruits"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*, type, taste, color"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="catalog/*[following-sibling::*]"> 
     <xsl:next-match/> 
     <xsl:text>&#10;&#10;</xsl:text> 
    </xsl:template> 

</xsl:transform> 

http://xsltransform.net/6pS1zDR。 XSLT 3であなたが特別に必要とする要素のみの2つのテンプレートを書くことに集中することができます。最後

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="catalog/fruits" name="fruits" priority="5"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:apply-templates select="type"/> 
      <xsl:apply-templates select="taste"/> 
      <xsl:apply-templates select="color"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="catalog/fruits[following-sibling::*]" priority="5"> 
     <xsl:call-template name="fruits"/> 
     <xsl:text>&#10;</xsl:text> 
    </xsl:template> 

    <xsl:template match="catalog/*[following-sibling::*]"> 
     <xsl:call-template name="identity"/> 
     <xsl:text>&#10;</xsl:text> 
    </xsl:template> 

</xsl:transform> 

、:XSLT 1.0で

名前のテンプレートを持っているとコールテンプレートの代わりに、次のマッチを使用します。治療:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="3.0"> 

    <xsl:mode on-no-match="shallow-copy"/> 

    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes"/> 

    <xsl:template match="fruits"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*, type, taste, color"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="catalog/*[following-sibling::*]"> 
     <xsl:next-match/> 
     <xsl:text>&#10;&#10;</xsl:text> 
    </xsl:template> 

</xsl:stylesheet> 
+0

ホーエン+1よー!出来た。どうもありがとう。 – user1654352

関連する問題