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

September 3rd, 2013, 01:01 PM
|
|
Authorized User
|
|
Join Date: Jun 2013
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks for a reply Mckay and SAM
This is works fine its working but...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"exclude-result-prefixes="xs saxon"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:saxon="http://saxon.sf.net/">
<xsl:template match="/">
<xsl:for-each select="root/E/F/A | root/d/A">
<xsl:sort select="."data-type="number"order="ascending"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
But in this code, instead of <xsl:sort select="A" > I given <xsl:sort select="." , Its not accepting as <xsl:sort select="A" >
1.Why its behaving like this?
My requirement is, I got to combine two different logic from two xpaths .
2.so I cant use one <xsl:value-of select="."/> for the two xpaths like "root/E/F/A | root/d/A">
I got to transform like this
<xsl:value-of select=first path of 'A'>
<xsl:value-of select=second path of 'A'>
I cant have one value of select for these two xpaths.
{
logic for path root/E/F/A
1 john london
3 leon london
}
{
logic for path2 root/d/A
6 neo chicaco
8 raj london
9 seon london
}
In this case I cant use one value-of select these blocks
Cant I use two value of select for two xpath ?
Last edited by sen1953; September 3rd, 2013 at 01:12 PM..
Reason: For understanding requirement
|
|

September 3rd, 2013, 01:15 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You say this code is working, and it looks fine to me. Your explanation as to why you can't use this code is incoherent. If it works, why isn't it OK?
Remember:
* the xsl:for-each should select the set of nodes to be sorted
* the xsl:sort select expression should evaluate the sort key, using each item to be sorted as the context item. So if the item being sorted is an A element, then xsl:sort select="A" won't work, unless your A elements have children called A, which doesn't appear to be the case.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

September 3rd, 2013, 03:32 PM
|
|
Authorized User
|
|
Join Date: Jun 2013
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks for your reply McKay
My problem is my requiremt is like below
Code:
root>
<E>
<F>
<A>3</A>
</F>
</E>
<E>
<F>
<A>8</A>
</F>
</E>
<E>
<F>
<A>2</A>
</F>
</E>
<d>
<b>
<A>5</A>
</b>
<b>
<A>1</A>
</b>
<b>
<A>9</A>
</b>
<b>
<A>7</A>
</b>
</d>
</root>
I got to sort based on 'A' elements. But not all ,particularly for these paths root/E/F/A and root/d/b/A
E nodes have 1 to many and b also 1 to many..
for these I cant apply these xsl as below
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"exclude-result-prefixes="xs saxon"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:saxon="http://saxon.sf.net/">
<xsl:template match="/">
<xsl:for-each select="root/E/F/A | root/d/A">
<xsl:sort select="."data-type="number"order="ascending"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
how to sort out this issue ?
<xsl:for-each select="root/E/F/A">
<xsl:sort select="."data-type="number"order="ascending"/>
<xsl:value-of select="."/>
</xsl:for-each>
<xsl:for-each select=" root/d/A">
<xsl:sort select="."data-type="number"order="ascending"/>
<xsl:value-of select="."/>
</xsl:for-each>
Can we combine two for-each then do sorting Is it possible or not.Thanks in advance
Last edited by sen1953; September 3rd, 2013 at 03:49 PM..
|
|

September 3rd, 2013, 04:04 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You keep changing the problem!
What output do you want from this latest input?
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

September 3rd, 2013, 04:24 PM
|
|
Authorized User
|
|
Join Date: Jun 2013
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks for your reply Mckay.
My required output is only
1235789
If you give solution by combine two for-each and then sorting , will it be great for my requirement. I completed my all work without sorting .That is my problem. Let us assume, each for-each will have complex logic not only
number. for example my requirement will be like this
Code:
2 john london this will from different path
5 Dillon london
7 london 6sep this will from different path l
9 madrid 4aug
so i need flexibility on sorting. Thanks in advance...
|
|

September 3rd, 2013, 04:56 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>My required output is only 1235789
Well you can achieve that using
Code:
<xsl:for-each select="//A">
<xsl:sort select="."/>
<xsl:value-of select="."/>
</xsl:for-each>
But you haven't explained why your own "working" code isn't good enough. You aren't explaining your requirements very clearly.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

September 3rd, 2013, 05:38 PM
|
|
Authorized User
|
|
Join Date: Jun 2013
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks for your reply and patience
Yes you are correct. I didnt explain my problem clearly..
this is my input xml
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>
my xsl code
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 select="accountList/account">
<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>
<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>
current output without soring
Code:
02 JOHN
07 PETE
01 REMOVED ADOGA
08 REMOVED NASH
09 HOLD LUCAS
21 HOLD DONALD
24 HOLD LONDON
Expected As usual I got to sortout by accno
Code:
01 REMOVED ADOGA
02 JOHN
07 PETE
08 REMOVED NASH
09 HOLD LUCAS
17 JOHN
20 JOHN
21 JOHN
24HOLD LONDON
That is my problem. Now you can understand my requirement as well. I tried many approach.I tried lot.. Please provide soltuion.
Thanks in advance
Last edited by sen1953; September 4th, 2013 at 11:37 AM..
Reason: I removed this word' I am trying this solution for past 3 weeks.
|
|

September 4th, 2013, 12:13 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
And back to my original answer (from nearly a month ago!):
Code:
<xsl:for-each select="accountList/account | accountList/previousAccount/account">
<xsl:sort select="accNo" order="ascending"/>
<xsl:value-of select="accNo"/>
<!-- do stuff with the other elements -->
</xsl:for-each>
What is wrong with this? Why does this not meet your requirements?
Is it because it still requires you to do some actual work (by filling in the bit in the middle?) or for some other reason I cannot fathom?
|
|

September 10th, 2013, 12:17 PM
|
|
Authorized User
|
|
Join Date: Jun 2013
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks Mckay and SAM
Thanks .I solved this by below approach
Code:
<xsl:for-each select="accountList/account | accountList/previousAccount/account">
<xsl:sort select="accNo" order="ascending"/>
<xsl:value-of select="accNo"/>
<!-- do stuff with the other elements -->
</xsl:for-each>
Problem is ,I did wrong xpaths. That is why its not working well.we got to concentrate more on xpaths when we go for two different xpath. I understood the concepts and I came to know how to use xpath efficiently as well.
And also we can solve this same issue by the below approach
Code:
<xsl:variable name="sortingAccNumber">
- <xsl:for-each select="accountList/account">
<xsl:sort select="accNo" />
<xsl:copy-of select="." />
</xsl:for-each>
- <xsl:for-each select="accountList/previousAccount/account">
<xsl:sort select="accNo" />
<xsl:copy-of select="." />
</xsl:for-each>
</xsl:variable>
- <xsl:for-each select="ext:node-set($sortingAccNumber)/account">
<xsl:sort select="accNo" />
<xsl:value-of select="accNo" />
</xsl:for-each>
Thanks a lot Mckay and Samjudson for your extended support
|
|
 |