开发者

SQL Server 2008 XML Query method - retrieve text() value where another text() value exists?

Please consider the following:

DECLARE @xml  XML
SET @xml =
'<Capture>
<Data><DataType>Card Number</DataType><Value>1234567898765</Value></Data>
<Data><DataType>Expiry Date</DataType><Value>1010</Value></Data>
</Capture>'

SELECT @xml.query('//*[text()="Expiry Date"]/text()')

Returns:

Expiry Date

Rather than retrieving the <DataType/> text node how can I retrieve the <Value/> text node where开发者_StackOverflow中文版 the text node value of <DataType/> is "Expiry Date"?


Try this:

SELECT 
  @xml.value('(//Data[DataType="Expiry Date"]/Value/text())[1]', 'varchar(50)')

You select any <Data> node which has the <DataType>Expiry Date</DataType>, and for that node, you select it's <Value> inner text.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜