Thread: xmlns in xpath query

xmlns in xpath query

From
Frank Glandorf
Date:
Using postgresql v8.3 and xml xpath query function and having trouble with xmlns. Note: the select statements were originally all on a single line.

The xpath function works ok if the xmlns attributes are missing.

select xpath('/gpx', '<gpx  version="1.0" creator="GPSBabel - http://www.gpsbabel.org">abc</gpx>');

                                       xpath
-----------------------------------------------------------------------------------
 {"<gpx version=\"1.0\" creator=\"GPSBabel - http://www.gpsbabel.org\">abc</gpx>"}
(1 row)

The xpath function gives no result when xmlns attributes are supplied.

select xpath('/gpx', '<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" version="1.0" creator="GPSBabel - http://www.gpsbabel.org"  xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">abc</gpx>');

 xpath
-------
 {}
(1 row)

Setting the optional name space attributes for the xpath function also gives no results.

select xpath('/gpx', '<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" version="1.0" creator="GPSBabel - http://www.gpsbabel.org"  xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">abc</gpx>', array[array['xsi','http://www.w3.org/2001/XMLSchema-instance'],array['','http://www.topografix.com/GPX/1/0']]);

 xpath
-------
 {}
(1 row)


Suggestions?

-Frank

Re: xmlns in xpath query

From
Frank Glandorf
Date:
Other examples. A definition for the default name space causes a failure.

glandorf_webmaps=> select xpath('/gpx','<gpx xmlns="http://example.com" version="1.0">abc</gpx>') from trips where trip_id=90;
 xpath
-------
 {}
(1 row)
glandorf_webmaps=> select xpath('/gpx','<gpx xmlns:aaa="http://example.com" version="1.0">abc</gpx>') from trips where trip_id=90;
                                xpath
---------------------------------------------------------------------
 {"<gpx xmlns:aaa=\"http://example.com\" version=\"1.0\">abc</gpx>"}
(1 row)
glandorf_webmaps=> select xpath('/gpx','<gpx xmlns="http://example.com" version=
"1.0">abc</gpx>',array[array['gpx','http://example.com']]) from trips where trip_id=90;
 xpath
-------
 {}
(1 row)

On Fri, Sep 18, 2009 at 10:13 PM, Frank Glandorf <frank.glandorf@gmail.com> wrote:
Using postgresql v8.3 and xml xpath query function and having trouble with xmlns. Note: the select statements were originally all on a single line.

The xpath function works ok if the xmlns attributes are missing.

select xpath('/gpx', '<gpx  version="1.0" creator="GPSBabel - http://www.gpsbabel.org">abc</gpx>');

                                       xpath
-----------------------------------------------------------------------------------
 {"<gpx version=\"1.0\" creator=\"GPSBabel - http://www.gpsbabel.org\">abc</gpx>"}
(1 row)

The xpath function gives no result when xmlns attributes are supplied.

select xpath('/gpx', '<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" version="1.0" creator="GPSBabel - http://www.gpsbabel.org"  xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">abc</gpx>');

 xpath
-------
 {}
(1 row)

Setting the optional name space attributes for the xpath function also gives no results.

select xpath('/gpx', '<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" version="1.0" creator="GPSBabel - http://www.gpsbabel.org"  xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">abc</gpx>', array[array['xsi','http://www.w3.org/2001/XMLSchema-instance'],array['','http://www.topografix.com/GPX/1/0']]);

 xpath
-------
 {}
(1 row)


Suggestions?

-Frank