Solved

Manipulate the sequence of tags in xml with xsl

Posted on 2015-02-21
6
46 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

707 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

15 Experts available now in Live!

Get 1:1 Help Now