[xsl] sort & modulo

T

Tjerk Wolterink

I posted my problem earlier, but i simplified the examples,
and i know what the cause of the problem is, but i dont know the solution,

my xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="datastyle.xsl"?>
<data xmlns="http://tjerk.com">
<item>a1</item>
<item>b2</item>
<item>c3</item>
<item>d4</item>
<item>e5</item>
<item>f6</item>
<item>a0</item>
</data>


If i would sort the text items elements i would place the
last item a0 at the beginning. But i am also
using the modulo operator to create a table like structure:

The xsl file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:tjerk="http://tjerk.com"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="xml" indent="yes"/>
<xsl:param name="cols" select="3"/>

<xsl:template match="/tjerk:data">
<data>
<xsl:for-each select="./tjerk:item[(position() mod $cols)=1]">
<xsl:sort data-type="text" select="tjerk:item" order="descending" />
<tr>
<xsl:apply-templates select="."/>
<xsl:apply-templates select="following-sibling::tjerk:item[position() &lt;
$cols]"/>
</tr>
</xsl:for-each>
</data>
</xsl:template>

<xsl:template match="tjerk:item">
<td><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>

---



The output is:
-
<data>
<tr><td>a0</td></tr>
<tr><td>a1</td><td>b0</td><td>c3</td></tr>
<tr><td>d4</td><td>e5</td><td>f6</td></tr>
</data>
-

But i want the output to be like this:
-
<data>
<tr><td>a0</td><td>a1</td><td>b0</td></tr>
<tr><td>c3</td><td>d4</td><td>e5</td></tr>
<tr><td>f6</td></tr>
</data>
-

The problem is the sort element in the foreach loop,

how can i solve this???
Help is much appreciated.
 
D

Dimitre Novatchev

Use two pass transformation -- the first to sort and the second to group.

Cheers,
Dimitre Novatchev
 
T

Tjerk Wolterink

Dimitre said:
Use two pass transformation -- the first to sort and the second to group.

Cheers,
Dimitre Novatchev


Ok but can i do that with just one invocation of the xsltprocessor??
I do not have acces to the xslt processor, and it just calls the xml with xsl
transformation one time, so is there no other solution??
Or can i do a twopass transformmation in one xsltransformation??


I posted my problem earlier, but i simplified the examples,
and i know what the cause of the problem is, but i dont know the solution,

my xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="datastyle.xsl"?>
<data xmlns="http://tjerk.com">
<item>a1</item>
<item>b2</item>
<item>c3</item>
<item>d4</item>
<item>e5</item>
<item>f6</item>
<item>a0</item>
</data>


If i would sort the text items elements i would place the
last item a0 at the beginning. But i am also
using the modulo operator to create a table like structure:

The xsl file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:tjerk="http://tjerk.com"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="xml" indent="yes"/>
<xsl:param name="cols" select="3"/>

<xsl:template match="/tjerk:data">
<data>
<xsl:for-each select="./tjerk:item[(position() mod $cols)=1]">
<xsl:sort data-type="text" select="tjerk:item" order="descending" />
<tr>
<xsl:apply-templates select="."/>
<xsl:apply-templates select="following-sibling::tjerk:item[position() &lt;
$cols]"/>
</tr>
</xsl:for-each>
</data>
</xsl:template>

<xsl:template match="tjerk:item">
<td><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>

---



The output is:
-
<data>
<tr><td>a0</td></tr>
<tr><td>a1</td><td>b0</td><td>c3</td></tr>
<tr><td>d4</td><td>e5</td><td>f6</td></tr>
</data>
-

But i want the output to be like this:
-
<data>
<tr><td>a0</td><td>a1</td><td>b0</td></tr>
<tr><td>c3</td><td>d4</td><td>e5</td></tr>
<tr><td>f6</td></tr>
</data>
-

The problem is the sort element in the foreach loop,

how can i solve this???
Help is much appreciated.
 
D

Dimitre Novatchev

Tjerk Wolterink said:
Ok but can i do that with just one invocation of the xsltprocessor??
Yes.

I do not have acces to the xslt processor, and it just calls the xml with
xsl transformation one time, so is there no other solution??
Or can i do a twopass transformmation in one xsltransformation??

Yes.

The result of the first transformation is produced within the content (body)
of an xsl:variable.

Then this RTF is converted to an intermediary tree using the xxx:node-set()
extension function (available with almost every XSLT processor).

Then the second transformation is applied on this intermediary tree.

See for example:

"A Generic template for multi-pass processing (Was: Re: Applying two
transformations consecutively)"

http://www.biglist.com/lists/xsl-list/archives/200106/msg01152.html


Cheers,
Dimitre Novatchev.
 

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,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top