简体   繁体   English

我的xpath有什么问题?

[英]What is the wrong in my xpath?

I have the following xml document: 我有以下xml文档:

<Item>
  <DataContainer>
    <ComponentId>12</ComponentId>
    <ChangeTo>undefined</ChangeTo>
    <OperationCode>ADD</OperationCode>
    <ComponentTableName>invFeatureType</ComponentTableName>
    <ComponentColName>invFeatureTypeId</ComponentColName>
    <Type>Feature</Type>
    <PlanFeatureName>Long Distance</PlanFeatureName>
    <ToPlanFeatureName/>
  </DataContainer>
</Item>

When I apply the following xpath to select a specific node : 当我应用以下xpath选择特定节点时:

"/Item/DataContainer[ChangeTo = '12' and Type='Feature']")

I get the following error: 我收到以下错误:

Uncaught DOMException: Failed to execute 'evaluate' on 'Document': The string '/ITEM/DATACONTAINER[CHANGETO = '12' AND TYPE='FEATURE']' is not a valid XPath expression 未捕获的DOMException:无法对'Document'执行'evaluate':字符串'/ ITEM / DATACONTAINER [CHANGETO ='12'AND TYPE ='FEATURE']'不是有效的XPath表达式

Any idea ?? 任何想法 ??

Perhaps you just confused ChangeTo with ComponentId ? 也许您只是将ChangeToComponentId混淆了? Given only the document fragment you show, the following XPath expression would retrieve the single DataContainer element: 给出您显示的文档片段,以下XPath表达式将检索单个DataContainer元素:

/Item/DataContainer[ComponentId = '12' and Type='Feature']

It is obvious that you do not show a minimal, complete and verifiable document - which is always a bad idea. 很明显,您没有显示最小,完整和可验证的文档 -这始终是一个坏主意。

Regarding the error you are getting while evaluating your path expression: If you are actually trying to evaluate a path expression that is all uppercase it will not work. 关于您在评估路径表达式时遇到的错误:如果您实际上是在尝试评估全部为大写的路径表达式,它将无法正常工作。 XML and XPath are case-sensitive and the logical and cannot be uppercase in some implementations. XML和XPath区分大小写并且是逻辑的and在某些实现中不能大写。

If not, it might be because your XPath library or framework only supports a fraction of the XPath specification. 如果不是,则可能是因为您的XPath库或框架仅支持XPath规范的一部分。 For instance, it might not support predicates (the part between [ and ] ). 例如,它可能不支持谓词( []之间的部分)。


Reacting to a comment: 对评论作出反应:

add // or / at the beginning. 在开头添加/// – eLRuLL – eLRuLL

No, even without // or / a path expression is valid and this will not return the kind of error you are describing. 否,即使没有///路径表达式也有效,这不会返回您所描述的错误类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM