Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Getting parent's attributes recursively

From: Ebenezer <vaciapairat@----.--->
To: NULL
Date: 10/21/2008 8:59:00 AM
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:output 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:otherwise><xsl:value-of select="@attr" /></xsl:otherwise>
		  </xsl:choose>
     </xsl:template>
</xsl:stylesheet>


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent