Ebenezer wrote:
> What I want to achieve is to have an XSL that:
> - takes a $id variable externally from a PHP script
> - finds the node with @id=$id
> - outputs the value "@attr" attribute IF PRESENT, otherwise the parent's
> "@attr" attribute IF PRESENT, otherwise the grandparent's... recursively
> traversing from parent to parent, until an @attr value is found
>
> This won't work for some nodes:
>
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0">
> <xsl
utput method="html" />
You need to define the id parameter
<xsl

aram name="id"/>
> <xsl:template match="root">
> <xsl:apply-templates select="//node[@id=$id]" />
> </xsl:template>
> <xsl:template match="node">
> <xsl:choose>
> <xsl:when test="not(@attr)"><xsl:apply-templates ".."
> /></xsl:when>
That is not the correct syntax, you need
<xsl:apply-templates select=".."/>
Also it is not clear what you want to do if you do not find a 'node'
ancestor element with an 'attr' attribute.
If you walk up to the 'root element then the template matching that will
lead to infinite recursion. So perhaps you should better use
<xsl:apply-templates select="parent::node"/>
Or if you want to walk up to the 'root' element as well you need a mode e.g.
<xsl:apply-templates select=".." mode="attribute-check"/>
and then you need to add that mode parameter to the template e.g.
<xsl:template match="root">
<xsl:apply-templates select="//node[@id=$id]"
mode="attribute-check" />
</xsl:template>
<xsl:template match="node | root" mode="attribute-check">
<xsl:choose>
<xsl:when test="not(@attr)"><xsl:apply-templates
select="parent::*" /></xsl:when>
<xsl

therwise><xsl:value-of select="@attr" /></xsl

therwise>
</xsl:choose>
</xsl:template>
--
Martin Honnen
http://JavaScript.FAQTs.com/