开发者

xpath for xquery in SQL

开发者 https://www.devze.com 2023-02-02 06:50 出处:网络
I have a xml filed in sql table the xml have the structure like these <root> <element> <sub name=\"1\">

I have a xml filed in sql table

the xml have the structure like these

<root>
<element>
<sub name="1">
<date>the date <date>
</sub>
<sub name="2">
<date>the date <date>
</sub>
<sub name="3">
<date>the date <date>
</sub>
.....
<element>
</root>

how i can get the date of the element with the name 1?

I have prove :

 /root/element/sub[Name = "1"]/date/text()

but nothing ha开发者_JS百科pen.

any ideas?


Try this (SQL Server 2005 and up):

DECLARE @Input XML 
SET @input = '<root>
<element>
<sub name="1">
<date>the date</date>
</sub>
<sub name="2">
<date>the date</date>
</sub>
<sub name="3">
<date>the date</date>
</sub>
</element>
</root>'

SELECT 
    @input.value('(/root/element/sub[@name="1"]/date)[1]', 'varchar(50)')

You need to use @name to get the XML attribute name - without the @, it's trying to find an XML element (instead of the attribute)

0

精彩评论

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