user619882
user619882

Reputation: 349

Searching for an xml element based on content with path data

I have the following xml:

<Summary>
<Decision>-1</Decision>
<Reasons>
   <Reason>
      <Element>Address/Postcode</Element>
      <Decision>1</Decision>
   </Reason>
   <Reason>
      <Element>Name/Firstname</Element>
      <Decision>13</Decision>
   </Reason>
   <Reason>
      <Element>Name/Lastname</Element>
      <Decision>1</Decision>
   </Reason>
   <Reason>
      <Element>IdentityDocument[@TypeOfDocument='NationalId']/Number</Element>
      <Decision>4</Decision>
   </Reason>
   <Reason>
      <Element>DOB</Element>
      <Decision>17</Decision>
   </Reason>
</Reasons>   
</Summary>

I'm trying to read a Decision based on an Element. For most of them it's easy:

//Reasons/Reason[Element='Name/Firstname']/Decision

But I fail when trying to get the IdentityDocument:

//Reasons/Reason[Element='IdentityDocument[@TypeOfDocument='NationalId']/Number']/Decision

Error
net.sf.saxon.trans.XPathException: XPath syntax error at char 125 in {...Document='NationalId']/Numb...}: expected "]", found name "NationalId"

When I try escaping \'NationalId\' it returns the same error Replacing with double quotes \"NationalId\" compiles but instead of finding the right Decision "4" it gets []

I'm using SoapUI and it's Groovy scripting language to read the xml.

How do I search for this line?

Upvotes: 0

Views: 1012

Answers (2)

SiKing
SiKing

Reputation: 10329

Your XPath has incorrectly enclosed quotes. You probably meant:

//Reasons/Reason[Element="IdentityDocument[@TypeOfDocument='NationalId']/Number"]/Decision

You could also use the contains() function, like so:

//Reason[Element[contains(., 'IdentityDocument')]]/Decision

Upvotes: 1

Alex Gidan
Alex Gidan

Reputation: 2679

You should puth the original xpath reference inside an element including it as CDATA. This way the parser will ignore it and your path will be valid.

Example:

<Reason>
   <Element><![CDATA[IdentityDocument[@TypeOfDocument='NationalId']/Number]]></Element>
   <Decision>4</Decision>
</Reason>

Upvotes: 0

Related Questions