Re: [xsl] How to do macro substitution in XPath?

Subject: Re: [xsl] How to do macro substitution in XPath?
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 2 Dec 2023 18:12:18 -0000
> Here is the wanted "pure XPath" that preserves the "readability":
>
>       let $LBJ := /Book/Title eq 'Path to Power' and /Book/Author eq 'Robert
A. Caro',
>           $RM  := /Book/Title eq 'Power Broker' and /Book/Author eq 'Robert
A. Caro'
>         return
>           if($LBJ) then 'action 1'
>           else if($RM) then 'action 2'
>           else 'Error'
>

The disadvantage here is that the code doesn't have a high level of
reusability. The code as written can only be used to test a book element that
is the outermost element of the current document. I prefer to use functions:

let $LBJ := function($book as element(Book)) as xs:boolean {$book/Title eq
'....' and $book/Author eq '....')
let $RM := function(....)

return
   if ($LBJ(/Book)) then 'action 1'
   else if ($RM(/Book)) then 'action 2'
   ...

But although an XPath expression was requested, the context appears to be
XSLT, and in that case I would use good old template rules:

<xsl:template match="Book[Title eq 'Path to Power' and Author eq 'Robert A.
Caro']">
   action-1
</xsl:template>

<xsl:template match="Book[Title eq 'Power Broker' and Author eq 'Robert A.
Caro']">
   action-2
</xsl:template>


Michael Kay
Saxonica

Current Thread