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

Re: How to do macro substitution in XPath?

Subject: Re: How to do macro substitution in XPath?
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 2 Dec 2023 18:24:55 -0000
Re:  How to do macro substitution in XPath?
>   > 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:

In my answer I wanted to adhere strictly to the XPath expressions provided
by Roger, and not to discuss their writing style, which goes way beyond the
original question ...

Thanks,
Dimitre

On Sat, Dec 2, 2023 at 10:12b/AM Michael Kay mike@xxxxxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

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

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.