Search code examples
xmlxpathxslt-2.0xsl-variable

selecting whole set of nodes at once instead of each node recursively


I have been looking around for this and it might be that there is actually no way to do it:

My xml file looks at one point like this:

<para>Play</para>
<para>MACBETH: My dearest love, <i>Duncan</i> comes here to-night.</para>
<para>LADY MACBETH:And when goes hence?</para>
<para>MACBETH:To-morrow, as he purposes. </para>
<para>End</para>

I have found countless ways of selecting each <para> for itself, but I want to match all the <para> nodes (plus enclosed childnodes) at once and store the block in a variable. Is this even possible?

To prevent misunderstandings: If the block above were enclosed by an extra node, lets call it <exmpl> , I would simlply match the childnodes of <exmpl> and get the block of nodes i want. How do I achieve the same result without such a parent node - using sibling with a unique content('Play' and 'End') instead doesn't seem to work?


Solution

  • There are operators << and >>:

    <xsl:variable name="start" select="//para[. = 'Play']"/>
    <xsl:variable name="end" select="//para[. = 'End']"/>
    <xsl:variable name="block" select="$start, $start//following-sibling::para[. &lt;&lt; $end], $end"/>
    

    Grouping might also help