 |
| 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
|
|
|
|

August 5th, 2013, 12:46 PM
|
|
Authorized User
|
|
Join Date: Jun 2013
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
xslt sorting from two different X paths
Code:
<?xml version="1.0" encoding="UTF-8"?>
<accountList>
<previousAccount>
<account>
<lastName>NASH</lastName>
<accountStatus>REMOVED</accountStatus>
<accNo>08</accNo>
</account>
<account>
<lastName>ADOGA</lastName>
<accountStatus>REMOVED</accountStatus>
<accNo>01</accNo>
</account>
<account>
<lastName>LUCAS</lastName>
<accountStatus>HOLD</accountStatus>
<accNo>09</accNo>
</account>
<account>
<lastName>DONALD</lastName>
<accountStatus>HOLD</accountStatus>
<accNo>21</accNo>
</account>
<account>
<accountStatus>HOLD</accountStatus>
<lastName>LONDON</lastName>
<accNo>24</accNo>
</account>
</previousAccount>
<account>
<Title>Mr</Title>
<firstName>RICHARD</firstName>
<lastName>JOHN</lastName>
<city>london</city>
<accNo>02</accNo>
</account>
<account>
<Title>Mr</Title>
<firstName>xxx</firstName>
<lastName>JOHN</lastName>
<city>London</city>
<accNo>17</accNo>
</account>
<account>
<Title>Mr</Title>
<firstName>HEWIT</firstName>
<lastName>JOHN</lastName>
<city>LONDON</city>
<accNo>20</accNo>
</account>
<account>
<Title>Mr</Title>
<firstName>xxx</firstName>
<lastName>JOHN</lastName>
<city>LONDON</city>
<accNo>21</accNo>
</account>
<account>
<Title>Mr</Title>
<firstName>KEVIN</firstName>
<lastName>PETE</lastName>
<city>LONDON</city>
<accNo>07</accNo>
</account>
</accountList>
xslt code
<xsl:stylesheet version="2.0"
xsi:schemaLocation="http://www.w3.org/1999/XSL/Transform"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="text"/>
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:for-each-group select="accountList/account" group-starting-with="*[firstName != 'xxx' or lastName != preceding-sibling::*[1]/lastName]">
<xsl:sort select="accNo" order="ascending"/>
<xsl:value-of select="accNo"/>
<xsl:text> </xsl:text>
<xsl:value-of select="lastName"/>
<xsl:value-of select="$newline"/>
</xsl:for-each-group>
<xsl:value-of select="$newline"/>
<xsl:for-each select="accountList/previousAccount/account">
<xsl:sort select="accNo" order="ascending"/>
<xsl:value-of select="accNo"/>
<xsl:text> </xsl:text>
<xsl:value-of select="accountStatus"/>
<xsl:text> </xsl:text>
<xsl:value-of select="lastName"/>
<xsl:value-of select="$newline"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
In my requirement ,I got to sort accNo which are different nodes.I think I'll do much better explaining this in code rather than words, accountList/previousAccount/account/accNo accountList/account/accNo
I used descendant,which is not working fine for my requirement.Here I have used two for-each for two nodes and sorted separtedly.But this is not a required output. Please help me to sort out this please find my output below
Actual Output
Code:
02 JOHN
07 PETE
20 JOHN
01 REMOVED ADOGA
08 REMOVED NASH
09 HOLD LUCAS
21 HOLD DONALD
24 HOLD LONDON
Expecting output
Code:
01 REMOVED ADOGA
02 JOHN
07 PETE
08 REMOVED NASH
09 HOLD LUCAS
20 JOHN
21 HOLD DONALD
24 HOLD LONDON
accNo will be in sorte
Output is in sorted format.
Last edited by sen1953; August 7th, 2013 at 10:05 AM..
Reason: Understanding purpose i modified the xml and xslt
|
|

August 21st, 2013, 09:08 AM
|
|
Authorized User
|
|
Join Date: Jun 2013
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Please give some idea , whether its is possible or not?
Please give some idea , whether its is possible or not? I am unable to find solution for this . Can we sort one element which is from two different template.?
|
|

August 21st, 2013, 12:25 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Try starting with something like this:
Code:
<xsl:for-each select="accountList/account | accountList/previousAccount/account">
<xsl:sort select="accNo" order="ascending"/>
<!-- do something in here with each item -->
</xsl:for>
|
|

August 29th, 2013, 11:57 AM
|
|
Authorized User
|
|
Join Date: Jun 2013
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks Som,
Thanks SAM,,Already I tried this approach ,again i check xpath and come back again..Thanks lot for your reply
|
|

August 30th, 2013, 10:45 AM
|
|
Authorized User
|
|
Join Date: Jun 2013
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Hi SAM..Given solution is not working for me
Given solution is not working for me..I am getting error like this when use two select operation in for-each for sorting
Error
A sequence of more than one item is not allowed as the @select attribute of xsl:sort; SystemID: file
Please help me SAM..
|
|

August 30th, 2013, 10:58 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
I didn't have a sequence in the select of the xsl:sort - I had a sequence in the select of the for-each.
I've tested this using the following XML and it works fine:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<A id="1"/>
<B id="2"/>
<A id="2"/>
<B id="1"/>
<A id="4"/>
<B id="1"/>
</root>
XSLT:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:saxon="http://saxon.sf.net/"
version="1.0"
exclude-result-prefixes="xs saxon">
<xsl:template match="/">
<xsl:for-each select="root/A | root/B">
<xsl:sort select="@id" data-type="number"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
Code:
<A id="1"/><B id="1"/><B id="1"/><B id="2"/><A id="2"/><A id="4"/>
|
|

August 30th, 2013, 12:53 PM
|
|
Authorized User
|
|
Join Date: Jun 2013
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks for your valuable time,
I run and modified the input xml and run it form my requirement.I didn't get the expected result.See this is my problem , This is my xml structure
<?xml version="1.0" encoding="UTF-8"?>
<root>
<E>
<F>
<A>8</A>
<A>2</A>
<A>7</A>
<A>10</A>
<A>3</A>
<A>7</A>
</F>
</E>
<d>
<A>5</A>
<A>1</A>
<A>9</A>
</d> </root>
I tried this way...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:saxon="http://saxon.sf.net/"
version="1.0"
exclude-result-prefixes="xs saxon">
<xsl:template match="/">
<xsl:for-each select="root/E/F | root/D">
<xsl:sort select="A"data-type="number"order="ascending"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
My wrong output
<?xml version="1.0" encoding="UTF-8"?>
<F>
<A>8</A>
<A>2</A>
<A>7</A>
<A>10</A>
<A>3</A>
<A>7</A>
</F>
The problem is missing out this element
<d>
<A>5</A>
<A>1</A>
<A>9</A>
</d>
This is my requirement input xml..Please advice me...
Last edited by sen1953; August 30th, 2013 at 01:44 PM..
|
|

September 2nd, 2013, 09:49 AM
|
|
Authorized User
|
|
Join Date: Jun 2013
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Hi SAM,
I am looking for your reply ? Give me any suggestion pls
|
|

September 2nd, 2013, 10:12 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You are trying to sort the F elements based on the value of their child A elements. But they have more than one child A element, so you get an error, because the sort key must select a single value for each item in the sequence being sorted.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

September 2nd, 2013, 11:58 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
That and the fact that XML is case sensitive, so root/D will not find 'd' elements.
|
|
 |