英文:
how to query the result of fn:analyze-string()?
问题
在XQuery(在使用Saxon 10.something的BaseX中以及在Oxygen中使用Saxon 9.9.1.7的XSLT中),我尝试迭代*fn:analyze-string()*的匹配项,使用的是以下代码:fn:analyze-string($term, $pattern)/match
,尽管函数本身返回带有<match>和<non-match>子节点的典型元素,但没有给我任何输出。根据定义,该函数返回“[...]一个XML结构[...]”(参考)。因此,应该能够像这样查询它,给定的表达式应该有效。
由于它没有起作用,我尝试了fn:analyze-string($term, $pattern)/*[fn:local-name(.) eq 'match']
...这样做起作用了。然而,将星号和括号中的条件替换为child::match
等则不起作用。
我的问题是:为什么会这样?有什么区别?我在这里漏掉了什么?在所有情况下,子节点都是通过名称match来查询的。那么,为什么只有带有星号的版本给我带来了结果?
英文:
In XQuery (in BaseX which uses Saxon 10.something as well as in XSLT with Saxon 9.9.1.7 within Oxygen), I tried to iterate the matches of fn:analyze-string() by
fn:analyze-string($term, $pattern)/match
which did not give me any output although the function itself returned the typical element with <match> and <non-match> childnodes. According to the definition, the function returns "[...] an XML structure [...]" (cf.). Hence, one should be able to query it like that and the given expression should work.
Since it did not, I then tried fn:analyze-string($term, $pattern)/*[fn:local-name(.) eq 'match']
...and it worked. Replacing the asterisk and the condition in the brackets with child::match
etc. didn't, however.
My question is: Why? What is the difference; what did I miss here? In all cases, the child nodes are queried by the name match. So, why does only the version with the asterisk get me the results?
答案1
得分: 4
`match` 元素位于一个命名空间中,具体来说是 "fn" 命名空间 (`http://www.w3.org/2005/xpath-functions`)。因此,未经限定的名称测试 `match` 将无法找到它们,除非这恰好是元素的默认命名空间(默认情况下不会是这样)。您应该使用 `fn:match` 进行匹配(其中 `fn` 绑定到正确的命名空间),或者使用 `*:match`。
这个问题可能是关于 XSLT、XPath 和 XQuery 在 StackOverflow 上被问得最频繁的问题之一。然而,由于提问的方式各式各样,症状也各不相同,很难将其归类为重复问题。但如果您搜索 "XPath 默认命名空间",您会发现有成千上万的其他用户(上次统计有 2137 人)陷入同样的错误。
英文:
The match
elements are in a namespace, specifically the "fn" namespace (http://www.w3.org/2005/xpath-functions
). So the unqualified name test match
won't find them unless this happens to be the default namespace for elements (which won't be the case by default). You should match them using fn:match
(where fn
is bound to the right namespace), or *:match
.
This question is probably the most commonly asked question on StackOverflow about XSLT, XPath, and XQuery. However, because it's asked in so many different ways and the symptoms vary so widely, it's hard to close it as a duplicate. But if you search for "XPath default namespace" you'll find thousands of other users (2137 at the last count) falling into the same error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论