Re: [xsl] Finding the position of an ancestor

Subject: Re: [xsl] Finding the position of an ancestor
From: Kamal Bhatt <kbhatt@xxxxxxxxx>
Date: Tue, 20 Dec 2005 15:22:18 +1100
Not too sure what you mean. As I understand it, if you have a template match for Step, if will give you position of Step in relation to node set you selected. If you select all Step values anywhere //Step
you will probably get the position of the Step values as they appear in the nodeset (I think). If someone could confirm this for me, it would be very helpful.

So, to answer your question, I think it depends on how you call the template match for Step.

Nicholas Orr wrote:

Thanks Kamal,

Yes, that was something I thought of, but hadn't tried yet. The only issue is that if there are multiple <script> elements, does the position reset each time so that it starts again at 1 for each new group of <Step> elements?


On 20/12/2005, at 1:53 PM, Kamal Bhatt wrote:

One brute force method would be to have a template that matches step and then in this template call another template that matches for chunk and pass down the value of the position of step to this template.


<xsl:template match="Step">
   <xsl:apply-templates select="descendant::Chunk[@type='FieldRef']">
        <xsl:with-param name="pos" select="position()"/>


<xsl:emplate match="Chunk">
   <xsl:param name="pos"/>
   <!-- Field processing goes here -->

Apologies if syntax is incorrect it has been a while since I have done XSLT and I have never used descendant, but hopefully you get at what I am saying. I have done something similar in the past and this was the only way I could do it. If anyone knows of a better way, I would be grateful to find out.

Maybe you can find a way of making this more elegant by going back to the step rather than forward to the chunk and getting the position that way (ie, create a template that gets and prints the position of a step)

Nicholas Orr wrote:

I was wondering if someone can help me work out a position issue I'm struggling with.

I've got some XML that has is in it :

<Step id="1" name="Perform Script" enable="True">
<Chunk type="FieldRef">
<Field id="32867" name="cPref Server Temp Folder" table="gPrefs" />

Then in the xslt I run a for each loop

<xsl:for-each select="/FMPReport/File/ descendant::Chunk[@type='FieldRef']/Field">
<xsl:call-template name="RESULTSET" />

which works fine, and I can get the data I need. I grab the name from the Step using :

<xsl:value-of select="ancestor::Step[1]/@name" />

But how do I grab the step number? For example I want to know that this is step 4 in this script, by counting how many <Step> elements there are in the current <Script> element. I thought it would just be :

<xsl:value-of select="ancestor::Step[1]/ position ()"/>

But that doesn't work, and gives me a parsing error. Am I maybe going about the whole thing wrong?


Current Thread