By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,844 Members | 1,895 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,844 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Jul 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.