0

I have read several threads on the subject, this one being the closest to what I need, it seems simple, but it just doesn't work.

When I have the next xml:

<Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
 <Header>
  <context xmlns="urn:zimbra"><change token="11127"/>
  </context>
 </Header>
 <Body>
  <Fault>
   <Code>
    <Value>Sender</Value>
   </Code>
   <Reason>
    <Text>no such account: prueba5@empresarias.mx</Text>
   </Reason>
   <Detail>
    <Error xmlns="urn:zimbra">
     <Code>account.NO_SUCH_ACCOUNT</Code>
     <Trace>qtp1027591600-66260:1597793723503:4b07a03bafc0cc32</Trace>
    </Error>
   </Detail>
  </Fault>
 </Body>
</Envelope>

I can easily get what I need, in my case I need the text in Reason, Text, which I get with:

Object fault =
          xPath
              .compile("/Envelope/Body/Fault/Reason/Text/text()")
              .evaluate(xml, XPathConstants.STRING);

fault value is "no such account: prueba5@empresarias.mx", easy, right?

But when I have the next xml:

<Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
 <Header>
  <context xmlns="urn:zimbra">
   <change token="11127"/>
  </context>
 </Header>
 <Body>
  <GetAccountResponse xmlns="urn:zimbraAdmin">
   <account name="prueba2@empresarias.mx" id="6819863b-fcf1-466f-b711-4310457274b3">
    <a n="zimbraPasswordMustChange">TRUE</a>
    <a n="zimbraId">6819863b-fcf1-466f-b711-4310457274b3</a>
   </account>
  </GetAccountResponse>
 </Body>
</Envelope>

I'm trying to get the value of the attribute "id" inside "account", which I would think (based on other answers), that I would get with the next path:

Object zimbraId =
              xPath2
                  .compile("/Envelope/Body/GetAccountResponse/account/@id")
                  .evaluate(xml2, XPathConstants.STRING);

I have tried several things, like getting the using the attribute "name" to search for it, like this:

Object zimbraId =
              xPath2
                  .compile("/Envelope/Body/GetAccountResponse/account[@name='prueba2@empresarias.mx']/@id")
                  .evaluate(xml2, XPathConstants.STRING);

And many other things, but zimbraId is always "", I didn't want to post this question, but I'm really frustated.

Also, this is how zimbra service responds, so I can't change the xml. I'm suspicious about the "GetAccountResponse" element, but I don't really know what I'm doing wrong.

Any help would be very much appreciated.

Lauro182
  • 1,597
  • 3
  • 15
  • 41
  • I evaluated `/Envelope/Body/GetAccountResponse/account/@id` against "xml2" and successfully got the "id" value. I don't think your problem is with the XPath expression. – Allen D. Ball Aug 19 '20 at 02:09
  • @AllenD.Ball: `GetAccountResponse` is in a namespace unaccounted for in the XPath, so it's not at all clear how you'd be able to select the `id` attribute value with the provided XML and XPath. – kjhughes Aug 19 '20 at 02:20
  • ??? I don't see any prefix specified or used and I ran the code to verify. How would you adjust the XPath expression? – Allen D. Ball Aug 19 '20 at 02:35
  • 1
    @AllenD.Ball: `GetAccountResponse` has a default namespace (`xmlns="urn:zimbraAdmin"`). See duplicate link for how to define a namespace prefix and use in the XPath for any given hosting language. – kjhughes Aug 19 '20 at 02:55
  • BTW, @Lauro182, note that in addition to the main XPath namespace issue, your `Envelope` is not in the `http://www.w3.org/2003/05/soap-envelope` namespace as is usually done in SOAP. – kjhughes Aug 19 '20 at 03:00
  • @Lauro182, unfortunately, this question is closed or I would post the working code. I don't think your issue is with the XPath expression. – Allen D. Ball Aug 19 '20 at 03:10
  • @kjhughes, all the examples in the duplicate question's answer define a prefix ("i"). So, I expect there is no harm in setting the namespace context as described (if a prefix can be obtained) but it isn't necessary. – Allen D. Ball Aug 19 '20 at 03:10
  • @AllenD.Ball: Incorrect, as I've already explained. It appears that you may not understand the concept of a default namespace. – kjhughes Aug 19 '20 at 03:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220064/discussion-between-allen-d-ball-and-kjhughes). – Allen D. Ball Aug 19 '20 at 03:19
  • @kjhughes It certainly wasn't easy, but the answer on duplicate question guided me to finally solve my problem, thank you very much. – Lauro182 Aug 20 '20 at 02:24

0 Answers0