Thread: BUG #4206: function xpath gives wrong results

BUG #4206: function xpath gives wrong results

From
"Martin Drescher"
Date:
The following bug has been logged online:

Bug reference:      4206
Logged by:          Martin Drescher
Email address:      martin.drescher@robotron.de
PostgreSQL version: 8.3.1
Operating system:   Windows XP SP2
Description:        function xpath gives wrong results
Details:

To demonstrate I use the following simple XML.

<?xml version="1.0"?>
<elem1>
  <elem2>one</elem2>
  <elem2>two</elem2>
  <elem2>three</elem2>
  <elem3 att="2"/>
</elem1>

I executed the following query in psql (with the corresponding XPath).

select xpath('//text()',xmlparse(document '<?xml version="1.0"
?><elem1><elem2>one</elem2><elem2>two</elem2><elem2>three</elem2><elem3
att="2"/></elem1>'));

The expected result is checked using Altova XMLSpy 2008.

XPath: (//text())[1]
Expected result: {one}
Actual Result:
  ERROR:  XX000: invalid XPath expression
  DETAIL:  Invalid expression
  CONTEXT:  SQL function "xpath" statement 1
  LOCATION:  xml_ereport, .\src\backend\utils\adt\xml.c:1351

XPath: boolean(/elem1)
Expected result: {true}
Actual Result:
  ERROR:  XX000: invalid XPath expression
  DETAIL:  Invalid expression
  CONTEXT:  SQL function "xpath" statement 1
  LOCATION:  xml_ereport, .\src\backend\utils\adt\xml.c:1351

XPath: /elem1/..
Expected result: document root
Actual Result:
   {"<x>
     <elem1>
       <elem2>one</elem2>
       <elem2>two</elem2>
       <elem2>three</elem2>
       <elem3 att=\"2\"/>
     </elem1>
   </x>"}

Note: This XPath returns the document root. I don’t know, how you choose
to represent it. However, it should not return the element <x>


The problem imo is in .\src\backend\utils\adt\xml.c:3294, where a
<x>-element is wrapped around the xml and the XPath-expression is prepended
by "/x".
This is not needed for XML documents and fragments consisting of a single
root. Instead it breaks many XPath-Expressions; like where you use multiple
absolute path expressions or function calls.
I think, queries on a fragment consisting of multiple roots are not possible
with XPath 1.0.