Solved

Manipulate the sequence of tags in xml with xsl

Posted on 2015-02-21
6
47 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
DevOps Toolchain Recommendations

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

 
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 37
Adding Namespace to the second node of the xml 16 65
Create XML 5 47
"Black Box" Testing of Control System Software 2 26
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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now