• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XMLUnit and XPath problem

 
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I have a problem with XPath and XMLUnit.

I have two versions of an xml file one with namespace declarations and one without. Basically what I am trying to do is check if an element exists in the xml file using and Xpath expression and XMLUnit test methods.

When I use the second version of the file the test passes ok. But when I use the first version of the file the test fails.

I am calling the test method as follows: The parameters are an Xpath expression and a document. If the element exists the method does nothing, if it fails and Assertion Error is thrown.

//Fails
XMLAssert.assertXpathExists("/Book/Title", xml1);

//Suceeds
XMLAssert.assertXpathExists("/Book/Title", xml2);


XML File 1


XML File 2


Any ideas?
Thanks, Mark.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In XML file 1, your Book and Title elements are in the default namespace. But your XPath expression is looking for Book and Title elements that are not in a namespace. This is a defect in XPath. The workaround I usually use is something like this:
 
Mark Smyth
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
In XML file 1, your Book and Title elements are in the default namespace. But your XPath expression is looking for Book and Title elements that are not in a namespace. This is a defect in XPath. The workaround I usually use is something like this:



That did the trick, exactly what I was looking for. An oddity of XPath I was unaware of before.

Thanks,
Mark.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic