Pergunta

I am working on XML transformation and before I start transform XML I have sort only child elements. My current XSL file sort out child element and even parent element which I don't want to sort

Please see below

My XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Catalog>
    <libraries>
        <library3>
            <Name> COBB </Name>
            <city> Marietta </city>
        </library3>
        <library1>
            <Name> COBB </Name>
            <city> Marietta </city>
        </library1>
        <library4>
            <Name> COBB </Name>
            <city> Marietta </city>
        </library4>
        <library2>
            <Name> COBB </Name>
            <city> Marietta </city>
        </library2>
    </libraries>
    <Books>
        <Book1>
            <Name>Wise Otherwise</Name>
            <author>Great Expectations</author>
        </Book1>
        <Book3>
            <Name>Wise Otherwise</Name>
            <author>Great Expectations</author>
        </Book3>
        <Book6>
            <Name>Wise Otherwise</Name>
            <author>Great Expectations</author>
        </Book6>
        <Book2>
            <Name>Wise Otherwise</Name>
            <author>Great Expectations</author>
        </Book2>
    </Books>

</Catalog>

Desire Output

<?xml version="1.0" encoding="UTF-8"?>
<Catalog>
  <libraries>
    <library1>
      <city> Marietta </city>
      <Name> COBB </Name>
    </library1>
    <library2>
     <city> Marietta </city>
     <Name> COBB </Name>
    </library2>
    <library3>
     <city> Marietta </city>
     <Name> COBB </Name>
    </library3>
    <library4>
     <city> Marietta </city>
     <Name> COBB </Name>
    </library4>
   </libraries>

   <Books>
    <Book1>
     <author>Great Expectations</author>
     <Name>Wise Otherwise</Name>
    </Book1>
    <Book2>
     <author>Great Expectations</author>
     <Name>Wise Otherwise</Name>
    </Book2>
    <Book3>
     <author>Great Expectations</author>
     <Name>Wise Otherwise</Name>
    </Book3>
    <Book6>
      <author>Great Expectations</author>
      <Name>Wise Otherwise</Name>
     </Book6>
   </Books>
   </Catalog>

My XSL

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

<xsl:template match="Catalog/Books">
    <xsl:copy>
         <xsl:apply-templates select="node()">
            <xsl:sort select="name()"/>
         </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="Catalog/libraries">
    <xsl:copy>
         <xsl:apply-templates select="node()">
            <xsl:sort select="name()"/>
         </xsl:apply-templates>
    </xsl:copy>
</xsl:template>
Foi útil?

Solução

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

<xsl:template match="Catalog/Books">
<xsl:copy>
     <xsl:apply-templates select="node()">
        <xsl:sort select="name()"/>
     </xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="Catalog/libraries">
<xsl:copy>
     <xsl:apply-templates select="node()">
        <xsl:sort select="name()"/>
     </xsl:apply-templates>
</xsl:copy>
</xsl:template>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top