xsl-list
[Top] [All Lists]

RE: problem with variable and position()

2003-08-26 23:58:17
Hi,

<xsl:template match="body">
                    <xsl:element name="description">
                       <xsl:apply-templates 
select="text()[starts-with(normalize-space
(),'Description')]" mode="description"/>
                    </xsl:element>
</xsl:template>
<xsl:template 
match="text()[starts-with(normalize-space(),'Description')]" 
mode="description">
                         <xsl:variable name="descPos" 
select="position()"/>
                         <xsl:apply-templates 
select="text()[position()>$descPos]"/>
                                           
</xsl:template>

This template could as well be simply

  <xsl:template match="text()" mode="description">
    <xsl:variable name="descPos" select="position()"/>
    <xsl:apply-templates select="text()[position()>$descPos]"/>
  </xsl:template>

unless you have multiple different templates that match text nodes in 
"description" mode.

<xsl:template match="text()[position()>$descPos]">
                              <xsl:value-of select="."/>
</xsl:template>  

And this should be

  <xsl:template match="text()">
    <xsl:value-of select="."/>
  </xsl:template>

or, you don't even need this, because the build-in template rule will take care 
of it
  
the input documents. I tried to store the position of the 
description text in the 
descPos variable to use it later, but the above stylesheet 
gives me an error of "Invalid 
variable reference". How could I re-write the last template 
so I avoid this error? 

For one, the descPos variable will be out of scope in the other template -- see 
the XSLT spec for variable scope. Also, variable references are not allowed in 
match patterns.

Cheers,

Jarno

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list