Jonas
Jonas

Reputation: 67

I need a XML Element of a XML Element

Lets pretend i have the following XML.

<xml>
 ...
 <tag>
  <othertag>
  </othertag>
 </tag>
 ...
</xml>

What i want is to "extract" the

<tag>
  <othertag>
  </othertag>
 </tag>

into another Variable. How can i achieve this?

Upvotes: 0

Views: 19

Answers (1)

Gottfried Lesigang
Gottfried Lesigang

Reputation: 67311

DECLARE @xml XML=
'<xml>
 <SomeTag test="x"/>
 <SomeTag test="y"/>
 <tag>
  <othertag>
  </othertag>
 </tag>
 <SomeTag test="z"/>
</xml>';

If possible, be as specific as possible

DECLARE @xml2 XML = @xml.query('(/xml/tag)[1]');

SELECT @xml2;

If not, you might go the lazy way...

DECLARE @xml3 XML = @xml.query('//tag');

SELECT @xml3;

Upvotes: 1

Related Questions