简体   繁体   English

XPath根据SQL Server xml中的属性“B”获取属性“A”的值

[英]XPath get value of attribute 'A' based on attribute 'B' in SQL Server xml

I have this XML in SQL Server 我在SQL Server中有这个XML

<data>
<add key="images" value="image/path/img.gif" />
<data>

I want to select the value attribute of every "add" node with key = "images" 我想用key =“images”选择每个“add”节点的value属性

What i have now is: 我现在拥有的是:

SELECT ID, Data from Items 
where Data.value('(//data/add[@key="images"]/@value)[1]', 'nvarchar') Like '%img%'

Any suggestions? 有什么建议?

What you have works fine if you just specify a size for your nvarchar retrieved from the XML. 如果只是为从XML检索的nvarchar指定大小,那么你的工作正常。

SELECT ID, Data
from Items 
where Data.value('(//data/add[@key="images"]/@value)[1]', 'nvarchar(100)') Like '%img%'

Here I have specified 100 you may set it to something more appropriate for your situation. 在这里我指定了100你可以将它设置为更适合你情况的东西。 Without the size the column will have size 1 . 没有大小,列的大小为1

Use this XPath expression : 使用此XPath表达式

(//data
    /add
      [@key="images"]
         /@value
            [contains(.,"img")]
 )
 [1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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