Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

XMLUnit

  • 13-06-2008 11:55AM
    #1
    Moderators, Science, Health & Environment Moderators Posts: 10,093 Mod ✭✭✭✭


    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 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 version="1.0" encoding="UTF-8"?>
    <Book xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"   
    xmlns="http://www.publishing.org"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.publishing.org tests/etc/Book.xsd"> 
         <Title id="123">The Book</Title> 
         <ISBN>1-55874-262-X</ISBN>
         <Publisher>Acme Communications, Inc.</Publisher>
    </Book>
    

    XML File 2
    <?xml version="1.0" encoding="UTF-8"?>
    <Book> 
        <Title id="123">The Book</Title> 
        <ISBN>1-55874-262-X</ISBN>
        <Publisher>Acme Communications, Inc.</Publisher>
    </Book>"
    

    Any ideas? Changing the xml is not an option unfortunately.
    Thanks lads.


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 10,093 Mod ✭✭✭✭marco_polo


    Seems to not like the default namespace. I found this dirty hack it seems to do the trick. Not very pretty it must be said. If anyone has any better suggestions then let me know

    //*[local-name()="Book"]/*[local-name()="Title"]


Advertisement