xsl-list
[Top] [All Lists]

Re: [xsl] Help moving away from named templates

2007-05-02 09:56:51
Steve wrote:
Thank you David and Abel for your patience and thank you for
formulating (for me!) what part of my question actually is.

You're right. There are structural differences between the customer
XML and the customer's contacts XML. I apologize for being imprecise.
Let's give a simplistic version of the two:

Customer.xml
<Records>
  <Record>
     <id>6</id>
     <firstName>Steve</firstName>
  </Record>
</Records>

eContacts.xml
<Records>
 <Record>
    <custID>6</custID>
     <firstName>Mommy</firstName>
     <relationship>Mother</relationship>
  </Record>
</Records>

That'd be, on the selecting part:
<xsl:apply-templates select="Record[id]" />

and
<xsl:apply-templates select="Record[custID]" />

respectively.

Or, on the matching part, if you do <xsl:apply-templates select="*" /> it becomes:

<xsl:template match="Record[id]" >....

or
<xsl:template match="Record[custID]" >....

respectively.

If you want to combine certain matches, you can do so with the union symbol |, which gives you a way of displaying several records that are specified differently, but should be treated equally, for instance:

<xsl:template match="Record[custID | custid | CustID]" >....


This is precisely where XSLT shines: defining a couple of templates where *you* define what the specification is of your match, and, in the case of a match, *you* define what nodes should be created in the output tree. Having several templates like this is basically what just about any stylesheet will look like. And it doesn't matter when templates are not matching, because they'll be simply skipped.

Also, note that XSLT has a way of defining templates get precedence. That means, that if you have one Record with both a custID and and id element in it, they are indistinguishable, and the processor will notify you about it (but will still run and output normally). By specifying a less special rule, you can have a "catch-all" template, which will help you find Record elements that previously were not matched:

<xsl:template match="Record" >
 <error>Unmatched record found!</error>
<xsl:template>

something like that...





Obviously, distinguishing between these two in XPath is not really
difficult.

indeed, see above

But let's say there are 10 or 15 other pages about
Customers which need to be displayed, each with some nuanced
difference.

I don't see why this is a problem, though you may ask the one that creates such crippled XML to give you something more suitable, or process it in a preprocess phase to make it more suitable.

I suppose I feared ending up with a tangled mess of
templates which are potentially trying to chomp each other's input due
to similarities.

No, you shouldn't...



Cheers,
-- Abel


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--