htmlxmlxpathxercestinyxpath

In TinyXPath how to query data by indexed by element content


I am using TinyXPath to enhance an existing test tool so data from a customer XML structure can be fetched and used.

The XML looks like this

<Platform>
  <LinkData>
    <Plan>
      <Label>A</Label>
        <Settings>
           <SomeSetting1>ENABLED</SomeSetting1>
           <SomeSetting2>ENABLED</SomeSetting2>
        </Settings>
    </Plan>
    <Plan>
      <Label>B</Label>
        <Settings>
           <SomeSetting1>ENABLED</SomeSetting1>
           <SomeSetting2>DISABLED</SomeSetting2>
        </Settings>
    </Plan>
  </LinkData>
</Platform>

Given the above structure, which I have no control of, I need to be able to construct XPath expressions for TinyXPath. Put simply TinyXPath needs to return the values in the SomeSetting1/2 fields given when the correct child Label values match (resolve to true), so the test app can use them.

I have tried the following but an struggling with the way is indexed using a child element (normally I would expect use of an attribute. Here is my attempt which does not return a result (e.g. ENABLED/DISABLED) :-

Platform/LinkData/Plan[child::Label='A']/Settings/SomeSetting1/text()
Platform/LinkData/Plan[child::Label='A']/Settings/SomeSetting2/text()
Platform/LinkData/Plan[child::Label='B']/Settings/SomeSetting1/text()
Platform/LinkData/Plan[child::Label='B']/Settings/SomeSetting2/text()

Any further help from TinyXPath gurus would be much appreciated - thanks!


Solution

  • This xpath will return what you're looking for with the given XML:

    //Settings/child::node()/text()
    

    This xpath will also add a check for Labels and group by concat |:

    //LinkData/Plan[Label/text()='A']/Settings/child::node()/text() | //LinkData/Plan[Label/text()='B']/Settings/child::node()/text()
    

    And this one combines them both inside the label check:

    //LinkData/Plan[Label/text()='A' or Label/text()='B']/Settings/child::node()/text()
    

    Hope this helps!