RE: [xsl] The notion of Inheritance?

Subject: RE: [xsl] The notion of Inheritance?
From: William Bagby <williamb@xxxxxxxxx>
Date: Thu, 29 Mar 2001 12:45:33 -0500
Jeni,

I was a bit hasty with my reply.  Your solution pretty much does what I want
to do, except I need to change 

person[id = 2 or id = 3]

to 

person[id = 2]

I also cannot figure out how to prevent the rest of the data from passing
through.  Normally I would use something like:

<xsl:template match="*|@*|text()">
<!-- do nothing! -->
</xsl:template>

but that doesn't work here, as you've already matched *[*] and *[not(*)],
which seems to cover pretty much everything, although I could be (and
probably am!) mistaken.

Thanks for all your help!

William.

-----Original Message-----
From: Jeni Tennison [mailto:mail@xxxxxxxxxxxxxxxx]
Sent: Thursday, March 29, 2001 4:34 AM
To: William Bagby
Cc: XSL Mailing List (E-mail)
Subject: Re: [xsl] The notion of Inheritance?


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


Current Thread