使用命名空间运行 xpath 查询时出现问题 [英] Problem running xpath query with namespaces

查看:36
本文介绍了使用命名空间运行 xpath 查询时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 xpath 表达式在定义了不同命名空间的 xml 文档中选择节点集.

xml 看起来像这样:

<ns:Msg xmlns:ns="http://www.noventus.se/epix1/genericheader.xsd"><通用头文件><SubsysId>1</SubsysId><SubsysType>30003</SubsysType><SendDateTime>2009-08-13T14:28:15</SendDateTime></GenericHeader><m:OrderStatus xmlns:m="http://www.noventus.se/epix1/orderstatus.xsd"><标题><OrderSystemId>Soda SE</OrderSystemId><OrderNo>20090811</OrderNo><状态>0</状态></标题><线条>...

我只想选择具有OrderStatus"子节点的Msg"节点,因此我想使用以下 xpath 表达式:/Msg[count('OrderStatus') >0] 但这不起作用,因为我收到一条错误消息:需要命名空间管理器或 XsltContext.此查询具有前缀、变量或用户定义的函数".

所以我想我想使用一个看起来像这样的表达式:/*[local-name()='Msg'][count('OrderStatus') >0] 但这似乎不起作用..有什么想法吗?

兄弟,

安德烈亚斯

解决方案

<块引用>

我想使用以下 xpath表达式:

/Msg[count('OrderStatus')[ 0]

<块引用>

但这不起作用,因为我收到一条错误消息:命名空间需要管理器或 XsltContext.

这是一个常见问题.

在 XPath 中,无前缀的名称总是被认为属于无命名空间".

然而,你要选择的元素实际上在"http://www.noventus.se/epix1/genericheader.xsd"命名空间.

您有两种可能的方式来编写 XPath 表达式:

  1. 使用宿主语言的功能将前缀与表达式中的名称所属的所有不同命名空间相关联.在这个具体案例中,您没有说明托管语言是什么,所以我无法帮助您.可以在此处找到 C# 示例.

如果您已将前缀 "xxx" 与名称空间 "http://www.noventus.se/epix1/genericheader.xsd" 和前缀 "yyy" 到命名空间 "http://www.noventus.se/epix1/orderstatus.xsd",那么你的 Expression 可以写成:

/xxx:Msg[yyy:OrderStatus]

:2: 如果你根本不想使用任何前缀,仍然可以构造一个 XPath 表达式,但它不会太可读:

/*[local-name() = 'Msg' and *[local-name() = 'OrderStatus']]

最后,请注意:

  1. 为了测试元素 x 是否有子元素 y,没有必要测试正的 count(y).只需使用:x[y]

  2. Xpath 位置从 1 开始.这意味着 NodeSetExpression[0] 从不选择节点.你想要:NodeSetExpression[1]

I'm trying to use an xpath expression to select a node-set in an xml document with different namespaces defined.

The xml looks something like this:

<?POSTEN SND="SE00317644000" REC="5566420989" MSGTYPE="EPIX"?>
<ns:Msg xmlns:ns="http://www.noventus.se/epix1/genericheader.xsd">
  <GenericHeader>
    <SubsysId>1</SubsysId>
    <SubsysType>30003</SubsysType>
    <SendDateTime>2009-08-13T14:28:15</SendDateTime>
  </GenericHeader>
  <m:OrderStatus xmlns:m="http://www.noventus.se/epix1/orderstatus.xsd">
    <Header>
      <OrderSystemId>Soda SE</OrderSystemId>
      <OrderNo>20090811</OrderNo>
      <Status>0</Status>
    </Header>
    <Lines>...

I want to select only "Msg"-nodes that has the "OrderStatus" child and therefore I want to use the following xpath expression: /Msg[count('OrderStatus') > 0] but this won't work since I get an error message saying: "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function".

So I think I want to use an expression that looks something like this: /*[local-name()='Msg'][count('OrderStatus') > 0] but that doesn't seem to work.. any ideas?

Br,

Andreas

解决方案

I want to use the following xpath expression:

/Msg[count('OrderStatus')[ 0]

but this won't work since I get an error message saying: "Namespace Manager or XsltContext needed.

This is a FAQ.

In XPath a unprefixed name is always considered to belong in "no namespace".

However, the elements you want to select are in fact in the "http://www.noventus.se/epix1/genericheader.xsd" namespace.

You have two possible ways to write your XPath expression:

  1. Use the facilities of the hosting language to associate prefixes to all different namespaces to which names from the expression belong. You haven't indicated what is the hosting language in this concrete case, so I can't help you with this. A C# example can be found here.

If you have associated the prefix "xxx" to the namespace "http://www.noventus.se/epix1/genericheader.xsd" and the prefix "yyy" to the namespace "http://www.noventus.se/epix1/orderstatus.xsd", then your Expression can be written as:

/xxx:Msg[yyy:OrderStatus]

:2: If you don't want to use any prefixes at all, an XPath expression can still be constructed, however it will not be too readable:

/*[local-name() = 'Msg' and *[local-name() = 'OrderStatus']]

Finally, do note:

  1. In order to test if an element x has a child y it isn't necessary to test for a positive count(y). Just use: x[y]

  2. Xpath positions are 1-based. This means that NodeSetExpression[0] never selects a node. You want: NodeSetExpression[1]

这篇关于使用命名空间运行 xpath 查询时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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