3

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

</b:ViewItem>

I tried to write the expression as follows,

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?

Thanks

1 Answers1

3

One XPath expression that selects the wanted element is:

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

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

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 - based verification:

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

The two XPath expressions are evaluated and the nodes they select are output:

<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>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • I have used the following query and getting the value as well 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() Now I am trying to find the position of this value...:) Thanks a lot for your help. – Mohammad Syed Feb 14 '12 at 19:14