1

The last of my XML Parsing & T-Sql questions for a while. I have an xml field with data such as below:

<Criminal xmlns="http://schemas.somewebpage.com/data/stuff">
  <MessageContent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Content>Content in here!!</Content>
    <Type>Empty</Type>
  </MessageContent>
</Criminal>

This code does not seem to be working:

SELECT
Content = XmlContent.value('(/Criminal/MessageContent/Content)[1]', 'varchar(50)'),
Type = XmlContent.value('(/MessageContent/Type)[1]', 'varchar(50)')
FROM @table

...and only returns NULL.

Suggestions?

2
  • does it work if you remove the xml namespace attributes in the xml? Commented Nov 6, 2013 at 14:18
  • I removed the :xsi and :xsd namespace (if that was what you were referring to) and got an error. Then I removed all namespace references all together till was only left with: Criminal> <MessageContent> <Content>Content in here!!</Content> <Type>Empty</Type> </MessageContent> </Criminal> and my select statement worked. Only when the use of 3 namespaces did it cause problems for me. Commented Nov 6, 2013 at 14:53

1 Answer 1

2

The namespaces are tripping you up. You are also missing "/Criminal" in the 2nd part of the query. Try something like this:

;WITH XMLNAMESPACES(
  'http://schemas.somewebpage.com/data/stuff' as ns2,
  DEFAULT 'http://schemas.somewebpage.com/data/stuff'
  )
SELECT
Content = XmlContent.value('(/ns2:Criminal/MessageContent/Content)[1]', 'varchar(50)'),
Type = XmlContent.value('(/ns2:Criminal/MessageContent/Type)[1]', 'varchar(50)')
FROM @table

Results:

Content             Type
Content in here!!   Empty
Sign up to request clarification or add additional context in comments.

1 Comment

That did it! Must remember to declare the namespaces.

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.