How do you get a node that contains a child node with a given attribute value in an XML document using sql server?

Andy 1

I'm working on parsing XML documents using SQL Server 2008. I'm a complete noob and I was wondering if I can get help from you guys.

I have an XML document like the one below and I want to get the "section" node where the "code" node has val=5.

<root>
  <section>
    <code val=6 />
    ...
  </section>
  <section>
    <code val=5 />
    ...
  </section>
  <section>
    <code val=4 />
    ...
  </section>
</root>

So the result should be: <section> <code val=5 /> ... </section>

I tried doing this, but it didn't work:

select @xml.query('/root/section') where @xml.value('/root/section/code/@val,'int')= '5'

I also tried this: select @xml.query('/root/section') where @xml.exist('/root[1]/section[1]/code[@val="1"])= '1'

Any ideas? Thanks in advance.

Bogdan Sahlean

You could use this query:

DECLARE @x XML=N'
<root>
  <section atr="A">
    <code val="5" />
  </section>
  <section atr="B">
    <code val="6" />
  </section>
  <section atr="C">
    <code val="5" />
  </section>
</root>';

SELECT  a.b.query('.') AS SectionAsXmlElement,
        a.b.value('@atr','NVARCHAR(50)') AS SectionAtr
FROM    @x.nodes('/root/section[code/@val="5"]') a(b);

Results:

SectionAsXmlElement                         SectionAtr
------------------------------------------- ----------
<section atr="A"><code val="5" /></section> A
<section atr="C"><code val="5" /></section> C

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Echo XML child node value based on node attribute value

분류에서Dev

How do I get the specified node from XML in SQL

분류에서Dev

How to transform an XML adding a child node using XSLT?

분류에서Dev

remove attribute on document node

분류에서Dev

Filter XML using XSLT based on a child Node

분류에서Dev

Get value of an Xml attribute using Linq to Xml

분류에서Dev

How to get child element value in XML using LINQ

분류에서Dev

XML Get Value from Node C#

분류에서Dev

How do I modify a XML Node?

분류에서Dev

Delphi, TTreeView: how to get the screen coordinates of the given node and its icon?

분류에서Dev

How to get value of parent node as string?

분류에서Dev

How do you get the value of a "embed"?

분류에서Dev

xpath expression with predicates to get node value from xml with namespace

분류에서Dev

xml select next node and change value of that node

분류에서Dev

How do you update a node/minion's label in Kubernetes?

분류에서Dev

How do you create contiguous numbers in a field using a SQL Server trigger?

분류에서Dev

XML grab attribute from specific element node

분류에서Dev

How do you query a MongoDB document where an array attribute is null or of zero length?

분류에서Dev

Retrieve a node value from xml

분류에서Dev

Retrieve a node value from xml

분류에서Dev

How to find children of an XML element with a given attribute?

분류에서Dev

If condition on Selecting XML node in SQL

분류에서Dev

Attempt to get next and previous sibling of a node after parsing xml for a specific node using java

분류에서Dev

How to get multiple node element types into one variable from XML

분류에서Dev

php xpath query to get parent node based on value in repeating child nodes

분류에서Dev

How to get the value of custom HTML attribute using JQuery?

분류에서Dev

How do you get the numerical value from a string of digits?

분류에서Dev

How do I get the node count of SKSpriteNodes not visible in the scene?

분류에서Dev

How to add more pages and send a value in node using express?

Related 관련 기사

  1. 1

    Echo XML child node value based on node attribute value

  2. 2

    How do I get the specified node from XML in SQL

  3. 3

    How to transform an XML adding a child node using XSLT?

  4. 4

    remove attribute on document node

  5. 5

    Filter XML using XSLT based on a child Node

  6. 6

    Get value of an Xml attribute using Linq to Xml

  7. 7

    How to get child element value in XML using LINQ

  8. 8

    XML Get Value from Node C#

  9. 9

    How do I modify a XML Node?

  10. 10

    Delphi, TTreeView: how to get the screen coordinates of the given node and its icon?

  11. 11

    How to get value of parent node as string?

  12. 12

    How do you get the value of a "embed"?

  13. 13

    xpath expression with predicates to get node value from xml with namespace

  14. 14

    xml select next node and change value of that node

  15. 15

    How do you update a node/minion's label in Kubernetes?

  16. 16

    How do you create contiguous numbers in a field using a SQL Server trigger?

  17. 17

    XML grab attribute from specific element node

  18. 18

    How do you query a MongoDB document where an array attribute is null or of zero length?

  19. 19

    Retrieve a node value from xml

  20. 20

    Retrieve a node value from xml

  21. 21

    How to find children of an XML element with a given attribute?

  22. 22

    If condition on Selecting XML node in SQL

  23. 23

    Attempt to get next and previous sibling of a node after parsing xml for a specific node using java

  24. 24

    How to get multiple node element types into one variable from XML

  25. 25

    php xpath query to get parent node based on value in repeating child nodes

  26. 26

    How to get the value of custom HTML attribute using JQuery?

  27. 27

    How do you get the numerical value from a string of digits?

  28. 28

    How do I get the node count of SKSpriteNodes not visible in the scene?

  29. 29

    How to add more pages and send a value in node using express?

뜨겁다태그

보관