关于使用Saxon使用默认命名空间对xml进行xpath解析 [英] Regarding xpath parsing of xml with default namespace using Saxon

查看:549
本文介绍了关于使用Saxon使用默认命名空间对xml进行xpath解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一些与使用saxon对默认命名空间进行xpath解析相关的信息。我使用Saxon-HE-9.5.1-3.jar在我的代码中使用xpath 2功能。在类路径中包含saxon库之后,我面临着使用默认命名空间解析XML文档的xpath的问题。

I wanted some info related to xpath parsing of default namespace using saxon. I am using the Saxon-HE-9.5.1-3.jar to use xpath 2 features in my code. After including the saxon library in the class path, I am facing a issue parsing xpath for XML documents with default namespaces.

使用以下示例XML:

<?xml version="1.0" encoding="utf-8"?>
<RESPONSE  xmlns="http://www.abc.com/" responseCode="200">
  <HEADER>
    <HITS>100</HITS>
  </HEADER>
</RESPONSE>

有效的XPATH:/ RESPONSE / HEADER / HITS

以下是有效且不起作用的情况:

Below are the cases where it works and does not work:


  1. XPATH Works:当没有指定名称空间时

    示例:< RESPONSE responseCode =200>

  1. "XPATH Works" : When no namespace is specified
    Example : <RESPONSE responseCode="200">

XPATH Works:当给出带前缀的名称空间时

示例:< RESPONSE xmlns:res =http://www.abc.com/responseCode =200>

"XPATH Works" : When namespace with prefix is given
Example : <RESPONSE xmlns:res="http://www.abc.com/" responseCode="200">

XPATH不起作用:当给出带有out前缀的默认命名空间时示例:< RESPONSE xmlns =http://www.abc.com/responseCode =200 >

"XPATH does not work" : When default namespace with out prefix is given Example : <RESPONSE xmlns="http://www.abc.com/" responseCode="200">

你能否帮我解释为什么撒克逊人不会对待任何命名空间和默认命名空间以不同的方式?
另外,我如何解决为具有默认命名空间的文档执行xpath的情况。

Can you please help me out on why saxon treats no namespace and default namespace in a different way ? Also how do I solve the case of doing xpath for documents with default namespace.

以下是我的代码块:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import net.sf.saxon.xpath.XPathEvaluator;
import net.sf.saxon.xpath.XPathFactoryImpl;

.
.
.

DocumentBuilder builder;
Document doc;

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
builder = domFactory.newDocumentBuilder();
doc = builder.parse(new ByteArrayInputStream(b, 0, size));

XPathFactory factory = XPathFactoryImpl.newInstance(XPathConstants.DOM_OBJECT_MODEL);
XPathEvaluator xpathCompiler = (XPathEvaluator) factory.newXPath();
XPathExpression expr = xpathCompiler.compile(xpath);
NodeList childNodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

谢谢&问候
Pratap

Thanks & Regards Pratap

推荐答案

这不是特定于Saxon的,而是命名空间如何工作的基本部分。在您的示例1和示例2中, RESPONSE 元素不在命名空间中,但在案例3中它(及其所有后代)都在 http:/中/www.abc.com / 命名空间。使用 javax.xml.xpath API,如果希望能够匹配节点,则需要定义 NamespaceContext 一个特定的命名空间,或者因为你在XPath 2.0中,你可以使用 *:localName 表示法匹配具有给定本地名称的所有节点,而不管它们的名称空间。

This is not Saxon-specific but rather a fundamental part of how namespaces work. In your examples 1 and 2 the RESPONSE element is not in a namespace but in case 3 it (and all its descendants) are in the http://www.abc.com/ namespace. With the javax.xml.xpath API you need to define a NamespaceContext if you want to be able to match nodes in a specific namespace, or since you're in XPath 2.0 you can use the *:localName notation to match all nodes with a given local name regardless of their namespace.

/*:RESPONSE/*:HEADER/*:HITS

这篇关于关于使用Saxon使用默认命名空间对xml进行xpath解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆