This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 7450 - [XPath] 'for' expression example does not produce expected output
Summary: [XPath] 'for' expression example does not produce expected output
Status: RESOLVED FIXED
Alias: None
Product: XPath / XQuery / XSLT
Classification: Unclassified
Component: XPath 2.0 (show other bugs)
Version: Recommendation
Hardware: All All
: P5 trivial
Target Milestone: ---
Assignee: Jonathan Robie
QA Contact: Mailing list for public feedback on specs from XSL and XML Query WGs
URL: http://www.w3.org/TR/2007/REC-xpath20...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-08-28 01:07 UTC by Henry Zongaro
Modified: 2010-02-17 22:56 UTC (History)
0 users

See Also:


Attachments

Description Henry Zongaro 2009-08-28 01:07:21 UTC
Some colleagues pointed out a problem in example 'for' expression that appears in the second box in section 3.7 of XPath 2.0.[1]

    for $a in fn:distinct-values(book/author)
    return (book/author[. = $a][1], book[author = $a]/title)

The paragraph that appears before the example indicates that it "transforms the input document into a list in which each author's name appears only once, followed by a list of titles of books written by that author."

However, the expression before the comma operator actually selects, for each book element that has one or more author elements that match $a, the first such author element for that book.  So one author element will appear for each book written by that author.  With the given input, the output will be:

<author>Stevens</author>
<author>Stevens</author>
<title>TCP/IP Illustrated</title>
<title>Advanced Programming in the Unix environment</title>
<author>Abiteboul</author>
<title>Data on the Web</title>
<author>Buneman</author>
<title>Data on the Web</title>
<author>Suciu</author>
<title>Data on the Web</title>

The following was probably intended:

    for $a in fn:distinct-values(book/author)
    return ((book/author[. = $a])[1], book[author = $a]/title)

The corresponding example in XQuery 1.0 was written differently, and does not have the same problem.

[1] http://www.w3.org/TR/2007/REC-xpath20-20070123/#id-for-expressions
Comment 1 Jonathan Robie 2010-02-11 00:31:26 UTC
I am fixing this for future versions of XPath.