[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Sorting in XSLT

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

649 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