gpt4 book ai didi

java - 命名空间感知 XML 解析 - 不在根标记中的映射

转载 作者:行者123 更新时间:2023-11-29 07:15:22 25 4
gpt4 key购买 nike

我正在使用 Xerces 2.9.1 执行一些 XML 解析。 XML 包含命名空间,我将 Xerces 配置为可识别命名空间。

我定义了两个前缀,reccom。 rec 在根标签中定义,com 在使用它的每个节点中声明。

<rec:root xmlns:rec="...">
<rec:dummy ...>
<com:item xmlns:com="..." />
...

解析以下 XPath 表达式时出现异常:

/rec:root/rec:dummy/com:item

XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: com

我在博客上发现了这个,它谈论 Xalan,但我观察到 Xerces 具有相同的行为。

Well, you might argue a design flaw, since it only uses the mapping found at the node passed to it, i.e., to top level namespace declarations in our case. So, let us move the namespace declaration to the top level, to get the following XML input

Source

xmlns:com 移动到根标签可以解决问题,但我无法控制该文件。

最佳答案

xpath 前缀与文档中使用的前缀没有关系。您需要提供一个映射到 xpath 处理程序的“命名空间前缀”。使用标准的 Java DOM 工具,您将提供 NamespaceContext到 XPath 实例。

关于java - 命名空间感知 XML 解析 - 不在根标记中的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10011150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com