[英]SQL XML Select Value
How can I select the 'value' within this return. 如何在此返回中选择“值”。
<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">247734</int>
I have tried 我努力了
DECLARE @XML XML
SET @XML = '<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">247734</int>'
SELECT @XML.value('(/int)[1]', 'varchar(30)')
FROM @xml.nodes('/int') Tbl ( Col )
But I return no value. 但是我没有任何价值。
You can define an alias for the namespace and then use that alias in the xpath. 您可以为命名空间定义一个别名,然后在xpath中使用该别名。
Without specifying a namespace, an xpath would only find tags without a namespace. 如果不指定名称空间,则xpath将仅查找没有名称空间的标签。
Or you could look in the xpath for any namespace *:
或者,您可以在xpath中查找任何名称空间
*:
For example: 例如:
DECLARE @XML XML = N'<xml>
<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">247734</int>
<int>123</int>
</xml>';
-- method 1
;WITH XMLNAMESPACES('http://schemas.microsoft.com/2003/10/Serialization/' AS s)
SELECT Col.value('.', 'int') as Col
FROM @XML.nodes('//s:int') Tbl (Col);
-- method 2
SELECT Col.value('.', 'int') as Col
FROM @XML.nodes('//*:int') Tbl (Col);
Notice how method 1 only finds the first value, while method 2 finds both. 请注意,方法1仅找到第一个值,而方法2同时找到这两个值。
Try Following 尝试以下
DECLARE @XML XML
SET @XML = '<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">247734</int>'
SELECT Col.value('(.)[1]', 'int')
FROM @xml.nodes('//*') As Tbl (Col )
This issue is your XML is in a defined namespace, and you are trying to select using the "default namespace". 此问题是您的XML在定义的名称空间中,并且您尝试使用“默认名称空间”进行选择。
To fix the issue you should define the namespace in your selection: 要解决此问题,您应该在选择中定义名称空间:
DECLARE @XML XML
SET @XML = '<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">247734</int>'
SELECT @XML.value('declare namespace ns="http://schemas.microsoft.com/2003/10/Serialization/"; (/ns:int)[1]', 'varchar(30)' )
FROM @xml.nodes('/') Tbl ( Col )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.