Ebenezer (2)
Usenet Archive
- Joined
- Oct 21, 2008
- Messages
- 8
- Reaction score
- 0
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">
<xsl
utput 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> <xsl
therwise><xsl:value-of select="@attr" /></xsl
therwise> </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">
<xsl
<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> <xsl