不使用 local-name() 或 name() 函数的 XPath [英] XPath without using local-name() or name() functions

查看:27
本文介绍了不使用 local-name() 或 name() 函数的 XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用 XPath 从下面的 XML 解析 oprResult/@code.XPath 表达式

I have to parse oprResult/@code from below XML using XPath. The XPath expression

//*[local-name()='oprResult']/@code

按预期工作,但是我无法使用 namelocal-name 函数,因为 '(' ')' 用作分隔符我的解析函数.

is working as expected, BUT I could not use name or local-name functions as '(' ')' are used as delimiter in my parsing function.

是否可以在没有 local-name 的情况下解析 oprResult?

Is it possible to parse oprResult without local-name?

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ChangeResponse xmlns="http://www.example.com" code="0" message="Success">
            <oprResult code="0" message="Success"/>
        </ChangeResponse>
    </soap:Body>
</soap:Envelope>

推荐答案

是否可以在没有本地名称的情况下解析 oprResult?

*

不仅可能在不使用local-name()的情况下选择一个XML元素,而且它实际上是首选.

Yes*

Not only is it possible to select an XML element without using local-name(), it is actually preferred.

您看到带有诸如

//*[local-name()='oprResult']

是命名空间声明不是XPath的固有部分;命名空间和命名空间前缀声明出现在使用 XPath 库的系统或语言的更大上下文中.

is that namespace declarations are not an intrinsic part of XPath; namespace and namespace prefix declarations occur in the greater context of the system or language using an XPath library.

如果你不能让你的系统容忍 local-name() 的括号,如果 //oprResult/@code 不适合,或者如果你只是对正确使用命名空间而不是打败它们感兴趣,弄清楚如何声明命名空间前缀,例如 ex: 对于 http://www.example.com 命名空间,并在 XPath 中使用命名空间前缀.继续阅读以了解如何...

If you cannot make your system tolerate the parentheses of local-name(), if //oprResult/@code doesn't suite, or if you are simply interested in using namespaces properly rather than defeating them, figure out how to declare a namespace prefix, say ex: for the http://www.example.com namespace, and use the namespace prefix in the XPath. Read on for how...

您没有说明您用于 XPath 的语言和/或库是什么,但这里有几个示例可以让您了解要查找的内容...

You do not say what the language and/or library you're using for XPath, but here are a few examples to give a feel for what to look for...

XSLT:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                xmlns:ex="http://www.example.com">
   ...

Python (LibXML):

my $xc = XML::LibXML::XPathContext->new($xhtml_doc);
$xc->registerNs('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xc->registerNs('ex', 'http://www.example.com');

Javas (SAX):

NamespaceSupport support = new NamespaceSupport();
support.pushContext();
support.declarePrefix("soap", "http://schemas.xmlsoap.org/soap/envelope/");
support.declarePrefix("ex", "http://www.example.com");

一旦您声明了名称空间前缀,您的 XPath 就可以是:

/soap:Envelope/soap:Body/ex:ChangeResponse/oprResult/@code


*...但原因不应该是避免使用括号,因为它们在解析函数中用作分隔符".首先,如果 XPath 解析器在嵌入括号时遇到问题,请获取新的 XPath 解析器;如果使用 XPath 库的工具阻止您使用括号,请获取一个新工具(或深入调查以确保该工具没有得到应有的使用).其次,正如@predi 在问题评论中所观察到的,如果只有一个 oprResult 元素并且您的响应足够小以至于您不介意效率低下,那么在您的情况下,您可以使用没有名称空间的 XPath: //oprResult/@code


* ...but the reason should not be to avoid parentheses because they "are used as delimiters in a parsing function." First of all, if an XPath parser is having trouble with embedded parens, get a new XPath parser; if a tool that uses an XPath library is preventing you from using parens, get a new tool (or investigate deeper to be sure the tool is not being used as well as it can be). Secondly, as @predi observed in the question comments, if there's only a single oprResult element and your response is small enough that you don't mind the inefficiency, in your case you can use an XPath without namespaces: //oprResult/@code

这篇关于不使用 local-name() 或 name() 函数的 XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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