1

I'm working on a VBA script to get data from a xml document. I need help to modify my XPath code to search for a partial string instead of the whole one. This is my current expression:

xmlhttpResponse.SelectNodes("/CharacteristicList/Characteristic[Name='ZCOMP_A_03']
                        /ComplexValueList/ValueItem/Value").Item(0).text

What I would like to do is something like this [Name='ZCOMP_A_'], but it doesn't seems to exist or to work like this. I saw some codes with starts-with and contains, but I would like to use a XPath similar with the one already used (if possible).

This is part of the XML code.

<CharacteristicList>
   <Characteristic>
      <Name>ZCOMP_A_03</Name>
      <ValueList>
         <ValueItem>
            <Value>10.50</Value>

Update 1! I made some changes according to kjhughes, but I'm getting a "Unknown method" error. This is the updated code:

Dim xmlhttpRequest As New MSXML2.xmlhttp
Dim xmlhttpResponse As New MSXML2.DOMDocument
xmlhttpResponse.setProperty "SelectionLanguage", "XPath"

  Envelope = Envelope & "</soapenv:Envelope>"


  xmlhttpRequest.Open "POST", "http://website.xyz"
  xmlhttpRequest.setRequestHeader "Content-Type", "text/xml;charset=UTF-8"
  xmlhttpRequest.setRequestHeader "SOAPAction", "http://website2.xyz"

  xmlhttpRequest.send Envelope

  Set xmlhttpResponse = xmlhttpRequest.responseXML
  'Lenght_1 = xmlhttpResponse.SelectNodes("//Characteristic[Name='ZCOMP_A_03']/ComplexValueList/ValueItem/Value").Item(0).text Previous version
   Lenght_2 = xmlhttpResponse.SelectNodes("//Characteristic[starts-with(Name,'ZCOMP_A_')]/ComplexValueList/ValueItem/Value").Item(0).text
2
  • Perhaps vba doesn't support the way you expect otherwise SelectNodes("//Characteristic//Name[contains(.,'ZCOMP_A_')]")(0).Text this expression should bring you the desired results. Commented May 3, 2018 at 14:15
  • @SIM, I tried your suggestion, but it returns to me "Object is obrigatory". I'm trying some changes to solve this Commented May 3, 2018 at 14:31

2 Answers 2

2

What I would like to do is something like this [Name='ZCOMP_A_'], but it doesn't seems to exist or to work like this. I saw some codes with starts-with and contains, but I would like to use a xpath similar with the one already used (if possible).

Characteristic[Name='ZCOMP_A_'] selects for Characteristic elements with a Name child whose string value equals ZCOMP_A_.

If you want it to start with that string instead, use

Characteristic[starts-with(Name,'ZCOMP_A_')]

If you want it to appear anywhere within that string, use

Characteristic[contains(Name,'ZCOMP_A_')]

No, there is no shortened form that does either function.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. However, I'm getting an "Unknown method" when trying to use them. Do I need to do something else to get it working?
Edit your question and add your exact updated function call so that we can see what you've done. The XPath advice given in this answer is correct, so your problem must lie elsewhere.
1

Check this out. If this is what you wanted to try:

Sub DemoXML()
    Dim XDoc As New DOMDocument, elem As Object, strXML As String

    strXML = "<CharacteristicList><Characteristic><Name>ZCOMP_A_03</Name><ValueList><ValueItem><Value>10.50</Value></ValueItem></ValueList></Characteristic></CharacteristicList>"
    XDoc.LoadXML (strXML)

    For Each elem In XDoc.SelectNodes("//Characteristic")
        [A1] = elem.SelectNodes(".//Name")(0).Text
        [B1] = elem.SelectNodes(".//Value")(0).Text
    Next elem
End Sub

Reference to add:

Microsoft XML V6.0

Result:

ZCOMP_A_03
10.5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.