[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: is there a more concise way of pulling diverse el

Subject: Re: is there a more concise way of pulling diverse elements into a for-each?
From: "Michael Kay michaelkay90@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 27 Mar 2024 08:05:44 -0000
Re:  is there a more concise way of pulling diverse  el
Template rules are your friend: replace the call on xsl:for-each with a call
on xsl:apply-templates, perhaps with a named mode, and then write template
rules for matching the indvidual elements. You might use one rule for each
element:

<xsl:template match="document">...</xsl:template>

or one rule that matches several elements:

<xsl:template match="document|section">....

or you might make it the default rules

<xsl:template match="*">....

with specific rules for the elements that you DON'T want to match.

For the `included` element, write a rule that recurses:

<xsl:template match="included">
   <xsl:apply-templates select="document | section | heading"/>
</xsl:template>

Michael Kay
Saxonica

> On 27 Mar 2024, at 06:47, Trevor Nicholls trevor@xxxxxxxxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Hi
>
> Application is using XSL 2.0
>
> Apologies if this is a standard pattern or a FAQ
>
> I have this issue in several places but the most obvious is where I am
generating a TOC for a large document, and I need to extend it to pull in a
few additional elements
>
> The stylesheet started with a template which uses this kind of
construction:
>
>    <xsl:for-each select="document | included/document | section |
included/section | heading | included/heading">
>    b&
>    </xsl:for-each>
>
> This is OK but it doesn't scale well, and now I need to add half a dozen
additional elements to the selection, e.g.
>
>    <xsl:for-each select="document | included/document | section |
included/section | heading | included/heading | function | included/function |
switch | included/switch | b&">
>    b&
>    </xsl:for-each>
>
> The pattern is rapidly getting out of hand (aka error-prone) and I wonder if
there is a better way of handling this construction, without requiring a
schema change.
>
> As it happens all the elements listed above have an id attribute, so I
tried
>
>    <xsl:for-each select="*[@id] | included/*[@id]">
>
> but that selects far too much; there are numerous elements with ids which I
do not want to select, and a list of items to exclude would be as long or
longer than the list which I need to include.
>
> XSL is so elegant there must be a better way, but I haven't been able to
come up with it on my own. Can someone point me in the right direction
please?
>
> cheers
> T
>
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/3500899> (by
email <>)

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.