0

I currently have an XML I am pulling from a column in SQL formatted like this:

<Datas>
   <CHILD value="test" />
</Datas>

I need a way to grab the value in the CHILD tag but I am having some issues I believe to be stemming from it being a self closing tag. I am able to get the data if it was formatted in this format:

<Datas>
   <CHILD>test</CHILD>
</Datas>

However, this format is not an option as the application I'm pulling from does not store it this way.

I have tried the following SQL:

select cast(xmlField as xml) xmlField into tmp from (
select '<Datas><CHILD value = "test"/></Datas>' xmlField
) tb
SELECT
xmlField.value('(Datas/CHILD)[1]', 'nvarchar(max)') as Data
FROM tmp
drop table tmp

Any help is greatly appreciated. Thanks!

1 Answer 1

2

The problem isn't that it's self closing, it's that you want to access an attribute. For those you need to use the attribute's name prefixed with an @:

SELECT xmlField.value('(Datas/CHILD/@value)[1]', 'nvarchar(max)') AS Data
FROM tmp;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!

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.