如何获取具有不同命名空间的元素 [英] How to get an element with varying namespace

查看:28
本文介绍了如何获取具有不同命名空间的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取下面xml的元素名称.我的请求中的问题是我想获取 Payload 元素.为此,我使用了这个表达式

I wish to get the element name of the below xml. My issue in my request is that I want to get the Payload element. For that I used this expression

name(//env:Payload)

所以我得到了这个结果 env:Payload

So I am getting this result env:Payload

<env:requeset xmlns:env="http://eai.ssss.xxx/Envelope" xmlns:ggg="http://fai.dgn.sss/ggg">
<env:clientbody>
<env:Payload>
<ggg:Service>
<ggg:Request>
<a>name</a>
<b>age</b>
<c>address</c>
</ggg:Request>
</ggg:Service>
</env:Payload>
</env:clientbody>
</env:requeset>

但有时我的客户端发送具有不同元素的请求,有时也没有命名空间.那么我将如何获得以下请求

But some times my client sends requests with different elements and some times without namespace as well. So how would I get the below request also

<requeset xmlns:="http://eai.ssss.xxx/Envelope" xmlns:ggg="http://fai.dgn.sss/ggg">
<clientbody>
<Payload>
<ggg:Service>
<ggg:Request>
<a>name</a>
<b>age</b>
<c>address</c>
</ggg:Request>
</ggg:Service>
</Payload>
</clientbody>
</requeset>

对于上面的请求,我使用相同的表达式,因此它给出了错误,所以我将如何在两种情况下或任何情况下获得有效载荷元素.我试过这个,但它不起作用

For above request I am using same expression so its giving errors so how would I get the Payload element in 2 cases or any case. I have tried with this but its not working

name(//( local-name()='Payload'))

它给出 错误 - 无法计算 XPath 表达式:在/"或//"标记之后需要一个位置步骤.

所以请指导获取没有命名空间和不同命名空间的元素,我也尝试过使用包含表达式,但我无法获得.

So please guide to get that element without namespace and different namespaces also I have tried with Contains expression also but I am unable to get that.

推荐答案

如错误消息所示,您编写的语法不正确.如果要比较每个元素的本地名称,可以这样做:

As indicated by the error message, the syntax you wrote is incorrect. If you want to compare the local name of each element, you can do like this:

//*(local-name() = 'Payload')

但是等等,还有更好的选择来实现这一目标.你在这里所做的基本上是无视命名空间.您还可以使用通配符命名空间运算符 * 以更清晰的语法实现此目的.就这么简单

But wait, there are better options out there to achieve that. What you are doing here is basically disregarding the namespace. You can also use the wildcard namespace operator * to achieve this with a much clearer syntax. It is as simple as

//*:Payload

如果您仍然想尊重命名空间,您还可以使用联合运算符 | 合并两个结果.这样,您将获得带有 env 命名空间而没有任何命名空间的 Payload 元素:

If you still want to respect the namespaces you can also merge two results, using the union operator |. This way, you get the Payload element with env namespace and without any namespace:

//env:Payload | //Payload

当然,在所有这些情况下,您都可以使用 name() 来获取元素名称.

Of course, in all these cases you can use name() to get the element name.

这篇关于如何获取具有不同命名空间的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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