RE: [xsl] Xpath for a range of elements

Subject: RE: [xsl] Xpath for a range of elements
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 7 Feb 2007 23:37:22 -0000
In this expression

<xsl:apply-templates select="*[. &lt;&lt; procedure[1]]"/> 

you've forgotten that the context changes inside the predicate. Assign
procedure[1] to a variable first, and it should be OK.

Another way of doing this, which also works in 1.0, is:

<prereq>
  <xsl:apply-templates select="procedure/preceding-sibling::*"/>
</prereq>
<xsl:apply-templates select="./procedure"/>
<postreq>
  <xsl:apply-templates select="procedure/following-sibling::*"/>
</postreq>

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: Anderson, Paul [mailto:Paul.Anderson@xxxxxxxxxxxxx] 
> Sent: 07 February 2007 22:53
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Xpath for a range of elements
> 
> Greetings,
> 
> Consider the following skeleton for an XML source file:
> 
> <section>
>    <title>...</title>
>    <para>...</para>
>    <code-block>...</code-block>
>    <para>...</para>
>    <procedure>...</procedure>
>    <para>...</para>
>    <screen>...</screen>
>    <para>...</para>
> </section>
> 
> I need a Xpath expression that will return all nodes after 
> the <title> and before the <procedure> (i.e., from 
> section/para[1] through section/para[2]). Then I operate on 
> the <procedure>. Then I need another Xpath expression that 
> returns all the nodes after the procedure up to the end of 
> the <section> (i.e., from section/para[3] through 
> section/para[4]. The source documents vary enough that I do 
> not know in advance the number of elements or nature of the 
> elements before or after the <procedure>. I do, however, know 
> that there is always one (and only
> one) <procedure>.
> 
> In my research, I came across the following thread on this 
> list 
> (http://www.biglist.com/lists/xsl-list/archives/200608/msg00043.html).
> This thread appeared to hold the answer for me but I can't 
> make it work.
> I saw that the solution was specific to Xpath 2.0 so I made 
> sure to upgrade to Saxon 8 and to update my scripts so that 
> they would work (e.g., migrate <xsl:document> to 
> <xsl:result-document>, etc.). 
> 
> Here is an approximation of the template that matches the 
> section in the example above:
> 
> <xsl:template match="section">
> 	<my-proc id="{@id}">
> 		<xsl:element name="proc-title">
> 			<xsl:value-of select="./title"/>
> 		</xsl:element>
> 		<xsl:element name="procbody">
> 			<xsl:element name="prereq">
> 				<xsl:apply-templates select="*[.
> &lt;&lt; procedure[1]]"/>
> 			</xsl:element>
> 			<xsl:apply-templates select="./procedure"/>
> 			<xsl:element name="postreq">
> 				<xsl:apply-templates select="*[.
> &gt;&gt; procedure[1]]"/>
> 			</xsl:element>
> 		</xsl:element>
> 	</my-proc>
> </xsl:template>
> 
> I must be making a simple mistake. Any assistance is appreciated.
> 
> Best regards,
> 
> Paul Anderson
> 
> 
> 
> The contents of this e-mail are intended for the named 
> addressee only. It contains information that may be 
> confidential. Unless you are the named addressee or an 
> authorized designee, you may not copy or use it, or disclose 
> it to anyone else. If you received it in error please notify 
> us immediately and then destroy it. 

Current Thread