7

I want to extract location id from give xml, and also want to put condition that extract it if location does exists.

<?xml version="1.0" encoding="UTF-8"?>
<id>1006221342207</id>
<name>Hitesh Modha</name>
<email>[email protected]</email>
<location>
    <id>115440481803904</id>
    <name>Ahmedabad, India</name>
</location>

I have tried :

SET location = ExtractValue(xml, '//locations//id');    
SELECT ExtractValue(location, '//id'); 

but it is not working. Please help

3
  • You can use SimpleXML class then insert your data in the database Commented Jun 17, 2014 at 6:45
  • Your XML is invalid, it is missing a document element. Commented Jun 17, 2014 at 7:44
  • @ThW: It's not invalid, ExtractValue expects an XML fragment, not a Document, so it's fine. Commented Jun 19, 2014 at 17:36

1 Answer 1

13

The SQL

SELECT
  ExtractValue('<?xml version="1.0" encoding="UTF-8"?>
<id>1006221342207</id>
<name>Hitesh Modha</name>
<email>[email protected]</email>
<location>
    <id>115440481803904</id>
    <name>Ahmedabad, India</name>
</location>', '//location//id');

returns only an empty result, because the path //locations//id is empty for the XML fragment you use.

You're more likely looking for '/location/id' for the xpath expression, as it will return the id text() value: 115440481803904

Next mistake you do is that you think ExtractValue would return an XML fragment you can re-run ExtractValue on. That's just not the case (only if - which is not the case in your example - there would have been XML encoded as CDATA in the located text() node).

All the details are explained in detail here:

If you can not work around these misconceptions, there is no further suggestion I can give you.

Sign up to request clarification or add additional context in comments.

Comments

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.