Thread: BUG #4024: xpath() results lose namespace mappings

BUG #4024: xpath() results lose namespace mappings

From
"Matt Magoffin"
Date:
The following bug has been logged online:

Bug reference:      4024
Logged by:          Matt Magoffin
Email address:      postgresql.org@msqr.us
PostgreSQL version: 8.3.0
Operating system:   OS X 10.5, Windows XP
Description:        xpath() results lose namespace mappings
Details:

I was trying to extract XML fragments within a pl/pgsql function by nested
xpath() function calls, and found that when namespaces are used in the
XML, only the first xpath() call is able to correctly use namespaces.

First here is an example that works, when no namespaces are used:

BEGIN;
CREATE TEMPORARY TABLE tmp_xpath_test (x xml) ON COMMIT DROP;
INSERT INTO tmp_xpath_test VALUES (
    '<foo><bar x="y">bar1</bar><bar x="y">bar2</bar></foo>'::xml);
SELECT (xpath('/foo/bar[1]', t.x))[1] FROM tmp_xpath_test t;
SELECT xpath('/bar/@x', (xpath('/foo/bar[1]', t.x))[1]) FROM
tmp_xpath_test t;
COMMIT;

The first select is just there to show the result of the inner call to
xpath() in the second select, and the second select returns:

SELECT xpath('/bar/@x', (xpath('/foo/bar[1]', t.x))[1]) FROM
tmp_xpath_test t;
xpath
-------
{y}
(1 row)

Now if I use XML with namespaces, the first SELECT works, but the second
never returns the expected results:

BEGIN;
CREATE TEMPORARY TABLE tmp_xpath_test (x xml) ON COMMIT DROP;
INSERT INTO tmp_xpath_test VALUES (
    '<a:foo xmlns:a="a:urn"><a:bar x="y">bar1</a:bar><a:bar
x="y">bar2</a:bar></a:foo>'::xml);
SELECT (xpath('/a:foo/a:bar[1]', t.x, ARRAY[ARRAY['a','a:urn']]))[1] FROM
tmp_xpath_test t;
SELECT xpath('/a:bar/@x', (xpath('/a:foo/a:bar[1]', t.x,
ARRAY[ARRAY['a','a:urn']]))[1],
    ARRAY[ARRAY['a','a:urn']]) FROM tmp_xpath_test t;
COMMIT;

The select results are

SELECT (xpath('/a:foo/a:bar[1]', t.x, ARRAY[ARRAY['a','a:urn']]))[1] FROM
tmp_xpath_test t;
          xpath
---------------------------
<a:bar x="y">bar1</a:bar>
(1 row)

SELECT xpath('/a:bar/@x', (xpath('/a:foo/a:bar[1]', t.x,
ARRAY[ARRAY['a','a:urn']]))[1],
lms_kia(> ARRAY[ARRAY['a','a:urn']]) FROM tmp_xpath_test t;
xpath
-------
{}
(1 row)

For the second select, I expected a single XML text node containing "y",
just like from the no-namespace result.

Re: BUG #4024: xpath() results lose namespace mappings

From
Bruce Momjian
Date:
Would someone please respond to this bug report?

---------------------------------------------------------------------------

Matt Magoffin wrote:
>
> The following bug has been logged online:
>
> Bug reference:      4024
> Logged by:          Matt Magoffin
> Email address:      postgresql.org@msqr.us
> PostgreSQL version: 8.3.0
> Operating system:   OS X 10.5, Windows XP
> Description:        xpath() results lose namespace mappings
> Details:
>
> I was trying to extract XML fragments within a pl/pgsql function by nested
> xpath() function calls, and found that when namespaces are used in the
> XML, only the first xpath() call is able to correctly use namespaces.
>
> First here is an example that works, when no namespaces are used:
>
> BEGIN;
> CREATE TEMPORARY TABLE tmp_xpath_test (x xml) ON COMMIT DROP;
> INSERT INTO tmp_xpath_test VALUES (
>     '<foo><bar x="y">bar1</bar><bar x="y">bar2</bar></foo>'::xml);
> SELECT (xpath('/foo/bar[1]', t.x))[1] FROM tmp_xpath_test t;
> SELECT xpath('/bar/@x', (xpath('/foo/bar[1]', t.x))[1]) FROM
> tmp_xpath_test t;
> COMMIT;
>
> The first select is just there to show the result of the inner call to
> xpath() in the second select, and the second select returns:
>
> SELECT xpath('/bar/@x', (xpath('/foo/bar[1]', t.x))[1]) FROM
> tmp_xpath_test t;
> xpath
> -------
> {y}
> (1 row)
>
> Now if I use XML with namespaces, the first SELECT works, but the second
> never returns the expected results:
>
> BEGIN;
> CREATE TEMPORARY TABLE tmp_xpath_test (x xml) ON COMMIT DROP;
> INSERT INTO tmp_xpath_test VALUES (
>     '<a:foo xmlns:a="a:urn"><a:bar x="y">bar1</a:bar><a:bar
> x="y">bar2</a:bar></a:foo>'::xml);
> SELECT (xpath('/a:foo/a:bar[1]', t.x, ARRAY[ARRAY['a','a:urn']]))[1] FROM
> tmp_xpath_test t;
> SELECT xpath('/a:bar/@x', (xpath('/a:foo/a:bar[1]', t.x,
> ARRAY[ARRAY['a','a:urn']]))[1],
>     ARRAY[ARRAY['a','a:urn']]) FROM tmp_xpath_test t;
> COMMIT;
>
> The select results are
>
> SELECT (xpath('/a:foo/a:bar[1]', t.x, ARRAY[ARRAY['a','a:urn']]))[1] FROM
> tmp_xpath_test t;
>           xpath
> ---------------------------
> <a:bar x="y">bar1</a:bar>
> (1 row)
>
> SELECT xpath('/a:bar/@x', (xpath('/a:foo/a:bar[1]', t.x,
> ARRAY[ARRAY['a','a:urn']]))[1],
> lms_kia(> ARRAY[ARRAY['a','a:urn']]) FROM tmp_xpath_test t;
> xpath
> -------
> {}
> (1 row)
>
> For the second select, I expected a single XML text node containing "y",
> just like from the no-namespace result.
>
> --
> Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-bugs

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +

Re: BUG #4024: xpath() results lose namespace mappings

From
Bruce Momjian
Date:
Would someone please comment on this bug report from March, 2008?

---------------------------------------------------------------------------

Matt Magoffin wrote:
>
> The following bug has been logged online:
>
> Bug reference:      4024
> Logged by:          Matt Magoffin
> Email address:      postgresql.org@msqr.us
> PostgreSQL version: 8.3.0
> Operating system:   OS X 10.5, Windows XP
> Description:        xpath() results lose namespace mappings
> Details:
>
> I was trying to extract XML fragments within a pl/pgsql function by nested
> xpath() function calls, and found that when namespaces are used in the
> XML, only the first xpath() call is able to correctly use namespaces.
>
> First here is an example that works, when no namespaces are used:
>
> BEGIN;
> CREATE TEMPORARY TABLE tmp_xpath_test (x xml) ON COMMIT DROP;
> INSERT INTO tmp_xpath_test VALUES (
>     '<foo><bar x="y">bar1</bar><bar x="y">bar2</bar></foo>'::xml);
> SELECT (xpath('/foo/bar[1]', t.x))[1] FROM tmp_xpath_test t;
> SELECT xpath('/bar/@x', (xpath('/foo/bar[1]', t.x))[1]) FROM
> tmp_xpath_test t;
> COMMIT;
>
> The first select is just there to show the result of the inner call to
> xpath() in the second select, and the second select returns:
>
> SELECT xpath('/bar/@x', (xpath('/foo/bar[1]', t.x))[1]) FROM
> tmp_xpath_test t;
> xpath
> -------
> {y}
> (1 row)
>
> Now if I use XML with namespaces, the first SELECT works, but the second
> never returns the expected results:
>
> BEGIN;
> CREATE TEMPORARY TABLE tmp_xpath_test (x xml) ON COMMIT DROP;
> INSERT INTO tmp_xpath_test VALUES (
>     '<a:foo xmlns:a="a:urn"><a:bar x="y">bar1</a:bar><a:bar
> x="y">bar2</a:bar></a:foo>'::xml);
> SELECT (xpath('/a:foo/a:bar[1]', t.x, ARRAY[ARRAY['a','a:urn']]))[1] FROM
> tmp_xpath_test t;
> SELECT xpath('/a:bar/@x', (xpath('/a:foo/a:bar[1]', t.x,
> ARRAY[ARRAY['a','a:urn']]))[1],
>     ARRAY[ARRAY['a','a:urn']]) FROM tmp_xpath_test t;
> COMMIT;
>
> The select results are
>
> SELECT (xpath('/a:foo/a:bar[1]', t.x, ARRAY[ARRAY['a','a:urn']]))[1] FROM
> tmp_xpath_test t;
>           xpath
> ---------------------------
> <a:bar x="y">bar1</a:bar>
> (1 row)
>
> SELECT xpath('/a:bar/@x', (xpath('/a:foo/a:bar[1]', t.x,
> ARRAY[ARRAY['a','a:urn']]))[1],
> lms_kia(> ARRAY[ARRAY['a','a:urn']]) FROM tmp_xpath_test t;
> xpath
> -------
> {}
> (1 row)
>
> For the second select, I expected a single XML text node containing "y",
> just like from the no-namespace result.
>
> --
> Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-bugs

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +