sorting <xsl:for-each> versus <xsl:apply-templates>

S

Sparko

My Xml is as follows;


<root>
<st id="1">
<rt id="1">
<cr id="1" ca_w="" ca_x="" ca_y="" ca_z="" other attribs>
<ct some attribs>
<te some attribs />
<te some attribs />
<te some attribs />
<te some attribs />
</ct>
<cr id="1" ca_w="" ca_x="" ca_y="" ca_z="" other attribs>
<ct some attribs>
<te some attribs />
<te some attribs />
<te some attribs />
<te some attribs />
</ct>
</cr>
<cr id="1" ca_w="" ca_x="" ca_y="" ca_z="" other attribs>
<ct some attribs>
<te some attribs />
<te some attribs />
<te some attribs />
<te some attribs />
</ct>
</cr>
</rt>
<rt id="2">
<cr id="1" ca_a="" ca_b="" ca_c="" ca_d="" other attribs>
<ct some attribs>
<te some attribs />
<te some attribs />
<te some attribs />
<te some attribs />
</ct>
<cr id="1" ca_a="" ca_b="" ca_c="" ca_d="" other attribs>
<ct some attribs>
<te some attribs />
<te some attribs />
<te some attribs />
<te some attribs />
</ct>
</cr>
<cr id="1" ca_a="" ca_b="" ca_c="" ca_d="" other attribs>
<ct some attribs>
<te some attribs />
<te some attribs />
<te some attribs />
<te some attribs />
</ct>
</cr>
</rt>
</st>




I have written a recursive xslt template to iterate down the nodes and
produce a "tree" like output. I have used the xsl axis [child::*],
calling the template again within a for-each loop. I want to achieve
sorting on the "cr" nodes' ca_ attributes and as can be seen the
attributes change depending upon the st/rt xml sections. Therefore the
sort criteria needs to change from one section of the xml to another.
I'm not too sure this can be achieved due to the way I am processing
the document. I tried to use a sort key such as below, which didn't
work.

st[@id=1]/rt[@id=1]/cr/@ca_x

When just specifying @ca_x or @ca_y, when the xslt processor reaches
the first set of cr nodes it sorts it correctly, the only problem
being that I have no way of changing the sort criteria when it reaches
the next st/rt section. I have a feeling I should perhaps be
processing each st/rt section using the apply-templates element
therefore allowing me to specify different sort criteria for each. I
am not too sure how the apply-templates works if you want to process
the whole of a dynamic xml document, i.e if I'm not sure at processing
time how many st/rt sections there will be, the for-each seems the
only way I can process everything? I would be grateful for any advice.

Regards,

Sparko.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top