1

I am having some issues with xpath(). Consider the example XML below:

<data code = '123' platform = 'a' xmlns = "http://www.myexample.com" >
 <payload>
   <title> this is it! </title>
 </payload>
</data>

This is what is stored in my column xml in the database. I am simply trying to retrieve the this is it! text. I have tried many different queries (I think the issue might be the namespace), nothing works.

For instance:

select *, xpath('//my:payload/text()',
                xml::xml,
                ARRAY[ARRAY['my','http://www.myexample.com']]) from mydatabase

returns nothing. Any ideas? Thanks!

5
  • 1
    mydatabases is strange name for a table Commented Oct 31, 2022 at 9:42
  • its an example :) . any idea? thanks Commented Oct 31, 2022 at 9:44
  • The text is in the tag title, not payload , therefor you need '//my:payload/my:title/text()' as the xpath Commented Oct 31, 2022 at 9:44
  • thanks but that still returns an empty result... Commented Oct 31, 2022 at 9:52
  • 3
    Works for me Commented Oct 31, 2022 at 9:55

1 Answer 1

1

for return data from XML i use xmltable

select *
from T
  cross join xmltable(xmlnamespaces ('http://www.myexample.com' as m),
                      '/m:data/m:payload' 
                      passing cast(xml_column as xml)
                      columns title text path 'm:title'
                      ) as x
Sign up to request clarification or add additional context in comments.

2 Comments

it seems I cannot use xmltable... only xpath seems to work
@ℕʘʘḆḽḘ then you are either using an outdated (unsupported) Postgres version or not using Postgres at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.