java xerces xpath因命名空间而失败 [英] java xerces xpath fails with namespace

查看:81
本文介绍了java xerces xpath因命名空间而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序用xerces运行xpath 1_2_7

XPathFactory factory = XPathFactory.newInstance();

XPath xPath = factory.newXPath( ); $ / b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ FileInputStream(" a.xml"))));


如果a.xml是

<?xml

version =" 1.0"?>< root>< parent>< son> theTextValue< / son>< / parent>< / root>


和strXpr是/ root / parent / son / text()

我得到了正确的值theTextValue。


但是如果父是带有命名空间,那么作为肥皂信息,它总是失败,

eg.:<?xml version =" 1.0"?>< soap:Envelope

xmlns:soap = http://schemas.xmlsoap.org/soap/envelope/"

xmlns:xsd =" http://www.w3.org/2001/XMLSchema"

xmlns:xsi =" http://www.w3.org/2001/XMLSchema-in stance">< soap:Body>< son> theValue< / son>

< / soap:Body>< / soapEnvelope>


strXpr是/ soap:信封/肥皂:身体/儿子/文字()


返回空字符串...


任何建议欢迎。


谢谢。

I have a simple program to run xpath with xerces 1_2_7

XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
XPathExpression xp = xPath.compile(strXpr);
System.out.println(xp.evaluate(new InputSource(new
FileInputStream("a.xml"))));

if a.xml is
<?xml
version="1.0"?><root><parent><son>theTextValue</son></parent></root>

and strXpr is /root/parent/son/text()
I got correct value back "theTextValue".

But if parent is with namespace, such as soap message, it always fails,
eg.:<?xml version="1.0"?><soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><son>theValue</son>
</soap:Body></soapEnvelope>

strXpr is /soap:Envelope/soap:Body/son/text()

empty string was returned...

Any suggestion are welcome.

Thanks.

推荐答案

XPath是名称空间敏感的。要正确搜索命名空间文档,

必须在XPath中使用前缀,并提供从那些

前缀到相应命名空间URI的绑定。


如果你真的坚持做一个名称空间不敏感的搜索,那么通过使用诸如node()[name()=" foo"]之类的kluge-around可以实现
。 ..但是

真的不推荐。使用命名空间是因为它们是有意义的

区别。不要试图忽略或绕过它们。
XPath is namespace-sensitive. To correctly search a namespaced document,
you must use prefixes in your XPath and provide bindings from those
prefixes to the appropriate namespace URIs.

If you really insist on doing a namespace-insensitive search, it''s
possible by using kluge-arounds such as node()[name()="foo"] ... but
REALLY not recommended. Namespaces are used because they''re a meaningful
distinction. Don''t attempt to ignore or bypass them.


请您更详细一点吗?如何在xpath中指定前缀?


例如。我的xml文件是:

?xml version =''1.0''encoding =''UTF-8''?>

< soap:Envelope xmlns:soap =" http://schemas.xmlsoap.org/soap/envelope/"

xmlns:xsd =" http://www.w3.org/2

001 / XMLSchema"

xmlns:xsi =" http://www.w3.org/2001/XMLSchema-instance">< soap:Body> testtext< / soap:Body> ;

< / soap:Envelope>


和我的xpath是:

// soap:Envelope / soap:Body / text()



//信封/正文/文字()


全部没有结果....


谢谢。

Would you please more detail? How to specify prefix in xpath?

eg. my xml file is:
?xml version=''1.0'' encoding=''UTF-8''?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2
001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body>testtext</soap:Body>
</soap:Envelope>

and my xpath is:
//soap:Envelope/soap:Body/text()
or
//Envelope/Body/text()

all no result back....

Thanks.


jacksu写道:
和我的xpath是:
// soap:信封/肥皂:正文/文字()

//信封/正文/文字()
and my xpath is:
//soap:Envelope/soap:Body/text()
or
//Envelope/Body/text()




第二个不行。第一个会,*如果*你告诉你的XPath

处理器,soap:前缀映射到

" http://schemas.xmlsoap.org/肥皂/信封/


如何做到这一点取决于处理器。如果你在XSLT样式表中使用XPath

,你只需要确保soap:已经

正确地声明为命名空间。通过执行XPath的语句继承

;通常的做法是在顶层xsl:stylesheet

元素中定义大多数命名空间,以确保它们在整个文档中可用。


如果您使用某种类型的XPath API,请检查其文档以找出

如何告诉它前缀和名称空间URI之间的映射。



The second won''t work. The first will, *if* you''ve told your XPath
processor that the soap: prefix maps to
"http://schemas.xmlsoap.org/soap/envelope/"

How you do that depends on the processor. If you''re using the XPath
within an XSLT stylesheet, you just need to make sure soap: has been
properly declared as a namespace at a point where it will be inherited
by the statement which is executing the XPath; the usual practice is to
define most namespaces all the way up at the top-level xsl:stylesheet
element to make sure they''re available throughout the document.

If you''re using an XPath API of some sort, check its docs to find out
how to tell it the mapping between prefixes and namespace URIs.


这篇关于java xerces xpath因命名空间而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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