 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 13th, 2004, 04:45 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sort using XSLT
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>
<xsl:copy-of select="@* | Article[@info = 'food']"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
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>
<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
|
|

October 13th, 2004, 05:47 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
If I've understood then you need to apply-templates to Article within the template that matches Documents/Document. You can then sort within this apply-templates element. Then add a template to match Article and copy the node.
--
Joe (Co-author Beginning XML, 3rd edition)
|
|

October 13th, 2004, 06:21 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
can we take it in this order instead.
first things first.
I have this XML file:
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>
How do I sort Article elements by state-attribute?
The structure of XML should be the same.
|
|

October 13th, 2004, 10:49 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
I am having problems understanding the output you actualy want!
How about you post the output you require..
You can sort by Article using:
<xsl:apply-templates select="Document/Document/Article">
<xsl:sort select="@state" order="ascending"/>
</xsl:apply-templates>
You say you want the same xml structure but which Articles should appear under which Documents?
Bryan
|
|

October 14th, 2004, 03:02 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Let's take it from the start.
input XML
<?xml version="1.0"?>
<Documents>
<Document title="1" chapter="i" href="file1.xml" filter="food">
<Article title="1.1" info="sub" filter="drink" state="1"/>
<Article title="1.2" info="main" filter="food" state="3"/>
<Article title="1.3" info="main" filter="drink" state="2"/>
</Document>
<Document title="2" chapter="ii" href="file2.xml" filter="drink">
<Article title="2.1" info="main" filter="drink" state="1"/>
<Article title="2.1" info="sub" filter="drink" state="3"/>
<Article title="2.2" info="main" filter="food" state="2"/>
</Document>
<Document title="3" chapter="1" href="file2.xml" filter="">
<Article title="3.1" info="sub" filter="drink" state=""/>
<Article title="3.2" info="child" filter="" state="8"/>
</Document>
<Document title="4" chapter="2" href="file2.xml" filter="">
<Article title="3.1" info="sub" filter="drink" state=""/>
<Article title="3.2" info="main" filter="food" state="1"/>
</Document>
</Documents>
I want pick only Article elements where attribute info="main"
info-attribute is not located on the Document level, only on Article level,
so when info="main" located it should be placed in the same originating Document node
wheter info-atribute is located on Document level or not.
I need to order output XML by Chapter, chapter-attribute, on Document-level. (i,ii,1,2 ... n)
Then I need to sort the output on Article level by state, ascending.
the output XML should look like this
<?xml version="1.0"?>
<Document title="1" chapter="i" href="file1.xml" filter="food">
<Article title="1.3" info="main" filter="drink" state="2"/>
<Article title="1.2" info="main" filter="food" state="3"/>
</Document>
<Document title="2" chapter="ii" href="file2.xml" filter="drink">
<Article title="2.1" info="main" filter="drink" state="1"/>
<Article title="2.2" info="main" filter="food" state="2"/>
</Document>
<Document title="4" chapter="2" href="file2.xml" filter="">
<Article title="3.2" info="main" filter="food" state="1"/>
</Document>
|
|

October 14th, 2004, 10:59 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Try the following..
If the Document nodes are always in the correct order - its gets you all the way there.
If not, sorting based on @chapter - with values like i, ii, iii, iv, v might be tricky.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="ISO-8859-1"/>
<xsl:template match="/ | @* | * | text()">
<xsl:copy>
<xsl:apply-templates select="@* | * | text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Documents">
<Documents>
<xsl:apply-templates select="Document[Article[@info='main']]">
</xsl:apply-templates>
</Documents>
</xsl:template>
<xsl:template match="Document">
<Document title="{@title}" chapter="{@chapter}" href="{@href}" filter="{@food}">
<xsl:apply-templates select="Article[@info='main']">
<xsl:sort select="@state" />
</xsl:apply-templates>
</Document>
</xsl:template>
</xsl:stylesheet>
|
|
 |