我正在尝试获取某个值的 Xpath,但出现嵌套条件错误

2024-03-17

我试图通过为条件提供唯一值 U003_O100_001T_609644 来从多个 ViewItem 中查找带有值 1900310 谓词的 XPath 表达式。请看下面的代码,

ID

1900310 :值>

子类型

U003_O100_00IT_609644:值>

部分类型

已发布

:字段>

:查看项目>

我尝试将表达式写成如下,

查询=/信封/正文/GetViewByIdResponse/GetViewByIdResult/Items/ViewItem/Fields/KeyValueOfstringanyType[Value='U003_O100_001T_609644']/Value[Key='ID']

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

Thanks

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

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

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

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

另外,对于上面的第一个 XPath 表达式,命名空间前缀为"b:" and "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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我正在尝试获取某个值的 Xpath,但出现嵌套条件错误 的相关文章

随机推荐