Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 5th, 2013, 01:46 PM
Authorized User
Points: 83, Level: 1
Points: 83, Level: 1 Points: 83, Level: 1 Points: 83, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Location: Heathrow ,London
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Default 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 11:05 AM. Reason: Understanding purpose i modified the xml and xslt
Reply With Quote
  #2 (permalink)  
Old August 21st, 2013, 10:08 AM
Authorized User
Points: 83, Level: 1
Points: 83, Level: 1 Points: 83, Level: 1 Points: 83, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Location: Heathrow ,London
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Default 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.?
Reply With Quote
  #3 (permalink)  
Old August 21st, 2013, 01:25 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,133
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #4 (permalink)  
Old August 29th, 2013, 12:57 PM
Authorized User
Points: 83, Level: 1
Points: 83, Level: 1 Points: 83, Level: 1 Points: 83, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Location: Heathrow ,London
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Default Thanks Som,

Thanks SAM,,Already I tried this approach ,again i check xpath and come back again..Thanks lot for your reply
Reply With Quote
  #5 (permalink)  
Old August 30th, 2013, 11:45 AM
Authorized User
Points: 83, Level: 1
Points: 83, Level: 1 Points: 83, Level: 1 Points: 83, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Location: Heathrow ,London
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Default 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..
Reply With Quote
  #6 (permalink)  
Old August 30th, 2013, 11:58 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,133
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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"/>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #7 (permalink)  
Old August 30th, 2013, 01:53 PM
Authorized User
Points: 83, Level: 1
Points: 83, Level: 1 Points: 83, Level: 1 Points: 83, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Location: Heathrow ,London
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Default 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 02:44 PM.
Reply With Quote
  #8 (permalink)  
Old September 2nd, 2013, 10:49 AM
Authorized User
Points: 83, Level: 1
Points: 83, Level: 1 Points: 83, Level: 1 Points: 83, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2013
Location: Heathrow ,London
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Default Hi SAM,

I am looking for your reply ? Give me any suggestion pls
Reply With Quote
  #9 (permalink)  
Old September 2nd, 2013, 11:12 AM
mhkay's Avatar
Wrox Author
Points: 18,277, Level: 58
Points: 18,277, Level: 58 Points: 18,277, Level: 58 Points: 18,277, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,933
Thanks: 0
Thanked 282 Times in 277 Posts
Default

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
Reply With Quote
  #10 (permalink)  
Old September 2nd, 2013, 12:58 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,133
Thanks: 1
Thanked 189 Times in 188 Posts
Default

That and the fact that XML is case sensitive, so root/D will not find 'd' elements.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting with XSLT gullamahi XSLT 0 April 29th, 2013 06:02 AM
XSLT sorting issue? vb89 XSLT 11 February 22nd, 2012 05:32 PM
Sorting in XSLT hchaudh1 XSLT 1 November 30th, 2005 05:37 AM
Using XSLT arguments for in XML paths jacob XSLT 7 November 7th, 2005 11:04 AM
pre-sorting in XSLT - help! thebnut XSLT 6 April 6th, 2004 08:45 AM



All times are GMT -4. The time now is 07:46 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.