Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old June 12th, 2009, 11:25 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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:
JohnBampton (June 12th, 2009)
 
Old June 12th, 2009, 11:52 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

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
 
Old June 12th, 2009, 11:58 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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:
JohnBampton (June 12th, 2009)
 
Old June 12th, 2009, 12:02 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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:
JohnBampton (June 12th, 2009)
 
Old June 12th, 2009, 12:17 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

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
 
Old June 12th, 2009, 12:26 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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
 
Old June 12th, 2009, 12:27 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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

Think before you post: What have you tried?
 
Old June 12th, 2009, 12:45 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

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??
 
Old June 12th, 2009, 12:59 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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:
JohnBampton (June 12th, 2009)
 
Old June 12th, 2009, 01:03 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

Brilliant thanks guys! :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with XSLT sort nyctechwriter XSLT 2 August 18th, 2006 02:27 PM
XSLT Dynamic xsl:sort Method kwilliams XSLT 2 July 20th, 2005 03:19 PM
XSLT-- xsl:sort help... debo XSLT 2 December 3rd, 2004 04:25 PM
Sort using XSLT xrow XSLT 5 October 14th, 2004 10:59 AM
xsl:sort and IE 5.0 Issue babloo81 XSLT 1 March 17th, 2004 06:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.