在此 XML 文件中,映射由类似 xmlns:books="http://univNaSpResolver/booklist" 这样的 xmlns 属性或 xmlns="http://univNaSpResolver/book"(默认名称空间)提供。
提供名称空间解析的必要性
如果 XML 使用了名称空间,若不提供 NamespaceContext,那么 XPath 表达式将会失效。清单 2 中的示例 0 充分展示了这一点。其中的 XPath 对象在所加载的 XML 文档之上构建和求值。首先,尝试不用任何名称空间前缀(result1)编写此表达式。之后,再用名称空间前缀(result2)编写此表达式。
清单 2. 无名称空间解析的示例 0
private static void example0(Document example)
throws XPathExpressionException, TransformerException {
sysout("\n*** Zero example - no namespaces provided ***");
XPath xPath = XPathFactory.newInstance().newXPath();
...
NodeList result1 = (NodeList) xPath.evaluate("booklist/book", example,
XPathConstants.NODESET);
...
NodeList result2 = (NodeList) xPath.evaluate(
"books:booklist/science:book", example, XPathConstants.NODESET);
...
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
输出如下所示。
清单 3. 示例 0 的输出
*** Zero example - no namespaces provided ***
First try asking without namespace prefix:
--> booklist/book
Result is of length 0
Then try asking with namespace prefix:
--> books:booklist/science:book
Result is of length 0
The expression does not work in both cases.
1.
2.
3.
4.
5.
6.
7.
8.
在两种情况下,XPath 求值并不返回任何节点,而且也没有任何异常。XPath 找不到节点,因为缺少前缀到 URI 的映射。
硬编码的名称空间解析
也可以以硬编码的值来提供名称空间,类似于 清单 4 中的类:
清单 4. 硬编码的名称空间解析
public class HardcodedNamespaceResolver implements NamespaceContext {
/**
* This method returns the uri for all prefixes needed. Wherever possible
* it uses XMLConstants.
*
* @param prefix
* @return uri
*/
public String getNamespaceURI(String prefix) {
if (prefix == null) {
throw new IllegalArgumentException("No prefix provided!");
} else if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
return "http://univNaSpResolver/book";
} else if (prefix.equals("books")) {
return "http://univNaSpResolver/booklist";
} else if (prefix.equals("fiction")) {
return "http://univNaSpResolver/fictionbook";
} else if (prefix.equals("technical")) {
return "http://univNaSpResolver/sciencebook";
} else {
return XMLConstants.NULL_NS_URI;
}
}
public String getPrefix(String namespaceURI) {
// Not needed in this context.
return null;
}
public Iterator getPrefixes(String namespaceURI) {
// Not needed in this context.
return null;
}
}
private static void example1(Document example)
throws XPathExpressionException, TransformerException {
sysout("\n*** First example - namespacelookup hardcoded ***");
XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setNamespaceContext(new HardcodedNamespaceResolver());
...
NodeList result1 = (NodeList) xPath.evaluate(
"books:booklist/technical:book", example,
XPathConstants.NODESET);
...
NodeList result2 = (NodeList) xPath.evaluate(
"books:booklist/fiction:book", example, XPathConstants.NODESET);
...
String result = xPath.evaluate("books:booklist/technical:book/:author",
example);
...
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
如下是此示例的输出。
清单 6. 示例 1 的输出
*** First example - namespacelookup hardcoded ***
Using any namespaces results in a NodeList:
--> books:booklist/technical:book
Number of Nodes: 1
Michael Schmidt
--> books:booklist/fiction:book
Number of Nodes: 2
Johann Wolfgang von GoetheJohann Wolfgang von Goethe
The default namespace works also:
--> books:booklist/technical:book/:author
Michael Schmidt
名称空间及其前缀均存档在此 XML 文件内,因此可以从那里使用它们。实现此目的的最为简单的方式是将这个查找指派给该文档。
清单 7. 从文档直接进行名称空间解析
public class UniversalNamespaceResolver implements NamespaceContext {
// the delegate
private Document sourceDocument;
/**
* This constructor stores the source document to search the namespaces in
* it.
*
* @param document
* source document
*/
public UniversalNamespaceResolver(Document document) {
sourceDocument = document;
}
/**
* The lookup for the namespace uris is delegated to the stored document.
*
* @param prefix
* to search for
* @return uri
*/
public String getNamespaceURI(String prefix) {
if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
return sourceDocument.lookupNamespaceURI(null);
} else {
return sourceDocument.lookupNamespaceURI(prefix);
}
}
/**
* This method is not needed in this context, but can be implemented in a
* similar way.
*/
public String getPrefix(String namespaceURI) {
return sourceDocument.lookupPrefix(namespaceURI);
}
public Iterator getPrefixes(String namespaceURI) {
// not implemented yet
return null;
}
}
*** Second example - namespacelookup delegated to document ***
Try to use the science prefix: no result
--> books:booklist/science:book
The resolver only knows namespaces of the first level!
To be precise: Only namespaces above the node, passed in the constructor.
The fiction namespace is such a namespace:
--> books:booklist/fiction:book
Number of Nodes: 2
Johann Wolfgang von GoetheJohann Wolfgang von Goethe
The default namespace works also:
--> books:booklist/fiction:book[1]/:author
Johann Wolfgang von Goethe
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
正如输出所示,在 book 元素上声明的、具有前缀 science 的名称空间并未被解析。求值方法抛出了一个 XPathExpressionException。要解决这个问题,需要从文档提取节点 science:book 并将此节点用作代表(delegate)。但是这将意味着对此文档要进行额外的解析,而且也不优雅。
public class UniversalNamespaceCache implements NamespaceContext {
private static final String DEFAULT_NS = "DEFAULT";
private Map prefix2Uri = new HashMap();
private Map uri2Prefix = new HashMap();
/**
* This constructor parses the document and stores all namespaces it can
* find. If toplevelOnly is true, only namespaces in the root are used.
*
* @param document
* source document
* @param toplevelOnly
* restriction of the search to enhance performance
*/
public UniversalNamespaceCache(Document document, boolean toplevelOnly) {
examineNode(document.getFirstChild(), toplevelOnly);
System.out.println("The list of the cached namespaces:");
for (String key : prefix2Uri.keySet()) {
System.out
.println("prefix " + key + ": uri " + prefix2Uri.get(key));
}
}
/**
* A single node is read, the namespace attributes are extracted and stored.
*
* @param node
* to examine
* @param attributesOnly,
* if true no recursion happens
*/
private void examineNode(Node node, boolean attributesOnly) {
NamedNodeMap attributes = node.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
storeAttribute((Attr) attribute);
}
if (!attributesOnly) {
NodeList chields = node.getChildNodes();
for (int i = 0; i < chields.getLength(); i++) {
Node chield = chields.item(i);
if (chield.getNodeType() == Node.ELEMENT_NODE)
examineNode(chield, false);
}
}
}
/**
* This method looks at an attribute and stores it, if it is a namespace
* attribute.
*
* @param attribute
* to examine
*/
private void storeAttribute(Attr attribute) {
// examine the attributes in namespace xmlns
if (attribute.getNamespaceURI() != null
&& attribute.getNamespaceURI().equals(
XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
// Default namespace xmlns="uri goes here"
if (attribute.getNodeName().equals(XMLConstants.XMLNS_ATTRIBUTE)) {
putInCache(DEFAULT_NS, attribute.getNodeValue());
} else {
// The defined prefixes are stored here
putInCache(attribute.getLocalName(), attribute.getNodeValue());
}
}
}
private void putInCache(String prefix, String uri) {
prefix2Uri.put(prefix, uri);
uri2Prefix.put(uri, prefix);
}
/**
* This method is called by XPath. It returns the default namespace, if the
* prefix is null or "".
*
* @param prefix
* to search for
* @return uri
*/
public String getNamespaceURI(String prefix) {
if (prefix == null || prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
return prefix2Uri.get(DEFAULT_NS);
} else {
return prefix2Uri.get(prefix);
}
}
/**
* This method is not needed in this context, but can be implemented in a
* similar way.
*/
public String getPrefix(String namespaceURI) {
return uri2Prefix.get(namespaceURI);
}
public Iterator getPrefixes(String namespaceURI) {
// Not implemented
return null;
}
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
请注意在代码中有一个调试输出。每个节点的属性均被检查和存储。但子节点不被检查,因为构造函数内的布尔值 toplevelOnly 被设置为 true。如果此布尔值被设为 false,那么子节点的检查将会在属性存储完毕后开始。有关此代码,有一点需要注意:在 DOM 中,第一个节点代表整个文档,所以,要让元素 book 读取这些名称空间,必须访问子节点刚好一次。
*** Third example - namespaces of toplevel node cached ***
The list of the cached namespaces:
prefix DEFAULT: uri http://univNaSpResolver/book
prefix fiction: uri http://univNaSpResolver/fictionbook
prefix books: uri http://univNaSpResolver/booklist
Try to use the science prefix:
--> books:booklist/science:book
The cache only knows namespaces of the first level!
The fiction namespace is such a namespace:
--> books:booklist/fiction:book
Number of Nodes: 2
Johann Wolfgang von GoetheJohann Wolfgang von Goethe
The default namespace works also:
--> books:booklist/fiction:book[1]/:author
Johann Wolfgang von Goethe
*** Fourth example - namespaces all levels cached ***
The list of the cached namespaces:
prefix science: uri http://univNaSpResolver/sciencebook
prefix DEFAULT: uri http://univNaSpResolver/book
prefix fiction: uri http://univNaSpResolver/fictionbook
prefix books: uri http://univNaSpResolver/booklist
Now the use of the science prefix works as well:
--> books:booklist/science:book
Number of Nodes: 1
Michael Schmidt
The fiction namespace is resolved:
--> books:booklist/fiction:book
Number of Nodes: 2
Johann Wolfgang von GoetheJohann Wolfgang von Goethe
The default namespace works also:
--> books:booklist/fiction:book[1]/:author
Johann Wolfgang von Goethe
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
结束语
实现名称空间解析,有几种方式可供选择,这些方式大都好于硬编码的实现方式:
•如果示例很小并且所有名称空间均位于顶部元素内,指派到此文档的方式将会十分有效。
•如果 XML 文件较大且具有深层嵌套和多个 XPath 求值,最好是缓存名称空间的列表。
•但是如果您无法控制 XML 文件,并且别人可以发送给您任何前缀,最好是独立于他人的选择。您可以编码实现您自己的名称空间解析,如示例 1 (HardcodedNamespaceResolver)所示,并将它们用于您的 XPath 表达式。
在上述这些情况下,解析自此 XML 文件的 NamespaceContext 能够让您的代码更少、并且更为通用。