Chris Jeffries
Chris Jeffries

Reputation: 137

XSLT: Apply the same template to nodes with different names

I want to apply the same xsl:template to different nodes of an xml tree, preferably selecting by an attribute of the node. Here's an example of the XML tree:

<journal>
    <claimantmonth>
        <By format="lookup">Bank dr/cr</By>
        <Month format="inputRO">4</Month>
        <Year format="inputRO">2012</Year>
        <claimslist>
            <claim>
                <id format="hidden">867</id>
                <enteredDate format="dateRO">2012-06-11</enteredDate>
                <enteredBy format="lookup">chris</enteredBy>
                <expenseDate format="date">2012-04-02</expenseDate>
                <description format="text">Electric Co</description>
            </claim>
       </claimslist>
    </claimantmonth>
   </journal>

I would want to use the same template for enteredDate and expenseDate, but a different one for description etc.

I am very new to XSLT and I have searched in vain for an answer, so I just need a pointer to where to look.

I found lots of examples of how to apply a variety of different templates to the SAME node. but that is not my problem.

Upvotes: 0

Views: 416

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

You can match several elements like this:

<xsl:template match="enteredDate|expenseDate">

and you can match by the value of an attribute like this:

<xsl:template match="*[@class='sombre']">

I am very new to XSLT and I have searched in vain for an answer

I think the fact that you are asking these questions means that your strategy for learning the language isn't ideal. I think you're trying to learn by googling for examples. That really doesn't work well - you'll find sites written by experts for experts, sites written by fools for fools, and every other combination; you will very rarely come across a site that takes you by the hand and teaches you the concepts in a structured, well-thought-out and peer-reviewed way. And until you know the concepts, you don't know what terms to use in a search. How could you have known that you need to google for "union pattern"? Get yourself a good book instead.

Upvotes: 1

Related Questions