Solved

Manipulate the sequence of tags in xml with xsl

Posted on 2015-02-21
6
52 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.

737 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