2

Sample XML

<catalog>
    <book id="Adventure" Type="Hardcover">
        <other>
            <author name="Jones" id="Adventure">
            </author>
        </other>
    </book>
</catalog>

Do these statements make sense? I need a sanity check to see what's going on with all these variables/quotes. I tried to use one of the XPath test sites, but it keeps giving me an error and I'm not sure why.

bookVar = "Adventure"
bookStyle = "Hardcover"
bookAuthor = "Jon"

Set my_location = XMLFile.SelectNodes("/catalog/book[@id=""" & bookVar & """]/author/other/*[contains(name(),""" & bookAuthor & """]")

Set my_location2 = XMLFile.SelectNodes("/catalog/book[@id=""" & bookVar & """ and Type=""" & bookStyle &"""]

Set my_location3 = XMLFile.SelectNodes("/catalog/book[@id=""" & bookVar & """]/other/author/*[contains(name(),""" & bookAuthor & """]")

1 Answer 1

4

Several notes :

  • Use @ at the beginning of attribute name to reference an XML attribute in xpath

  • I'd suggest to use single quotes in your xpath since string wrapper character being used is double quotes. That's will make your code looks cleaner.

  • name() return name of the current context element/attribute, use @name instead to reference XML attribute named name

  • Some of your path doesn't correspond to the exact hierarchy of elements in the XML document

Example xpath queries :

bookVar = "Adventure"
bookStyle = "Hardcover"
bookAuthor = "Jon"

Set my_location = XMLFile.SelectNodes("/catalog/book[@id='" & bookVar & "']/other/author[contains(@name,'" & bookAuthor & "']")

Set my_location2 = XMLFile.SelectNodes("/catalog/book[@id='" & bookVar & "' and @Type='" & bookStyle &"']")
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your comment, very informative and helpful!

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.