Solved

Sorting in XSLT

Posted on 2004-10-12
7
222 Views
Last Modified: 2013-11-18
Hi,

I am having problem sorting on child-nodes (Article elements).
I would like to sort Article elements instead of Document elements
(below), by state-attribute. State is an integer.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

       <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

       <xsl:template match="/">
               <Documents>
                       <xsl:apply-templates select="Documents/Document">
                               <xsl:sort select="@state" order="ascending"/>
                       </xsl:apply-templates>
               </Documents>
       </xsl:template>

       <xsl:template match="/Documents/Document">
               <xsl:copy>
                               <!-- I need sorting on this level -->
                               <xsl:copy-of select="@* | Article[@info = 'food']"/>
               </xsl:copy>
       </xsl:template>
</xsl:stylesheet>

<xsl:sort select="@title" order="ascending"/>

-xrow
0
Comment
Question by:xrow
[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
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:J_Mak
ID: 12286173
Can you please post your XML file. Could you please just elaborate more on the specifics of what you want to sort, thanks.
0
 

Author Comment

by:xrow
ID: 12286237
Beside the filtering part already done in the XSLT,
what I want to sort is elements that contain attribute state. I would like to sort Article elements by state-attribute in the acesending order.

XML file:

<Documents>
       <Document title="1" chapter="1" href="file1.xml" filter="food">
               <Article title="1.1" info="sub" filter="food" state="1"/>
               <Article title="1.2" info="main" filter="food" state="3"/>
       </Document>
       <Document title="2" chapter="2" href="file2.xml" filter="drink">
               <Article title="2.1" info="sub" filter="drink" state="4"/>
               <Article title="2.2" info="main" filter="food" state="2"/>
       </Document>
       <Document title="3" chapter="3" href="file2.xml" filter="">
               <Article title="3.1" info="sub" filter="drink" state=""/>
               <Article title="3.2" info="child" filter="" state="8"/>
       </Document>
</Documents>
0
 
LVL 7

Expert Comment

by:J_Mak
ID: 12286386
Did you want to output an XML file or just plain text like so:

<Article title="2.2" info="main" filter="food" state="2"/>
<Article title="1.2" info="main" filter="food" state="3"/>
<Article title="2.1" info="sub" filter="drink" state="4"/>
<Article title="1.1" info="sub" filter="food" state="1"/>
<Article title="3.1" info="sub" filter="drink" state="7"/>
<Article title="3.2" info="child" filter="food" state="8"/>

Also, did you want to do 2 sorts? Becaues in your question you indicate that you want to sort by title attributes:

<xsl:sort select="@title" order="ascending"/>

And on another line you indicate this:

<!-- I need sorting on this level -->
<xsl:copy-of select="@* | Article[@info = 'food']"/>

And then on another line:

<xsl:sort select="@state" order="ascending"/>
0
 

Author Comment

by:xrow
ID: 12287203

<!-- I need sorting on this level -->
<xsl:copy-of select="@* | Article[@info = 'food']"/>

all other functions except sorting are supposed to be left there. i need to add sorting on Article level.
dissregard sorting on Document level. it is not necessary. Only Article level (sort-by state) is needed.

thanx.

-mike
0
 
LVL 6

Accepted Solution

by:
PeterCiuffetti earned 50 total points
ID: 12434833
This XSLT will sort the articles by @state.  I do it in a for-each loop that selects all articles.  If you do it inside an apply-templates, then it will only sort the children of the current element.  If you want to sort all the articles, it have to grab all of them in a single pass.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="Documents">
       <Documents>
           <xsl:for-each select="Document/Article">
                        <xsl:sort select="@state" order="ascending"/>
                        <xsl:apply-templates select="."/>
           </xsl:for-each>
       </Documents>
</xsl:template>

<xsl:template match="Article">
        <xsl:copy>
                 <xsl:copy-of select="@* | Article[@info = 'food']"/>
        </xsl:copy>
</xsl:template>

</xsl:stylesheet>


Produces...

<Documents>
  <Article title="3.1" info="sub" filter="drink" state=""></Article>
  <Article title="1.1" info="sub" filter="food" state="1"></Article>
  <Article title="2.2" info="main" filter="food" state="2"></Article>
  <Article title="1.2" info="main" filter="food" state="3"></Article>
  <Article title="2.1" info="sub" filter="drink" state="4"></Article>
  <Article title="3.2" info="child" filter="" state="8"></Article>
</Documents>

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 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