0

Similar to this question but I need to parse based on specific attribute. How do you select only one of the elements with the same name based on the attribute value?

DECLARE @doc int
EXEC sp_xml_preparedocument @doc OUTPUT, N'
<rootnode>
  <group>
    <id>1</id>
     <anothernode lang="de">first string</anothernode>
     <anothernode lang="en">second string</anothernode>
  </group>
  <group>
    <id>1</id>
     <anothernode lang="en">I</anothernode>
     <anothernode lang="de">Ich</anothernode>
  </group>
</rootnode>'

SELECT *
FROM OPENXML (@doc, 'rootnode/group')
WITH
(
    id int 'id',
    anothernode  varchar(30) 'anothernode'
)

EXEC sp_xml_removedocument @doc

Result is:

1   first string
2   Ich

And I need:

1   second string
2   I

1 Answer 1

2

After some hacking I found that I can get correct result by doing following SELECT:

SELECT *
FROM OPENXML (@doc, 'rootnode/group')
WITH
(
    id int 'id',
    anothernode  varchar(30) 'anothernode[@lang="en"]'
)

Result is:

1   second string
2   I

Main difference is [@lang="en"], which selects specific attribute value.

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.