Solved

Sorting in XSLT

Posted on 2004-10-12
7
219 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
  • 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

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
website content maintenance 3 79
C# XDocument.Save adds unwanted encoding information 5 42
microsoft access - xml 10 58
Post xml via PHP Soap Client 3 38
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

910 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

20 Experts available now in Live!

Get 1:1 Help Now