0

I have an issue as I never done that before.

I have an SQL table with the following :

ID int;
xml_record xml;

The xml record is looking like that :

<root xml:space="preserve" id="XXX">
<c1>Data1</c1>
<c2>Data2</c2>
<c3>Data3</c3>
...
<cn>DataN</cn>
</root>

However I tried to use the following query with no success (return null) :

SELECT xml_record.value('c1[1]','varchar(50)') as value_c1
FROM myTable

The problem might come from the "space" but not sure.

1
  • 1
    XML support is highly vendor-specific - so please add a tag to specify whether you're using mysql, postgresql, sql-server, oracle or db2 - or something else entirely. Commented Nov 22, 2018 at 20:45

2 Answers 2

2

You just need to fix the expression:

SELECT xml_record.value('(/root/c1)[1]','varchar(50)') AS value_c1
FROM ...
Sign up to request clarification or add additional context in comments.

Comments

0
   SELECT 
   xml_record.value
   ( '(/root/c1/text())[1])', 
  'varchar(50)') as value_c1
   FROM myTable 

else remove the first xml line

3 Comments

Thank you I'll try as soon as I can. But actually the record is a big line. How could I get rid of the first line ?
@HimanshuAhuja Why do you double the slash before //c1? This will trigger a deep search, which is not needed here... Furthermore you should pick text() instead of node()
@Shnugo my bad actually i referred from a different article got your point now //c1 will trigger for all c1s as you correctly said as deep search and wasnt aware that ` text() was there to select text inside the tag. thanks for the help :)

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.