Solved

Manipulate the sequence of tags in xml with xsl

Posted on 2015-02-21
6
48 Views
Last Modified: 2015-02-25
Hi,

i need to change the sequence of the xml tag , below is the current  xml

<Summary>
    <Category type="CA">
    <Description>You no longer qualify for CA</Description>
    </Category>
    <Category type="FF">
      <Description>You no longer qualify for FF</Description>
    </Category>
    <Category type="MM">
     <Description>You no longer qualify </Description>
    </Category>
    <Category type="CC">
   <Description>Based on information you provided </Description>
    </Category>
  </Summary>

i always need FF first, then CA, CC and MM in the order.. is there a way to do this in xslt.

<Summary>
    <Category type="FF">
      <Description>You no longer qualify for FF</Description>
    </Category>
    <Category type="CA">
    <Description>You no longer qualify for CA</Description>
    </Category>
   <Category type="CC">
   <Description>Based on information you provided </Description>
    </Category>
    <Category type="MM">
     <Description>You no longer qualify </Description>
    </Category>
 
  </Summary>
 

Thank you
0
Comment
Question by:sri1209
  • 5
6 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40623045
The brute force approach in XSLT1

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
      
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Summary">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="Category[@type = 'FF']"/>
            <xsl:apply-templates select="Category[@type = 'CA']"/>
            <xsl:apply-templates select="Category[@type = 'CC']"/>
            <xsl:apply-templates select="Category[@type = 'MM']"/>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40623063
A more refined technique in XSLT1 is described here
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XML/Q_21571844.html
I like the elegance of my approach there, but it requires the document() function to be switched on
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40623066
If you are using XSLT2, it is easy, just make a sequence of the four terms and use index-of or a function in the @select of the sort
Let me know if you like a XSLT2 solution
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 40623068
anyway, just for the completeness of the answer

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 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:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Summary">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="Category">
                <xsl:sort select="index-of(('FF', 'CA', 'CC', 'MM'), @type)" data-type="number"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:sri1209
ID: 40631506
Hi Geert,

Sorry for the delay in replying back, you are awesome, works perfectly.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40631674
welcome
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 47
control image tags in a string ? 12 131
Issue with XSLT mapping 10 36
some keys in my laptop is not working. any suggestion 6 29
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question