转自 http://wakan.blog.51cto.com/blog/59583/7220
原文 http://www.edankert.com/defaultnamespaces.html
诸如“为什么用 XPath 的表达式进行查询,却没有返回所期望的结果?” 的问题通常都与命名空间(NameSpace)有关,而且绝大多数是与缺省命名空间(Default Namespace)有关。本文试图解释这个问题并针对三种流行的 XPath 实现给出解决方法:Jaxen、JAXP XPPathFactory 以及 XSLT。
内容列表
- 问题描述
- “前缀-命名空间”映射
- Jaxen 和 Dom4J
- Jaxen 和 XOM
- Jaxen 和 JDOM
- JAXP XPathFactory
- XSLT
- 结束语
- 资源
问题描述
'http://www.edankert.com/examples/' 命名空间中。
[url]http://www.edankert.com/examples/[/url]”。
“前缀-命名空间”映射
Jaxen 和 Dom4J
SAXReader reader = new SAXReader();
Document document = reader.read( "file:catalog.xml");
HashMap map = new HashMap ();
map.put( "edx", "http://www.edankert.com/examples/");
XPath xpath = new Dom4jXPath( "//edx:cd");
xpath.setNamespaceContext( new SimpleNamespaceContext ( map));
List nodes = xpath.selectNodes( document);
...
} catch ( JaxenException e) { // An error occurred parsing or executing the XPath ... } catch ( DocumentException e) {
// the document is not well-formed.
...
}
Jaxen 和 XOM
Builder builder = new Builder();
Document document = builder.build( "file:catalog.xml");
HashMap map = new HashMap ();
map.put( "edx", "http://www.edankert.com/examples/");
XPath xpath = new XOMXPath( "//edx:cd");
xpath.setNamespaceContext( new SimpleNamespaceContext ( map));
List nodes = xpath.selectNodes( document);
...
} catch ( JaxenException e) { // An error occurred parsing or executing the XPath ... } catch ( IOException e) {
// An error occurred opening the document
...
} catch ( ParsingException e) {
// An error occurred parsing the document
...
}
XOMXPath。创建方法是把 XPath 表达式传递给构造方法,然后就可以通过 XOM 使用 Jaxen 的 XPath 功能了。
Jaxen 和 JDOM
SAXBuilder builder = new SAXBuilder();
Document document = builder.build( "file:catalog.xml");
HashMap map = new HashMap ();
map.put( "edx", "http://www.edankert.com/examples/");
XPath xpath = new JDOMXPath( "//edx:cd");
xpath.setNamespaceContext( new SimpleNamespaceContext ( map));
List nodes = xpath.selectNodes( document);
...
} catch ( JaxenException e) { // An error occurred parsing or executing the XPath ... } catch ( IOException e) {
// An error occurred opening the document
...
} catch ( JDOMException e) {
// An error occurred parsing the document
...
}
SAXBuilder 创建了一个特定于 JDom 的
Document 对象。
JDOMXPath。
SimpleNamespaceContext 对象封装起来,传递给 XPath 引擎。
Document 对象作为本方法的输入参数。
JAXP XPathFactory
DocumentBuilderFactory domFactory = DocumentBuilderFactory .newInstance();
domFactory.setNamespaceAware( true);
DocumentBuilder builder = domFactory.newDocumentBuilder(); Document document = builder.parse( new InputSource ( "file:catalog.xml"));
XPathFactory factory = XPathFactory .newInstance();
XPath xpath = factory.newXPath();
xpath.setNamespaceContext( new NamespaceContext () {
public String getNamespaceURI( String prefix) {
if ( prefix.equals( "edx")) {
return "http://www.edankert.com/examples/";
} else if ...
...
}
return XPathConstants .NULL_NS_URI;
}
public String getPrefix( String namespaceURI) {
if ( namespaceURI.equals( "http://www.edankert.com/examples/")) {
return "edx";
} else if ...
...
}
return null;
}
public Iterator getPrefixes( String namespaceURI) {
ArrayList list = new ArrayList ();
if ( namespaceURI.equals( "http://www.edankert.com/examples/")) {
list.add( "edx");
} else if ...
...
}
return list.iterator();
}
});
Object nodes = xpath.evaluate( "//edx:cd", document.getDocumentElement(),
XPathConstants .NODESET);
...
} catch ( ParserConfigurationException e) {
...
} catch ( XPathExpressionException e) {
...
} catch ( SAXException e) {
...
} catch ( IOException e) {
...
}
DocumentBuilderFactory 创建一个
org.w3c.dom.Document 对象,确保启用了 namespace 处理功能。
NamespaceContext 接口,该接口目前还没有默认实现类。这就意味着要实现 getNamespaceURI、getPrefix 和getPrefixes 方法,并确保这些方法能返回正确的值,包括“xmlns”和“xml”前缀所对应的命名空间的 URI 值。
NamespaceContext 对象传递给
XPath 引擎后,就可以通过
evaluate 方法来查询 XPath 表达式所对应的元素:使用上文中提到的 XPath 表达式,并使用 Document 的根节点作为输入入参数,并接收一个
NodeList 对象作为返回结果。
XSLT
<xsl:template match="//edx:cd" xmlns:edx="http://www.edankert.com/examples/">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
//edx:cd”相匹配的
xsl:template,能得到与上文其他例子相同的输出结果。
结束语
<examples:cd>
<examples:artist>Sufjan Stevens</examples:artist>
<examples:title>Illinois</examples:title>
<examples:src> [url]http://www.sufjan.com/</[/url] examples:src>
</examples:cd>
<examples:cd>
<examples:artist>Stoat</examples:artist>
<examples:title>Future come and get me</examples:title>
<examples:src> [url]http://www.stoatmusic.com/</[/url] examples:src>
</examples:cd>
<examples:cd>
<examples:artist>The White Stripes</examples:artist>
<examples:title>Get behind me satan</examples:title>
<examples:src> [url]http://www.whitestripes.com/</[/url] examples:src>
</examples:cd>
</examples:catalog>
资源
- Extensible Markup Language (XML) 1.0 (Third Edition)
[url]http://www.w3.org/TR/REC-xml/[/url] - Namespaces in XML
[url]http://www.w3.org/TR/REC-xml-names/[/url] - XML Path Language (XPath) Version 1.0
[url]http://www.w3.org/TR/xpath[/url] - XSL Transformations (XSLT) Version 1.0
[url]http://www.w3.org/TR/xslt[/url] - dom4j
[url]http://www.dom4j.org/[/url] - XOM
[url]http://www.xom.nu/[/url] - JDOM
[url]http://www.jdom.org/[/url] - Jaxen
[url]http://www.jaxen.org/[/url]
2488



被折叠的 条评论
为什么被折叠?



