Subject: Re: [xsl] parent node/ child node From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx> Date: Sat, 23 Dec 2000 20:19:34 +0000 |
James, > I'm trying to replace a missing attribute in a node with the a > default that is stored in its' parent node. To put this in terms that make it easier to find the XSLT solution, you want to create a copy of your XML source, but when a particular attribute is missing on an element in the XML source, you want to give the value of an attribute of its parent instead. In your example, the 'parent' elements are always copied as they are, so you can use a basic copying template for them: <xsl:template match="parent"> <!-- create a copy of the element --> <xsl:copy> <!-- create a copy of its attributes --> <xsl:copy-of select="@*" /> <!-- apply templates to its children --> <xsl:apply-templates /> </xsl:copy> </xsl:template> When you have a 'child' element, on the other hand, you want to copy it, and copy its attributes, and then, if the 'attribute' attribute is missing, add a copy of its parent's 'defaultAttribute' attribute in its place. Testing whether the 'attribute' attribute exists can be done with the XPath: not(attribute::attribute) or, shorter: not(@attribute) The parent element's 'defaultAttribute' attribute can be located with the XPath: parent::parent/attribute::defaultAttribute or, shorter: ../@defaultAttribute So, the template you're after is: <xsl:template match="child"> <!-- create a copy of the element --> <xsl:copy> <!-- create a copy of its attributes --> <xsl:copy-of select="@*" /> <!-- if the 'attribute' attribute isn't present --> <xsl:if test="not(@attribute)"> <!-- copy the parent's 'defaultAttribute' attribute --> <xsl:copy-of select="../@defaultAttribute" /> </xsl:if> </xsl:copy> </xsl:template> Of course you don't have to copy the parent's 'defaultAttribute': you can change its name, or its value, if you create it using xsl:attribute instead. The following does exactly the same as the xsl:copy-of above: <xsl:attribute name="defaultAttribute"> <xsl:value-of select="../@defaultAttribute" /> </xsl:attribute> I hope that helps, Jeni --- Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] parent node/ child node, James Piva | Thread | [xsl] Text Following <br/>, Edierley Messias |
Re: [xsl] Not able to work further , Jeni Tennison | Date | Re: [xsl] strangeness with <xsl:var, Jeni Tennison |
Month |