Re: [xsl] finding position() of an element in a different context

Subject: Re: [xsl] finding position() of an element in a different context
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 26 Apr 2001 09:58:49 +0100
Hi Kevin,

> I've remembered another part of the problem. Look at the structure
> again:
> <top>
>     <visits>
>           <visit id="visit1Name"/>
>           <visit id="visit2Name"/>
>     </visits>
>     <formtypes>
>         <formtype id="formtype1Name"/>
>         <formtype id="formtype2Name"/>
>     </visits>
>     <forms>
>         <form visit="visit1Name" formtype="formtype1Name"/>
>         <form visit="visit1Name" formtype="formtype2Name"/>
>         <form visit="visit2Name" formtype="formtype2Name"/>
>     </forms>
> </top>
> Am I not right in saying that if I am in the context of a <visit>
> element, doing:
>   <xsl:variable name="matchingForm"
>   select="//forms/form[(@formtypeID = $formtypeID) and (@visitID =
>   $visitID)]"/>
> will not work, because "//forms" is taken relative to the context
> node? Certainly this seems to be happening, as no matches are
> occurring.

Well, you're right that it won't work but you're wrong about why it
won't work.  If you start a path with / then it's an absolute path -
the processor starts at the very top of the node tree and travels down
from there.  So //forms gets all forms elements in the document.  In
fact you just want:


since there's only one forms element that you're actually interested
in and you know where that is.

The reason your XPath won't work is that you're looking for a
'formtypeID' attribute and a 'visitID' attribute on each form element,
whereas the attributes that are there are called 'formtype' and
'visit'.  Since it doesn't find the attributes, the predicate returns
false and you get no matches.  I think you want:

  /top/forms/form[@formtype = $formtypeID and @visit = $visitID]

You might also be interested in using a key to retrieve the relevant
form.  You could create a key that uses both the visit and the
formtype to index the form elements:

<xsl:key name="forms"
         use="concat(@formtype, ':', @visit)" />

You can then retrieve the relevant form with:

  key('forms', concat($formtypeID, ':', $visitID))

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread