[xsl] is there a more concise way of pulling diverse elements into a for-each?

Subject: [xsl] is there a more concise way of pulling diverse elements into a for-each?
From: "Trevor Nicholls trevor@xxxxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 27 Mar 2024 06:47:11 -0000
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">

   .

   </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 | .">

   .

   </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

Current Thread