获取 XML 字段中的节点数 XQuery SQL Server 2008
我正在尝试获取 XML 字段中的节点数。但我总是看到 0 结果。这是我的查询的样子。
DECLARE @XmlTable TABLE (XmlResult XML)
INSERT INTO @XmlTable EXECUTE [dbo].usp_GetBooks @EditionId=400
--select * from @XmlTable
SELECT
--Count number of nodes
COUNT(*) AS BooksCount
FROM
(
SELECT XmlResult FROM @XmlTable
) AS XmlTable(XmlColumn)
CROSS APPLY XmlColumn.nodes('./books/book') XmlTableFunction(XmlColumn2);
我的 XML 看起来像:
<Version number ="1">
<books>
<book>
<name> </name>
<author></author>
</book>
<book>
<name> </name>
<author></author>
</book>
</books>
</Version>
I am trying to get the count of nodes in an XML field. but I always see 0 as the result. Here is how my query looks like.
DECLARE @XmlTable TABLE (XmlResult XML)
INSERT INTO @XmlTable EXECUTE [dbo].usp_GetBooks @EditionId=400
--select * from @XmlTable
SELECT
--Count number of nodes
COUNT(*) AS BooksCount
FROM
(
SELECT XmlResult FROM @XmlTable
) AS XmlTable(XmlColumn)
CROSS APPLY XmlColumn.nodes('./books/book') XmlTableFunction(XmlColumn2);
My XML Looks like :
<Version number ="1">
<books>
<book>
<name> </name>
<author></author>
</book>
<book>
<name> </name>
<author></author>
</book>
</books>
</Version>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的 XPath 表达式是错误的 - 试试这个:
或者更简单:
I think your XPath expression is wrong - try this instead:
Or even simpler:
对我来说,您提供的 XML 模式
Exist 方法也非常有用。我使用 NULLIF 将 0 更改为 NULL (它是位,所以需要使用 SUM 进行 CAST)
编辑,更新后
您发布的 XML 也是错误的。
您没有正确指定根音:
Works for me with the XML pattern you give
Exist method is quite useful too. I use NULLIF to change 0 to NULL (it is bit so would need CAST with SUM)
Edit, after update
The XML you posted is wrong too.
You are not specifying the root note correctly: