XPath是否明确允许未定义的变量?

huangapple go评论50阅读模式
英文:

Does xpath explicitly allow undefined variables?

问题

I was trying to pull some data out of an XML file using xmlstarlet, but xmlstarlet sel -t -v '//div[contains(@class,"addresses")]/a' was getting me a lot more than I expected. I finally worked out - with the help of replacing -v with -c to debug the xpath - that it was because the single quotes around 'addresses' were vanishing because the whole argument is single-quoted: replacing that with "addresses" worked as expected. What I'm really confused about is quite what contains(@class,addresses) does - because xmlstarlet didn't flag it as an error or warn about it, even though I presume it's treated as an undefined variable. I checked the source in https://github.com/fishjam/xmlstarlet/blob/master/src/xml_select.c but I can't spot a 'warnings' flag - should there be something like that, or are xpaths defined to allow this?

英文:

I was trying to pull some data out of an XML file using xmlstarlet, but

xmlstarlet sel -t  -v '//div[contains(@class,'addresses')]/a'

was getting me a lot more than I expected.

I finally worked out - with the help of replacing -v with -c to debug the xpath - that it was because the single quotes around 'addresses' were vanishing because the whole argument is single-quoted: replacing that with "addresses" worked as expected.

What I'm really confused about is quite what contains(@class,addresses) does - because xmlstarlet didn't flag it as an error or warn about it, even though I presume it's treated as an undefined variable.

I checked the source in https://github.com/fishjam/xmlstarlet/blob/master/src/xml_select.c but I can't spot a 'warnings' flag - should there be something like that, or are xpaths defined to allow this?

答案1

得分: 2

contains(@class,addresses) 中,addresses 选择上下文节点(即在 //div[contains(@class,addresses)] 中,div 元素中的断言 [contains(@class,addresses)] 所属的可能的子元素名为 addresses 的元素;一个变量(引用)将被写作 $addresses

英文:

In contains(@class,addresses), addresses selects (possible) child elements named addresses of the context node (i.e. in //div[contains(@class,addresses)] the div element the predicate [contains(@class,addresses)] belongs to); a variable (reference) would be written as $addresses.

huangapple
  • 本文由 发表于 2023年5月7日 21:44:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定