从下面的 xml 中,我试图提取 IP,但这不起作用..我不确定我在哪里犯了错误
declare @xml xml
set @xml='<auditElement>
<RequestOrigination>
<IP>20.20.20.20</IP>
</RequestOrigination>
</auditElement>'
我的尝试
select
b.value('@IP[1]','nvarchar(100)')
from @xml.nodes('/auditElement/RequestOrigination') as org(b)
需要的输出:
IP
20.20.20.20
最佳答案
不需要 .nodes()
...
IP
前不需要@
。这将尝试读取名为“IP”的属性,但您正在读取*元素的内容(text()
节点)。您的代码适用于这样的事情:
<SomeElement IP="20.20.20.20">
您可以像这里一样将 .value()
与完整的 XPath
一起使用:
declare @xml xml
set @xml='<auditElement>
<RequestOrigination>
<IP>20.20.20.20</IP>
</RequestOrigination>
</auditElement>';
SELECT @xml.value('(/auditElement/RequestOrigination/IP/text())[1]','varchar(20)');
关于sql-server - 尝试使用 XML 节点从 XML 中提取 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52633356/