【问题标题】:Using XPath functions in libxml在 libxml 中使用 XPath 函数
【发布时间】:2014-03-17 15:39:37
【问题描述】:

我正在尝试使用 libxml 2.8.0 评估某些 XPath 表达式,并收到带有函数的错误消息。我不清楚 libxml 是实现 XPath 规范的全部还是子集。是否有任何资源清楚地表明实现了什么以及如何使用它?

在我的具体情况下,我试图评估的一个表达式是:

/TemporalInformation/RecipeInformation[fn:exists(./ActionToActionRelation[fn:contains(./@actionA,'cook') and fn:exists(./IngredientA[fn:contains(./@classes,'onion')])])]

我正在通过 XML::LibXML Perl 模块使用 libxml,但我也尝试使用 xmlsoft.org 提供的the xpath1.c example 工具来表达我的表达式,并得到相同的错误消息:

$ ./xpath-tester data/fk.xml "/TemporalInformation/RecipeInformation[fn:exists(.> /ActionToActionRelation[fn:contains(./@actionA,'cook') and fn:exists(./IngredientA[fn:contains(./@classes,'onion')])])]" "fn=http://www.w3.org/2005/xpath-functions"
xmlXPathCompOpEval: function contains not found
XPath error : Unregistered function
xmlXPathCompOpEval: function exists not found
XPath error : Unregistered function
XPath error : Invalid expression
XPath error : Stack usage errror
Error: unable to evaluate xpath expression "/TemporalInformation/RecipeInformation[fn:exists(./ActionToActionRelation[fn:contains(./@actionA,'cook') and fn:exists(./IngredientA[fn:contains(./@classes,'onion')])])]"
Usage: ./xpath-tester <xml-file> <xpath-expr> [<known-ns-list>]
where <known-ns-list> is a list of known namespaces
in "<prefix1>=<href1> <prefix2>=href2> ..." format

我尝试了使用和不使用 fn 命名空间,使用 xpath.c 和我的 Perl 脚本,得到了相同的结果。

【问题讨论】:

    标签: xpath libxml2 xml-libxml


    【解决方案1】:

    libxml XPath 实现只是 XPath 1.0,所以像 exists 这样的仅限 2.0 的函数不可用。在 1.0 中,您只有几个 core functions - 有 starts-with 但没有 ends-with,没有正则表达式支持,没有日期处理等,没有强类型,也没有像 iffor.

    所以回答你的问题,是的,它确实支持整个 XPath,但只支持 XPath 1.0。

    【讨论】:

    • 感谢您提供的信息。由于它也为contains 提供了错误消息,所以我没有发生这种情况。经过测试,我确认如果表达式没有使用existscontains 的错误消息就会消失。够奇怪的!
    • @scozy 它会抱怨fn:contains,但不会抱怨contains 没有fn: 前缀。就 XPath 1.0 而言,fn:contains 是未知的扩展功能,而不是核心功能。
    猜你喜欢
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多