Discussion:
How to sort a $variable and store that into another $variable ?
Jean-Philippe Martin
2008-12-10 18:15:04 UTC
Permalink
Hi,
I'm trying to sort a <xsl:variable> containing 22 nodes and store this
sorted list into another xsl:variable.

Is this possible ?

Here's the best I could do :

<xsl:variable name="sortedList">
<xsl:call-template name="sortThis">
<xsl:with-param name="unsortedList" select="$unsortedList"/>
</xsl:call-template>
</xsl:variable>

<xsl:template name="sortThis">
<xsl:param name="unsortedList"/>

<xsl:for-each select="$unsortedList">
<xsl:sort select="."/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>

I have also tried it this way but with the same result :

<xsl:variable name="sortedList">
<xsl:apply-templates mode="sort" select="$unsortedList">
<xsl:sort select="."/>
</xsl:apply-templates>
</xsl:variable>

<xsl:template match="node()" mode="sort">
<xsl:copy-of select="@*"/>
</xsl:template>


What I get is a single node containing all the values sorted
concatenated in a single string.

Is there a way to do this ?

Ps: this is xsl 1.0.

Thank you very much.

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Martin Honnen
2008-12-10 18:30:55 UTC
Permalink
Post by Jean-Philippe Martin
I'm trying to sort a <xsl:variable> containing 22 nodes and store this
sorted list into another xsl:variable.
Is this possible ?
Is there a way to do this ?
Ps: this is xsl 1.0.
There is a way if you show us your XML input and how exactly you
initialize your first variable.

Assuming the XML input document is

<root>
<foo>10</foo>
<foo>2</foo>
<foo>1</foo>
<foo>4</foo>
</root>

then this stylesheet

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:template match="/">
<xsl:variable name="v1" select="root/foo"/>
<xsl:variable name="v2">
<xsl:for-each select="$v1">
<xsl:sort select="." data-type="number"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<result>
<xsl:copy-of select="$v2"/>
</result>
</xsl:template>

</xsl:stylesheet>

outputs

<result>
<foo>1</foo>
<foo>2</foo>
<foo>4</foo>
<foo>10</foo>
</result>

Variable v1 has the foo elements from the XML input, variable v2 a
result tree fragment of the sorted foo elements.
--
Martin Honnen
http://JavaScript.FAQTs.com/

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Jean-Philippe Martin
2008-12-10 19:05:24 UTC
Permalink
Ok.
I'll try to make this understandable.

My need is :
- extract a list of values
- make it unique
- make it sorted
- put it on many columns in a html table

My XML document is big. I create an unordered list of unique values
with this call (the first // is important since the info can be
anywhere in the xml tree) :

<xsl:variable name="unsortedList"
select="//category[@defid=3183315]//*[@attid=49]/node()[not(.=following::*)]"
/>


I put this list in a variable because i want to make a multi-column
html table with the values. And the technique I used needs a variable

<table style="width:100%; ">
<xsl:variable name="unsortedList"
select="//category[@defid=3183315]//*[@attid=49]/node()[not(.=following::*)]"
/>
<xsl:variable name="nbCol" select="ceiling(count($unsortedList) div 5)"/>

<xsl:for-each select="($unsortedList)[position() &lt;= $nbCol]">
<!--xsl:sort select="."/-->
<xsl:variable name="here" select="position()"/>
<tr>
<xsl:call-template name="TDcompetence">
<xsl:with-param name="competence"
select="$unsortedList[$here+$nbCol*0]"/>
</xsl:call-template>

<xsl:call-template name="TDcompetence">
<xsl:with-param name="competence"
select="$unsortedList[$here+$nbCol*1]"/>
</xsl:call-template>

<xsl:call-template name="TDcompetence">
<xsl:with-param name="competence"
select="$unsortedList[$here+$nbCol*2]"/>
</xsl:call-template>

<xsl:call-template name="TDcompetence">
<xsl:with-param name="competence"
select="$unsortedList[$here+$nbCol*3]"/>
</xsl:call-template>

<xsl:call-template name="TDcompetence">
<xsl:with-param name="competence"
select="$unsortedList[$here+$nbCol*4]"/>
</xsl:call-template>
</tr>
</xsl:for-each>
</table>


The sort is in comments because it do not work since I access the
value with a position into the $unsortedList variable directly and not
through the for-each which could sort.

Thank you.
Post by Jean-Philippe Martin
I'm trying to sort a <xsl:variable> containing 22 nodes and store this
sorted list into another xsl:variable.
Is this possible ?
Is there a way to do this ?
Ps: this is xsl 1.0.
There is a way if you show us your XML input and how exactly you initialize
your first variable.
Assuming the XML input document is
<root>
<foo>10</foo>
<foo>2</foo>
<foo>1</foo>
<foo>4</foo>
</root>
then this stylesheet
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:variable name="v1" select="root/foo"/>
<xsl:variable name="v2">
<xsl:for-each select="$v1">
<xsl:sort select="." data-type="number"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<result>
<xsl:copy-of select="$v2"/>
</result>
</xsl:template>
</xsl:stylesheet>
outputs
<result>
<foo>1</foo>
<foo>2</foo>
<foo>4</foo>
<foo>10</foo>
</result>
Variable v1 has the foo elements from the XML input, variable v2 a result
tree fragment of the sorted foo elements.
--
Martin Honnen
http://JavaScript.FAQTs.com/
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
--~--
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Martin Honnen
2008-12-11 16:16:37 UTC
Permalink
Post by Jean-Philippe Martin
- extract a list of values
- make it unique
- make it sorted
- put it on many columns in a html table
My XML document is big. I create an unordered list of unique values
with this call (the first // is important since the info can be
<xsl:variable name="unsortedList"
/>
Your first post said you had troubles to sort nodes into a variable, the
problem being "I get is a single node containing all the values sorted
concatenated in a single string". Can you show us the code that causes
that problem?
What you did show in your second post did not even attempt to sort into
a variable and output that sorted variable.

The only problem I see above is that the variable unsortedList could
contain all kind of nodes (e.g. element nodes, text nodes, comment
nodes) and I am not sure sorting such an incoherent node-set makes much
sense.

And as I pointed out in my initial post, if you sort a node-set into a
variable in XSLT 1.0 then you have a result tree fragment. If you want
to apply XPath on a result tree fragment then you first need to apply an
extension function like exsl:node-set to convert the result tree
fragment into a node-set. So that could also be a problem, but you have
not shown us your attempt to sort into a variable and to use that
variable later so I am just guessing.
--
Martin Honnen
http://JavaScript.FAQTs.com/

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-***@lists.mulberrytech.com>
--~--
Continue reading on narkive:
Loading...