Re: the "~" operator (was Re: [xsl] Add id to next element)

Subject: Re: the "~" operator (was Re: [xsl] Add id to next element)
From: "John Lumley john@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 23 May 2022 13:40:58 -0000
On 23/05/2022 12:08, Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx wrote:
   <p outputclass="foo bar"/>
   <p outputclass="foo BAZ"/>
<xsl:template match="p">
       <xsl:attribute name="outputclass"
         select="tokenize(@outputclass, '\s+')
           => mine:add_value('BAZ')
           => distinct-values()
           => string-join(' ')"/>
       <xsl:apply-templates select="node()|(@* except @outputclass)"/>

With XSLT/XPath 3 there is a far more elegant and efficient method. Just add the following template:

   <xsl:template match="p/@outputclass[not(contains-token(.,'BAZ'))]>
    B B B  <xsl:attribute name="{name(.)}" select=". || ' BAZ'"/>

(see and your template for p can merely be:

   <xsl:template match="p">
    B B B  <xsl:apply-templates select="@*,node()" mode="#current"/>

and if you're using

<xsl:mode on-no-match="shallow-copy"/>

you won't even need the second template.

*John Lumley* MA PhD CEng FIEE

Current Thread