0

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>
2
  • I got solution. Need sorting on particular element not main node level. Edited my XSL. Thanks Commented Apr 24, 2014 at 18:11
  • 2
    You could combine the last two templates into one using match="Catalog/Books | Catalog/libraries". As you have found a solution yourself, the right way to close your problem is to post your solution as an answer and to accept it. Commented Apr 24, 2014 at 20:34

1 Answer 1

0
<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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.