?
Solved

Sorting in XSLT

Posted on 2004-10-12
7
Medium Priority
?
224 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 200 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

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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…
Suggested Courses

777 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