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

Re: Seek XPath 2.0 expression for this: Are there any

Subject: Re: Seek XPath 2.0 expression for this: Are there any book titles with more than one binding?
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 2 May 2019 15:07:49 -0000
Re:  Seek XPath 2.0 expression for this: Are there any
You're using "=" rather than "is" to compare books. That's a dangerous mistake
to make (a) because it gives you the right answer 99% of the time, and (b)
because it's incredibly expensive.

I couldn't think of a better approach with XPath 2.0. As Martin says, it's the
wrong tool for the job.

Michael Kay
Saxonica

> On 2 May 2019, at 13:55, Costello, Roger L. costello@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Hello XPath experts!
>
> At the bottom of this message is my XML document. I need an XPath 2.0
expression that returns the result of this query:
>
> 	Are there any book titles with more than
> 	one binding? If yes, then show the first book
> 	for each binding.
>
> For the below XML document, the query should return these two books:
>
> <Book>
>    <Author>Sally Smith</Author>
>    <Title>XYZ</Title>
>    <Binding>hardcover</Binding>
>    <Count>1</Count>
>    <Description>lo lo lo ..</Description>
> </Book>
> <Book>
>    <Author>Sally Smith</Author>
>    <Title>XYZ</Title>
>    <Binding>softcover</Binding>
>    <Count>1</Count>
>    <Description>do do do ..</Description>
> </Book>
>
> Notice that the two books have the same author and title but different
binding (hardcover versus softcover).
>
> This XPath 2.0 expression seems to work:
>
> for $i in /Bookstore/BookTitle/Book[1],
>    $j in /Bookstore/BookTitle/Book[1] return
>        if (($i ne $j)
>            and ($i/Author eq $j/Author)
>            and ($i/Title eq $j/Title)
>            and ($i/Binding ne $j/Binding)
>            and ($i = $j/preceding::Book))
>        then ($i, $j)
>        else ()
>
> That expression seems awfully complicated. Is there a simpler expression? If
there isn't a simpler expression, then do you see anything missing in the
expression (i.e., something that the expression doesn't take into
consideration)?  /Roger
>
> <Bookstore>
>    <BookTitle>
>        <Book>
>            <Author>John Doe</Author>
>            <Title>ABC</Title>
>            <Binding>hardcover</Binding>
>            <Count>1</Count>
>            <Description>da da da ..</Description>
>        </Book>
>        <Book>
>            <Author>John Doe</Author>
>            <Title>ABC</Title>
>            <Binding>hardcover</Binding>
>            <Count>2</Count>
>            <Description>za za za ..</Description>
>        </Book>
>    </BookTitle>
>    <BookTitle>
>        <Book>
>            <Author>John Doe</Author>
>            <Title>Foo</Title>
>            <Binding>softcover</Binding>
>            <Count>1</Count>
>            <Description>na na na ..</Description>
>        </Book>
>    </BookTitle>
>    <BookTitle>
>        <Book>
>            <Author>Sally Smith</Author>
>            <Title>XYZ</Title>
>            <Binding>hardcover</Binding>
>            <Count>1</Count>
>            <Description>lo lo lo ..</Description>
>        </Book>
>        <Book>
>            <Author>Sally Smith</Author>
>            <Title>XYZ</Title>
>            <Binding>hardcover</Binding>
>            <Count>2</Count>
>            <Description>ho ho ho ..</Description>
>        </Book>
>    </BookTitle>
>    <BookTitle>
>        <Book>
>            <Author>Sally Smith</Author>
>            <Title>XYZ</Title>
>            <Binding>softcover</Binding>
>            <Count>1</Count>
>            <Description>do do do ..</Description>
>        </Book>
>    </BookTitle>
> </Bookstore>

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.