This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: The notion of Inheritance?
- To: William Bagby <williamb at adone dot com>
- Subject: Re: [xsl] The notion of Inheritance?
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Thu, 29 Mar 2001 10:34:16 +0100
- CC: "XSL Mailing List (E-mail)" <xsl-list at lists dot mulberrytech dot com>
- Organization: Jeni Tennison Consulting Ltd
- References: <F991D4265D6AD4119A1900508BC98E572FD5DE@NTEXCL01>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi William,
> Suppose I want to match id=2 and id=3 (respectively) and transform
> them to separate XMLs, using id=1 as the default:
OK, so you're matching person[id = 2 or id = 3] with a template:
<xsl:template match="person[id = 2 or id = 3]">
...
</xsl:template>
>From within this template, you can always get at person id=1 with one
of the paths:
../person[id = 1] (id child equals '1')
../person[1] (first person in the list)
To do the defaulting, it's probably easiest to go through the elements
within the default person and copy them unless there's an equivalent
element in the person that you're considering. Since the elements are
nested, you should probably use a recursive solution where you pass
the overriding information down as you work through the person.
So, from within your template, apply templates to the person with id =
1, passing the person that you're looking at as a parameter. I'll use
an 'inherit' mode to avoid clashes between the templates:
<xsl:template match="person[id = 2 or id = 3]">
<xsl:apply-templates select="../person[id = 1]" mode="inherit">
<xsl:with-param name="override" select="." />
</xsl:apply-templates>
</xsl:template>
Then, you want to have a template that matches elements with element
content (like person and physical). They copy themselves and then
apply templates to their child elements, passing the relevant child of
the $override node down to the next level:
<xsl:template match="*[*]" mode="inherit">
<xsl:param name="override" />
<xsl:copy>
<xsl:for-each select="*">
<xsl:apply-templates select="." mode="inherit">
<xsl:with-param name="override"
select="$override/*[name() = name(current())]" />
</xsl:apply-templates>
</xsl:for-each>
</xsl:copy>
</xsl:template>
When you get to textual elements, you want to see if the $override
element exists - if it does, it should be copied, otherwise the
current node should be copied:
<xsl:template match="*[not(*)]" mode="inherit">
<xsl:param name="override" />
<xsl:copy-of select="$override | current()[not($override)]" />
</xsl:template>
I hope that helps,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list