(unnamed)

text/html

Filename: (unnamed)
Type: text/html
Part: 0
Message: Re: xpath improvement suggestion
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Robert Haas wrote:
<blockquote
 cite="mid:603c8f071001051854x10875b50vf620a3fca727ab11@mail.gmail.com"
 type="cite">
  <pre wrap="">On Tue, Jan 5, 2010 at 6:09 PM, Arie Bikker <a
 class="moz-txt-link-rfc2396E" href="mailto:arie@abikker.nl">&lt;arie@abikker.nl&gt;</a> wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">Hi all,

Well I had &nbsp;to burn some midnight oil trying to figure out why a construct
like
SELECT xpath('name()','&lt;a/&gt;');
doesn't give the expected result. Kept getting an empty array:
&nbsp;xpath
-------------
{}
instead of the expected "{a}"
BugID 4294 and the TODO item "better handling of XPath data types" pointed
in the right direction.
whithin src/backend/utils/adt/xml.c in the function xpath the result of the
call to xmlXPathCompiledEval is not handled optimally. In fact, the result
is assumed to be a nodeset without consulting the -&gt;type member of the
result. I've made some minor changes to xml.c to handle some non-nodeset
results of xmlXPathCompiledEval.
Essentially, the revised code makes an array of all the nodes in the
xpathobj result in case this is a nodeset, or an array with a single element
in case the reult is a number/string/boolean. The problem cases mentioned in
<a class="moz-txt-link-freetext"
 href="http://archives.postgresql.org/pgsql-hackers/2008-06/msg00616.php">http://archives.postgresql.org/pgsql-hackers/2008-06/msg00616.php</a> now work
as expected.
Revision of the code involves:
- A switch statement to handle the result type of xmlXPathCompiledEval.
- an additional function xmlpathobjtoxmltype.

diff of the revisioned code with respect to original is in attached file.

kind regards, Arie Bikker
    </pre>
  </blockquote>
  <pre wrap=""><!---->
Hi,

Could you please resend this as a context diff and add it to our patch
management application?

<a class="moz-txt-link-freetext"
 href="http://wiki.postgresql.org/wiki/Submitting_a_Patch">http://wiki.postgresql.org/wiki/Submitting_a_Patch</a>
<a class="moz-txt-link-freetext"
 href="https://commitfest.postgresql.org/action/commitfest_view/open">https://commitfest.postgresql.org/action/commitfest_view/open</a>

Thanks!

...Robert
  </pre>
</blockquote>
Hope this is the right attachement type (I'm new at this)<br>
BTW. here a some nice examples:<br>
<br>
- Get the number of attributes of the first childnode:<br>
<tt>select ( xpath('count(@*)',(xpath('*[1]','&lt;a b="c"&gt;&lt;d
e="f" g="j"/&gt;&lt;/a&gt;'))[1]))[1];<br>
<br>
</tt>- an alternative for xpath_exist('/a/d')<br>
<tt>select (xpath('boolean(/a/d)','&lt;a b="c"&gt;&lt;d e="f"
g="j"/&gt;&lt;/a&gt;'))[1];<br>
<br>
</tt>- fixes bug 4206<br>
<tt>select xpath('//text()',xmlparse(document '&lt;?xml
version="1.0"?&gt;&lt;elem1&gt;&lt;elem2&gt;one&lt;/elem2&gt;&lt;elem2&gt;two&lt;/elem2&gt;&lt;elem2&gt;three&lt;/elem2&gt;&lt;elem3</tt><tt>att="2"/&gt;&lt;/elem1&gt;'));<br>
</tt><tt><br>
</tt>- fixes bug 4294<br>
<tt>select xpath('name(/my:a/*[last()])', '&lt;a
xmlns=<a class="moz-txt-link-rfc2396E" href="http://myns.com/ns">"http://myns.com/ns"</a>&gt;&lt;b&gt;text1&lt;/b&gt;&lt;c&gt;text2&lt;/c&gt;&lt;/a&gt;',
ARRAY[ARRAY['my','<a class="moz-txt-link-freetext"
 href="http://myns.com/ns">http://myns.com/ns</a>']]);<br>
<br>
</tt>kind regards, Arie Bikker<br>
</body>
</html>