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 March 27th, 2008, 03:57 AM
Registered User
 
Join Date: Mar 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSL for-each/when problem


Hey,

Not sure if much XSL stuff goes on in here, but im currently having a problem and the debug information is as useless as a a chocolate fireguard.

Im currently just trying to do something simple...

Read in an XML file, style it with an XSL file, then display it in a div...
All works fine, but as soon as i try to put in sorting logic it just all falls apart and i dont have a clue why...

Code:
<xsl:param name="sort_type"/>


<xsl:template match="/">

<table cellspacing="0" cellpadding="0" border="0" >
  <tr>
    <th>Address 1</th>
    <th>Address 2</th>
  </tr>
<xsl:for-each select="people/person">
  <xsl:choose>
    <xsl:when test="contains($sort_type, 'address_1')">
      <xsl:sort select="address_1" order="descending" data-type="text" />        
    </xsl:when>
    <xsl:otherwise>
      <xsl:sort select="id" order="descending" data-type="number" />
    </xsl:otherwise>
  </xsl:choose>
  <tr>
    <td><xsl:value-of select="address_1" /></td>
    <td><xsl:value-of select="address_2" /></td>
  </tr>
</xsl:for-each>
</table> 

    </xsl:template>
There is also stuff for the stylesheet defines but i thought i would only bother showing the core part. Anyway as you can see i do the for-each, then sort it based on the $sort_type, the sort_type is set via javascript externally, but i know it works as if i display it via XSL without the sorting logic it displays the correct contents. The default XML contains people, which contains many persons, all having address 1,2 and an ID but i only want to display the addresses.

The error i keep getting basically says there is an exception when processing the XSL, if i take out the sorting logic it works fine, so am i doing something wrong, im basing most of my stuff from the w3schools and other various tutorials online...

ANY help would be great!

 
Old March 27th, 2008, 04:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>the debug information is as useless as a a chocolate fireguard.

Perhaps you need a better development environment? Might I guess that you are trying to develop and test your code by running it in the browser? That's the worst possible route you could choose.

This is what Saxon says about your code:

Error at xsl:sort on line 18 column 74 of file:/c:/temp/test.xsl:
  XTSE0010: An xsl:when element must not contain an xsl:sort element
Error at xsl:sort on line 21 column 69 of file:/c:/temp/test.xsl:
  XTSE0010: An xsl:otherwise element must not contain an xsl:sort element
Failed to compile stylesheet. 2 errors detected.

Which is about as clear a statement of your error as I could come up with.

In XSLT 2.0 you can write a conditional expression (if-then-else) inside the xsl:sort select="". If you're stuck with XSLT 1.0 then one way of solving the problem is to have two sort keys, one of which has no effect:

<xsl:for-each select="people/person">
  <xsl:sort select="address_1[contains($sort_type, 'address_1')]"
    order="descending" data-type="text" />
  <xsl:sort select="id[not(contains($sort_type, 'address_1'))]"
    order="descending" data-type="number" />

Another is to compute a variable $sort_key outside the for-each, with value "address_1" or "id", and then use <xsl:sort select="*[name()=$sort_key]".


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 27th, 2008, 04:26 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can't put the <xsl:sort> inside an <xsl:choose> like that.

Instead try to define two separate sorts, one of which may be not used depending on your variable value:

Code:
<xsl:for-each select="people/person">
  <xsl:sort select="address_1[$sort_type='address_1']"  order="descending" data-type="text" />
  <xsl:sort select="id" order="descending" data-type="number" />
  ...
/- Sam Judson : Wrox Technical Editor -/
 
Old March 27th, 2008, 04:36 AM
Registered User
 
Join Date: Mar 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey,

I am using the browser (firefox + firebug) i would have used some software for editing the XSL but there were no decent free editors that i could find...

Oh well as long as i know why its not working, it seems a tad odd that you cant do sorts within switch statements (i know they are not called switches in here but same principle)

Cheers guys!

 
Old March 27th, 2008, 05:49 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Free editor - http://www.liquid-technologies.com/Download.aspx



 
Old March 27th, 2008, 05:52 AM
Registered User
 
Join Date: Mar 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

cheers!

 
Old March 27th, 2008, 09:41 AM
Registered User
 
Join Date: Mar 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

can you do logical and (&&) when doing the when/ifs?

i want to check for 2 things

<xsl:choose>
    <xsl:when test="contains($sort_type, 'address_1') AND contains($sort_type", 'ascending'">
      <xsl:sort select="address_1" order="descending" data-type="text" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:sort select="address_1" order="ascending" data-type="text" />
    </xsl:otherwise>
  </xsl:choose>

 
Old March 27th, 2008, 09:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Since you're asking on a Wrox site, could I suggest you get yourself a reference book? It's a much quicker way of finding out basic information like this.

The "and" operator in XPath is spelt "and".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 27th, 2008, 09:56 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

As already pointed out, you can't put an xsl:sort inside of xsl:choose/when.
With XSLT 2.0 you could instead use
Code:
<xsl:sort select="address_1"
          order="{if (contains($sort_type, 'address_1') and contains($sort_type", 'ascending') then 'descending' else 'ascending'"
          data-type="text"/>
--
  Martin Honnen
  Microsoft MVP - XML
 
Old March 27th, 2008, 10:07 AM
Registered User
 
Join Date: Mar 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes i know the previous posters pointed that out, its just i copied and pasted the code to show an example of what i wanted to do...

Whats the difference between a wrox site and any other site? i only found this by fluke when looking for XSL based communities, i only plan on doing some small things with XSL as im having to spend some time helpping out on another project. Well i say small things, for XSL it may be quite big but in any other language the logic would be fairly simple to implement...

Thanks again for your answer though, if there is an AND then things should be easier when i get back to the office.






Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:if problem lscjtw XSLT 2 July 26th, 2007 11:29 AM
XSL problem cheez XSLT 1 September 14th, 2006 01:20 AM
Problem with XSL sandeepa XSLT 5 May 13th, 2005 04:31 AM
Help with xsl:when problem sundar_revathi XSLT 2 November 9th, 2004 07:39 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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