0

It is legal to have XPath that is "/root/name[bogus = 'dave']" where the bogus node does not exist. It will return null on a SelectSingleNode.

But XPath that is "/root/name[5]" where there are only 4 name nodes under root throws an exception. (At least it does in .NET).

What is a good way to test and see if an index value is valid?

thanks - dave

4

1 回答 1

1

在 XPath 1.0 中,您可以计算这个 XPath 表达式:

bool(/root/name[5])

<root>如果元素有第五个<name>孩子,这将返回 true 。

但正如其他人所说,XPath 本身不应引发尝试访问不存在的节点的异常。但是,如果您尝试取消引用空值,.NET 库可能会引发异常。

于 2013-06-05T14:41:21.753 回答