XPath and Regular Expressions

LegacyForumLegacyForum Posts: 1,669 ✭✭
edited December 2016 in SOAtest
XPATH
Hello board members,

I need your help once more. I have an XML tag in my result structures that represents the "Date". The value of this tag
changes every time a new request comes to my server process. I added the Xpath to my global properties. In particular
my Xpath contains

/Envelope/Body/SomeWhere/In/Time/Date/,
/Envelope/Body/Eisai/Kati/Magiko/Date/,
/Envelope/Body/Opoios/Einai/Vazelos/Date/,
/Envelope/Body/Test/Date/
/Envelope/Body/Ekei/Ekei/Sthn/B_Ethniki/Date/
and so on, or sometimes "Date" is between two other tags for example
/Envelope/Body/Lefteris/Date/SOAP/

To be honest I would like to have my Xpath properties more compact. Eg */Date. Or /Envelope/*/Date but it is not working.
Do you know any other way that simplifies the representation of XPath?

Lefteris
Tagged:

Comments

  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    edited December 2016


    The wild card replaces a specific (parent) element. Using /Envelope/*/Date, implies Date element is the third nested child, but that's not the case. You should use /Envelope/*/*/*/*/Date to replace the first Xpath in your list (/Envelope/Body/SomeWhere/In/Time/Date). You can then use /Envelope/*/*/*/Date to replace the second and third in your list, and so forth.

    Hope that helps
  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    edited December 2016
    Note:

    You don't need to know the exact structure of the document or exactly how deep the Date element may be in the document. You don't need to use regular expressions, just use an XPath that is defined to search within any depth.

    This will find an element with the tag name "Date" anywhere in the document.

    //Date

    Alternatively, you could use this:

    /descendant::Date

    If there are multiple Date elements in the document, the XPath will return a sequence of all of the elements.

    There is a subtle difference between // and /descendant if you use indexes. If there are multiple Date elements in your document and you want to get only the first one, use this:

    /descendant::Date[1]

    The XPath //Date[1] could return multiple elements for subtle reasons that I won't touch here on unless someone asks.

    If the Date element has a namespace, then the simplest XPaths you create can simply ignore the namespace using the prefix:tag format with "*" for the tag:

    //*:Date

    Or:

    /descendant::*:Date

    These will match elements with the tag name "Date" regardless of the namespace.
Sign In or Register to comment.