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

June 12th, 2009, 11:25 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Once you have a first and last name element you can then easily sort:
Code:
<xsl:template match="students">
<xsl:variable name="students">
<xsl:apply-templates select="student" mode="change"/>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($students)/student">
<xsl:sort select="first"/>
<xsl:sort select="last"/>
</xsl:apply-templates>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|
|

June 12th, 2009, 11:52 AM
|
|
Friend of Wrox
|
|
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
|
|
Hi Martin,
In your code you have
Code:
<xsl:template match="stundent/*" mode="change">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
The "stundent/*" must be a typo right?
If so then this stylesheet is just a different identity transform??
I figured out the sorting. Thanks. It had to be name/first and name/last
Please correct me if I am wrong. I appreciate all the help you have provided. Thanks
|
|

June 12th, 2009, 11:58 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
The code in the last post is slightly wrong, you need
Code:
<xsl:template match="students">
<xsl:variable name="students">
<xsl:apply-templates select="student" mode="change"/>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($students)/student">
<xsl:sort select="name/first"/>
<xsl:sort select="name/last"/>
</xsl:apply-templates>
</xsl:template>
If you need to restore the original studentName element then you need to do that when processing the sorted student elements in your templates.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|
|

June 12th, 2009, 12:02 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Sorry about the typo, here is a corrected version:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:exsl="http://exslt.org/common"
xmlns:str="http://exslt.org/strings"
exclude-result-prefixes="exsl str">
<xsl:template match="students">
<xsl:variable name="students">
<xsl:apply-templates select="student" mode="change"/>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($students)/student">
<xsl:sort select="name/first"/>
<xsl:sort select="name/last"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="student" mode="change">
<xsl:copy>
<xsl:apply-templates mode="change"/>
</xsl:copy>
</xsl:template>
<xsl:template match="student/studentName" mode="change" priority="2">
<xsl:variable name="comp" select="str:tokenize(translate(., ', ', '||'), '|')"/>
<name>
<first>
<xsl:for-each select="$comp[position() != last()]">
<xsl:value-of select="."/>
<xsl:if test="position() != last()"><xsl:text> </xsl:text></xsl:if>
</xsl:for-each>
</first>
<last>
<xsl:value-of select="$comp[last()]"/>
</last>
</name>
</xsl:template>
<xsl:template match="student/*" mode="change">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="student">
<!-- process, this example simply copies -->
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|
|

June 12th, 2009, 12:17 PM
|
|
Friend of Wrox
|
|
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
|
|
That works. But now for the hard part. I want now want to output HTML something like
<xsl:for-each student>
"sorted"
<tr>
<td rowspan="2">
<img src ="{normalize-space(studentPhoto/path)}"></img>
</td>
<td >
<xsl:value-of select="concat(first,' ',last)">
</td>
</tr>
<tr>
<td>
<xsl:value-of select="studentLink"/>
</td>
</tr>
</xsl:for-each>
I can't seem to adapt your solution to do this.
Regards
|
|

June 12th, 2009, 12:26 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
This should help:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:exsl="http://exslt.org/common"
xmlns:str="http://exslt.org/strings"
exclude-result-prefixes="exsl str">
<xsl:output method="html" indent="yes"/>
<xsl:template match="students">
<xsl:variable name="students">
<xsl:apply-templates select="student" mode="change"/>
</xsl:variable>
<xsl:for-each select="exsl:node-set($students)/student">
<xsl:sort select="name/first"/>
<xsl:sort select="name/last"/>
<tr>
<td rowspan="2">
<img src="{normalize-space(studentPhoto/path)}"></img>
</td>
<td>
<xsl:value-of select="concat(name/first,' ',name/last)"/>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="studentLink"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
<xsl:template match="student" mode="change">
<xsl:copy>
<xsl:apply-templates mode="change"/>
</xsl:copy>
</xsl:template>
<xsl:template match="student/studentName" mode="change" priority="2">
<xsl:variable name="comp" select="str:tokenize(translate(., ', ', '||'), '|')"/>
<name>
<first>
<xsl:for-each select="$comp[position() != last()]">
<xsl:value-of select="."/>
<xsl:if test="position() != last()"><xsl:text> </xsl:text></xsl:if>
</xsl:for-each>
</first>
<last>
<xsl:value-of select="$comp[last()]"/>
</last>
</name>
</xsl:template>
<xsl:template match="student/*" mode="change">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

June 12th, 2009, 12:27 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
That's not hard at all - you just need to put whatever you want in the last template:
Code:
<xsl:template match="student">
<!-- process, this example simply copies -->
<tr>
<td rowspan="2">
<img src ="{normalize-space(studentPhoto/path)}"></img>
</td>
<td >
<xsl:value-of select="concat(first,' ',last)">
</td>
</tr>
<tr>
<td>
<xsl:value-of select="studentLink"/>
</td>
</tr>
</xsl:template>
|
|

June 12th, 2009, 12:45 PM
|
|
Friend of Wrox
|
|
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
|
|
Code:
<tr>
<td rowspan="2">
<img src="{normalize-space(studentPhoto/path)}"></img>
</td>
<td>
<xsl:value-of select="concat(name/first,' ',name/last)"/>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="studentLink"/>
</td>
</tr>
Thanks for your help, but I cannot seem to get the path and the studentlink to show??
|
|

June 12th, 2009, 12:59 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
We are getting closer :). Try
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:exsl="http://exslt.org/common"
xmlns:str="http://exslt.org/strings"
exclude-result-prefixes="exsl str">
<xsl:output method="html" indent="yes"/>
<xsl:template match="students">
<xsl:variable name="students">
<xsl:apply-templates select="student" mode="change"/>
</xsl:variable>
<xsl:for-each select="exsl:node-set($students)/student">
<xsl:sort select="name/first"/>
<xsl:sort select="name/last"/>
<tr>
<td rowspan="2">
<img src="{normalize-space(studentPhoto/path)}"></img>
</td>
<td>
<xsl:value-of select="concat(name/first,' ',name/last)"/>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="studentLink"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
<xsl:template match="student" mode="change">
<xsl:copy>
<xsl:apply-templates mode="change"/>
</xsl:copy>
</xsl:template>
<xsl:template match="student/studentName" mode="change" priority="2">
<xsl:variable name="comp" select="str:tokenize(translate(., ', ', '||'), '|')"/>
<name>
<first>
<xsl:for-each select="$comp[position() != last()]">
<xsl:value-of select="."/>
<xsl:if test="position() != last()"><xsl:text> </xsl:text></xsl:if>
</xsl:for-each>
</first>
<last>
<xsl:value-of select="$comp[last()]"/>
</last>
</name>
</xsl:template>
<xsl:template match="student/*" mode="change">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|
|

June 12th, 2009, 01:03 PM
|
|
Friend of Wrox
|
|
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
|
|
Brilliant thanks guys! :)
|
|
 |