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 April 11th, 2006, 12:38 PM
Registered User
 
Join Date: Apr 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sorting a xsl:if test="position()"

Hi All,

I am wondering if someone can help.
I am trying to sort an XSL transformation where the output is from xsl:if statements....
I need to sort the output in ascending order of the following xsl:if
<xsl:if test="position()=9">

Charmaigne
================================================== ==
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:qq="urn:schemas-microsoft-com:office:spreadsheet" version="1.0">
<xsl:template match="/">
    <HTML>
    <TABLE>
   <xsl:for-each select="qq:Workbook/qq:Worksheet/qq:Table/qq:Row">
   <TR>
    <xsl:for-each select="qq:Cell">
            <xsl:if test="position()=1">
                <TD>
                    <xsl:value-of select="qq:Data"/>
                </TD>
            </xsl:if>
            <xsl:if test="position()=9">
                <TD>
                    <xsl:value-of select="qq:Data"/>
                </TD>
            </xsl:if>
        <xsl:if test="position()=7">
            <TD>
                <xsl:value-of select="qq:Data"/>
            </TD>
        </xsl:if>
        <xsl:if test="position()=11">
            <TD>
                <xsl:value-of select="qq:Data"/>
            </TD>
        </xsl:if>
        <xsl:if test="position()=4">
            <TD>
                <xsl:value-of select="qq:Data"/>
            </TD>
        </xsl:if>
    </xsl:for-each>
       </TR>
   </xsl:for-each>

    </TABLE>
    </HTML>
</xsl:template>
</xsl:stylesheet>

 
Old April 11th, 2006, 05:39 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your inner for-each seems a rather convoluted way of writing

<xsl:for-each select="qq:Cell[position() = (1,4,7,9,11)]">
                <TD>
                    <xsl:value-of select="qq:Data"/>
                </TD>
</xsl:for-each>

That's 2.0 syntax: in 1.0 you have to write the predicate as

position() = 1 or position() = 4 or ...

You don't make it clear, but I suspect you want to sort the rows (the outer for-each) not the columns. So the outer for-each becomes

<xsl:for-each select="qq:Workbook/qq:Worksheet/qq:Table/qq:Row">
  <xsl:sort select="qq:Cell[9]/qq:Data"/>

You need to be aware that there is no guarantee when you export an Excel spreadsheet as XML that the 9th Cell in a Row represents the contents of column 9 in the spreadsheet. Empty cells and cells spanning multiple columns can affect the count. But perhaps it works for your particular spreadsheet.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 12th, 2006, 04:42 AM
Registered User
 
Join Date: Apr 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,

Thankyou so much, the sort worked fantastically...
The first part to the solution concating all the position() in one did not, I think this may be as you sugessted that I am using and old version on XSL...

Charmaigne






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL 1.0 : How to find 8th position from string elayaraja.s XSLT 1 July 29th, 2008 04:26 AM
XML, XSL:FO- saving position() mikevn123 XSLT 0 August 8th, 2006 02:51 PM
combining a form input name with xsl position() ? joeprice XSLT 1 February 23rd, 2006 09:54 AM
XSL Test tclancy XSLT 2 February 3rd, 2006 02:49 PM
Using xsl:if test attributes rdavisiii XSLT 4 November 9th, 2004 12:24 AM





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