scope of xsl:sort

R

R

Hello everybody.

I was sorting all my XML data with for-each and sort.
But there were few cases that I didn't want to sort my data.

so I added nosort attribute - if given and set to '1' don't sort.

I wrote this:

<xsl:for-each select="field">
<xsl:if test="not(../data/@nosort='1')">
<xsl:sort select="description"/>
</xsl:if>
<!-- the rest of the template here -->
</xsl:for-each>

but this XSLT returns me an error:

Warning: xsl:sort : improper use this should not be reached in
/home/xh/workspace/ERS_new/engine/classes/XML.inc.php on line 138
(paresed with PHP5 DOM)

how can I make my nosort attribute work?

thanks for any help

best regards
R
 
M

Martin Honnen

R wrote:

I was sorting all my XML data with for-each and sort.
But there were few cases that I didn't want to sort my data.

so I added nosort attribute - if given and set to '1' don't sort.

I wrote this:

<xsl:for-each select="field">
<xsl:if test="not(../data/@nosort='1')">
<xsl:sort select="description"/>
</xsl:if>
<!-- the rest of the template here -->
</xsl:for-each>

but this XSLT returns me an error:

Warning: xsl:sort : improper use this should not be reached in
/home/xh/workspace/ERS_new/engine/classes/XML.inc.php on line 138
(paresed with PHP5 DOM)

xsl:sort needs to be a child of xsl:for-each or xsl:apply-templates.
 
D

David Carlisle

Your code imples that data is a sibling of field not a parent,
is that right?

<xsl:for-each select="field">
<xsl:sort select="description[not(../data/@nosort='1']"/>

would work although it's probably more efficient to move the test
outside the xsl:choose, as that allows the processor to know that it
doesn't need to sort anything in the unsorted case.

<xsl:choose>
<xsl:when test="data/@nosort='1'">
<xsl:for-each select="field">
<xsl:call-template name="x"/>
</xsl:for-each>
<xsl:eek:therwise>
<xsl:for-each select="field">
<xsl:sort select="description"/>
<xsl:call-template name="x"/>
</xsl:for-each>
</xsl:eek:therwise>
</xsl:choose>

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top