TSQL XML 解析
我正在调用一个 Web 服务(网址如下),并将 XML 结果作为 varchar(8000) 返回到 SQL Server 中,然后将其转换为 XML。这非常有效。我想将此 XML 信息解析为单独的值,但仍会获取空值。这是我第一次尝试在 SQL 2008 服务器上使用 XML,所以我知道我错过了一个非常微不足道的项目。
我正在获取收到的响应并将其存储到@XML 中。
SET @XML = CAST(@Response AS XML)
接下来,我尝试提取邮政编码来获取结果,但收到 NULL 或错误的节点。
返回 NULL
SELECT @XML.value('(/GeocodingResult/Results/Address/PostalCode) [1]', 'varchar(50)')
返回“版权所有 © 2010 Microsoft 及其供应商。全部”(不带引号)
SELECT @XML.value('(/) [1]', 'varchar(50)')
I'm calling a webservice (url below) and am getting the XML results back within SQL Server as a varchar(8000) and then converting that to XML. This works perfectly. I want to parse this XML information out into it's individual values but continue to get null values. This is my first attempt in using XML on my SQL 2008 server so I know I'm missing a very trivial item.
I'm taking the response received and storing it into @XML.
SET @XML = CAST(@Response AS XML)
I'm next trying to pull out the Postal Code to get my results and receive a NULL or the wrong node.
Returns NULL
SELECT @XML.value('(/GeocodingResult/Results/Address/PostalCode) [1]', 'varchar(50)')
Returns "Copyright © 2010 Microsoft and its suppliers. All " (without the quotes)
SELECT @XML.value('(/) [1]', 'varchar(50)')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 XPath 错误 - 您的根节点是
GeocodingResponse
(不是GeocodingResult
),并且您一路上丢失了GeocodingResult
。尝试这个 XPath:
或这个 SQL XQuery:
Your XPath is wrong - your root node is
GeocodingResponse
(notGeocodingResult
), and you're missing aGeocodingResult
along the way.Try this XPath:
or this SQL XQuery: