Re: [xsl] following nodes until "stop-node" reached (content between two nodes)

Subject: Re: [xsl] following nodes until "stop-node" reached (content between two nodes)
From: "jakob Beetz" <mailinglists.jakob@xxxxxxxxx>
Date: Wed, 9 Apr 2008 15:57:18 +0200
Gentlemen,

thank you all for your valuable and quick suggestions, they were
really helpful for understanding the whole thing.

On Tue, Apr 8, 2008 at 3:05 PM, Michael Kay <mike@xxxxxxxxxxxx> wrote:
>
>  Assuming you know there is only one "start" node and one "stop" node:
>

unfortunatly that is not the case (trying to scrape information from
hundreds of html pages), I missed to mention this in my original
problem statement. This meant for me that I could not tackle the
problem with any of the XPath and grouping solutions that were
suggested.

>  There are lots of other solutions. The most elegant use recursion, but
>  beginners tend to find that difficult.

That was the key to the solution.
Below is my quick'n'dirty recursive approach to it (works fine for my
real-world problem)

A big "Thank you" to all of you again,
Cheers
Jakob

<xsl:template match="/">
	<xsl:apply-templates select="//somenode[@value='start']"/>
</xsl:template>

<xsl:template match="//somenode[@value='start']">

	<xsl:call-template name="copy-until-stop">
		<xsl:with-param name="current_node"
select="self::node()/following-sibling::*[1]"/>
		<xsl:with-param name="count" select="0"/>
	</xsl:call-template>
</xsl:template>

<xsl:template name="copy-until-stop">
	<xsl:param name="current_node" />
        <xsl:param name="count" />

	<!--test if stop tag is reached. To avoid endless recursion break out
after at most 5000 nodes-->
	<xsl:if test="not($current_node[@value='stop']) and $count &lt; 5000" >
		<xsl:copy-of select= "$current_node"/>
		<xsl:call-template name="copy-until-stop">
			<xsl:with-param name="current_node"
select="$current_node/following-sibling::*[1]"/>
			<xsl:with-param name="count" select="$count+1"/>
		</xsl:call-template>
    </xsl:if>
	
</xsl:template>




>  >
>  > <somenode value="stop">something else</somenode> [...]
>  >
>  >
>  > how would I select anything (but not including) that is
>  > between "somenode" with the attribute "start" and "stop"?
>  >
>  > This is my beginers approach:
>  > <xsl:template match="//somenode[@value='start']">
>  >     <xsl:element name="entity">
>  >         <xsl:value-of select="."/>
>  >     </xsl:element>
>  >     <xsl:for-each select="following-sibling::*">
>  >         <td><xsl:value-of select="."/></td>
>  >      </xsl:for-each>
>  > </xsl:template>
>  >
>  > how do check wether the current node in the for-eachnode is
>  > the "somenode"-node with  "stop" as attribute value? Can I
>  > "break out" of for each or somehow if-then the inclusion of
>  > anything that comes _after_ that?
>  >
>  > As you probably see I do not have a really good undstanding
>  > of XSL/XPATH. Browsing the docs and XSL FAQ did not make me
>  > wiser either.
>  >
>  > Any help is greatly appreciated
>  > Thanks in advance and
>  > Cheers
>  > Jakob

Current Thread