简体   繁体   中英

Selecting next nodes in XML by it's text content in SQL

I am trying to query the XML below in a SQL select statement to find the type for book3.

<Book>
  <Title>Book1</Title>
  <Type>fiction</Type>
  <Cost>9.99</Cost>
</Book>

<Book>
  <Title>Book2</Title>
  <Type>non-fiction</Type>
  <Cost>5.99</Cost>
</Book>

<Book>
  <Title>Book3</Title>
  <Type>fiction</Type>
  <Cost>14.99</Cost>
</Book>

<Book>
  <Title>Book4</Title>
  <Type>non-fiction</Type>
  <Cost>19.99</Cost>
</Book>
...

I am trying to select the title "book 3" in my select statement by searching for this as a text, and the type which follows ie book3,fiction

So far I can perform a select by the index as below but I really want to find the node by incorporating some kind of text='book 3' statement.

SELECT XML.value('(/*//Book/Title/node())[3]', 'nvarchar(max)') as 'result'

I am not familiar on how to do this (I'm quite new to this approach)

Can anyone give me some pointers on how to do this? Or if there are any good resources online I can read up on (I can only find bits on pieces on how to do this in SQL)

Thanks!

This query should work: //Book[Title="Book3"]/Type/text()

Tip: this tool might be useful to test your XPATH queries: http://www.xpathtester.com/xpath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM