E
Ebenezer
Hello!
Let's suppose we have an XML with some nested NODE nodes:
<root attr="first">
<node id="1" attr="mike">
<node id="2" />
<node id="3" attr="dave" />
</node>
<node id="4">
<node id="5" attr="peter" />
</node>
</root>
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">
<xslutput method="html" />
<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>
<xsltherwise><xsl:value-of select="@attr" /></xsltherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Let's suppose we have an XML with some nested NODE nodes:
<root attr="first">
<node id="1" attr="mike">
<node id="2" />
<node id="3" attr="dave" />
</node>
<node id="4">
<node id="5" attr="peter" />
</node>
</root>
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">
<xslutput method="html" />
<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>
<xsltherwise><xsl:value-of select="@attr" /></xsltherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>