2012-06-21

XPath with bad XML

My problems yesterday with the HTTP header got solved. I just needed to figure out the correct soapAction. After adding the soapAction of CreateTicket the other system accepted my webservice call and responded with a big chunk of XML. My next problem for today was to retrieve the ticket id from the other systems XML response.

I was expecting it to look like this:
<PrimaryTicketID>20120621:002</PrimaryTicketID>

But instead it looks like this:
<Result ID="20120621:002">
<PrimaryTicketID \>

How do pull out the attribute value using XPath? Actually it is not hard at all if you have done some XPath before, but as you remember I am totally new to this XML business. After trying different syntax and not getting anything out at all, I started to google for an answer. By mistake I started reading pages about XSLT and tried to get clues from there. After some time I actually managed to figure out how to do by comparing XSLT from an example web page and the XPath I was working with.

I just had to do:
Result/@ID

And got the result:
<result>20120621:002</result>

But for that to work I first had to edit the XML. With the original XML from the adapter response it still didn't work. Something seemed to be wrong with the XML format and no nodes could be filtered out.

I even tried:
//Result/attribute::ID

In the end I got a tip to ignore the nodes and just search for any attribute with the name "ID":
//@ID

Finally I got the result I needed even with the original XML.

I found some good information about XPath on W3Schools, but I really need to buy myself a book about XPath because this is really new to me.

No comments:

Post a Comment