2017-11-02 4 views
2

Ich habe eine xsl, die elemets in Reihenfolge sortiert, und ich wollte auch Leerzeichen nach bestimmten Knoten Beispiel hinzuzufügen:XSL Sortierung von Elementen, um während auch Räume Zugabe

<?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 ist

<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> 

Erwarteter heraus setzen:

<?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> 

es keine Leerzeichen hinzufügen, aber es funktioniert, wenn ich die sortin Streifen aus g xsl-Vorlage. Sortierung und Abstand funktionieren nicht zusammen. Gedanken?

Antwort

1

Ich glaube, Sie den weißen Raum haben in einer Vorlage des falschen Übereinstimmungsmuster und im Allgemeinen solche Sachen zu schaffen ist einfacher in XSLT 1.0, wo Sie Aufgaben in einer Vorlage mit niedrigerer Priorität mit xsl:next-match delegieren kann:

<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.

Mit XSLT 1.0 Sie benennen Vorlagen haben und verwenden call-template statt nächsten Match:

<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> 

schließlich mit XSLT 3 nur die zwei Vorlagen auf das Schreiben für die Elemente benötigen spezielle konzentrieren Behandlung:

<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

Honen +1 Yay! es funktionierte. Danke vielmals. – user1654352

Verwandte Themen