【问题标题】:I am trying to get the Xpath for a value but getting error for nested condition我正在尝试获取 Xpath 的值,但嵌套条件出错
【发布时间】:2012-02-13 18:00:48
【问题描述】:

我试图通过给条件一个唯一值 U003_O100_001T_609644 从几个 ViewItem 中找到具有值 1900310 的谓词的 XPath 表达式。请看下面的代码,

ID

1900310

子类型

U003_O100_00IT_609644

SectionType

已发布

归档>

我试着把表达式写成如下,

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

但这并没有给我带来价值。你能帮忙吗?

谢谢

【问题讨论】:

    标签: xpath expression


    【解决方案1】:

    一个选择所需元素的 XPath 表达式是

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

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

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

    此外,对于上面的第一个 XPath 表达式,以 "b:""c:" 为前缀的命名空间 必须“注册”(阅读您的 XPath 引擎的文档如何做到这一点)。

    基于 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>
    

    当此 XSLT 转换应用于以下 XML 文档时(提供的文档,已针对严重的格式错误进行了更正):

    <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>
    

    【讨论】:

    • 我使用了以下查询并获取了值 Query=/s:Envelope/s:Body/GetViewResponse/GetViewResult/b:Items/b:ViewItem/b:Fields/c:KeyValueOfstringanyType /c:Value[text()=\"{pSpec}\"]/../../../b:Fields/c:KeyValueOfstringanyType/c:Key[text()=\"ID\"]/ ../c:Value/text() 现在我正在尝试找到这个值的位置...:) 非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2014-02-08
    • 2021-02-18
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多