我试图获取一个值的Xpath,但是获得嵌套条件的错误 [英] I am trying to get the Xpath for a value but getting error for nested condition

查看:118
本文介绍了我试图获取一个值的Xpath,但是获得嵌套条件的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过给出条件一个唯一的值U003_O100_001T_609644来查找来自多个ViewItem的值为1900310的谓词的XPath表达式。请参阅下面的代码,

I am trying to find the XPath expression with predicate for the value 1900310 from several ViewItem by giving the condition a unique value U003_O100_001T_609644. Please see the code below,

< b:ViewItem>

<b:ViewItem>

< b:Fields>

<b:Fields>

< c:KeyValueOfstringanyType>

<c:KeyValueOfstringanyType>

< c:Key> ID< / c:Key>

<c:Key>ID</c:Key>

< c:Value> 1900310 < / c:Value>

<c:Value>1900310 </c:Value>

< / c:KeyValueOfstringanyType>

</c:KeyValueOfstringanyType>

< c:KeyValueOfstringanyType>

<c:KeyValueOfstringanyType>

< c:Key> SubType< / c:Key>

<c:Key>SubType</c:Key>

< c:Value> U003_O100_00IT_609644< / c:Value>

<c:Value>U003_O100_00IT_609644</c:Value>

< / c:KeyValueOfstringanyType>

</c:KeyValueOfstringanyType>

< c:KeyValueOfstringanyType>

<c:KeyValueOfstringanyType>

< c:Key> SectionType< / c:Key>

<c:Key>SectionType</c:Key>

< c:Value>已发布< / c:Value>

<c:Value>Released</c:Value>

< / c:KeyValueOfstringanyType>

</c:KeyValueOfstringanyType>

< / b:Fileds>

</b:Fileds>

< / b:ViewItem>

</b:ViewItem>

我试图写下面的表达式,

I tried to write the expression as follows,

Query = / Envelope / Body / GetViewByIdResponse / GetViewByIdResult / Items / ViewItem / Fields / KeyValueOfstringanyType [Value ='U003_O100_001T_609644'] /值[Key ='ID']

Query=/Envelope/Body/GetViewByIdResponse/GetViewByIdResult/Items/ViewItem/Fields/KeyValueOfstringanyType[Value='U003_O100_001T_609644']/Value[Key='ID']

但它不是给我的价值。你可以帮忙吗?

But it is not giving me the value. Can you please help?

推荐答案

一个选择想要的元素的XPath表达式是

One XPath expression that selects the wanted element is:

 /b:ViewItem
    /b:Fields
       /c:KeyValueOfstringanyType
          [c:Key = 'ID']
                   /c:Value

请注意:提供的XML文档具有命名空间,并且包含未校正元素名称的任何XPath表达式将不会选择所需元素,但以下形式的表达式除外:

Do note that the provided XML document has namespaces and any XPath expression that contains unprefixed element names is not going to select the wanted element, with the exception of expressions of the following form:

 /*[local-name() = 'ViewItem']
    /*[local-name() = 'Fields']
       /*[local-name() = 'KeyValueOfstringanyType']
               [*[local-name() = 'Key'] = 'ID']
                   /*[local-name() = 'Value']

另外,对于上面的第一个XPath表达式,名称空间以 b:c:
必须注册(请阅读XPath引擎的文档如何执行)。

Also, for the first XPath expression above, the namespaces prefixed by "b:" and "c:" must be "registered" (read the documentation of your XPath engine how to do that).

基于XSLT的验证

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:b="some:b" xmlns:c="some:c">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "/b:ViewItem
        /b:Fields
           /c:KeyValueOfstringanyType
              [c:Key = 'ID']
                /c:Value
    "/>

==============

  <xsl:copy-of select=
   "/*[local-name() = 'ViewItem']
         /*[local-name() = 'Fields']
               /*[local-name() = 'KeyValueOfstringanyType']
                    [*[local-name() = 'Key'] = 'ID']
                            /*[local-name() = 'Value']
   "/>
 </xsl:template>
</xsl:stylesheet>

一个,纠正严重畸形):

when this XSLT transformation is applied on the following XML document (the provided one, corrected for severe malformedness):

<b:ViewItem xmlns:b="some:b" xmlns:c="some:c">
    <b:Fields>
        <c:KeyValueOfstringanyType>
            <c:Key>ID</c:Key>
            <c:Value>1900310 </c:Value>
        </c:KeyValueOfstringanyType>
        <c:KeyValueOfstringanyType>
            <c:Key>SubType</c:Key>
            <c:Value>U003_O100_00IT_609644</c:Value>
        </c:KeyValueOfstringanyType>
        <c:KeyValueOfstringanyType>
            <c:Key>SectionType</c:Key>
            <c:Value>Released</c:Value>
        </c:KeyValueOfstringanyType>
    </b:Fields>
</b:ViewItem>

评估两个XPath表达式,并输出所选择的节点

<c:Value xmlns:c="some:c" xmlns:b="some:b">1900310 </c:Value>

==============

<c:Value xmlns:c="some:c" xmlns:b="some:b">1900310 </c:Value>

这篇关于我试图获取一个值的Xpath,但是获得嵌套条件的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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