Menu

#25 Qualified values and XMLAssert.assertXpathEvaluatesTo

Java 1.6
closed-fixed
nobody
None
5
2014-12-31
2008-10-02
No

I try to use XMLAssert.assertXpathEvaluatesTo to check a node value (faulcode value of a SOAP Fault). The value is a qualified value.

XMLAssert.assertXpathEvaluatesTo does not work correctly with qualified value. In the namespace context, if you set a prefix different than the one in your XML document, you can't be able to have a successfull test, expect if you set the prefix provided by your XML document.

My XML:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Body><env:Fault><faultcode>env:Server</faultcode><faultstring>marche pas</faultstring><detail/></env:Fault></env:Body></env:Envelope>

My namespace context:
final Map<String, String> namespaces = new HashMap<String, String>();
namespaces.put("env11", "http://schemas.xmlsoap.org/soap/envelope/");

XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(namespaces));

My assert:
XMLAssert.assertXpathEvaluatesTo("env11:Server", "//env11:Envelope/env11:Body/env11:Fault/faultcode", faultDocument);

Discussion

  • Stefan Bodewig

    Stefan Bodewig - 2008-10-09

    Hmm, env:Server is just the textual content of an element from a pure XML point of view. How should
    XMLUnit know that env: inside some arbitrary text is supposed to be the prefix attached to an XML namespace?

     
  • Christophe DENEUX

    Yes, right, perhaps can you add a new assert to check qualified values: assertXpathEvaluatesTo(QName, String, Document) ? This assert expects that the value is a qualified value, if not the assert fails. If the qualied value is not the expected one, the assert fails.

    Example:
    XMLAssert.assertXpathEvaluatesTo(new QName("http://schemas.xmlsoap.org/soap/envelope/", "Server"),
    "//env11:Envelope/env11:Body/env11:Fault/faultcode", faultDocument);

     
  • Stefan Bodewig

    Stefan Bodewig - 2008-10-20

    not as straight forward as it sounds, but a valid enhancement request.

    Not a bug, I hope you agree.

    I'm afraid QName doesn't exist prior to Java5 so we'd need to either provide our own abstraction or make the method Java5 only.

     
  • Stefan Bodewig

    Stefan Bodewig - 2008-10-20
    • milestone: 814134 -->
     
  • Stefan Bodewig

    Stefan Bodewig - 2014-12-31
    • status: open --> closed-fixed
    • Group: --> Java 1.6
     
  • Stefan Bodewig

    Stefan Bodewig - 2014-12-31

    added with svn revision 585 - https://sourceforge.net/p/xmlunit/code/585/

    Just need to update the user guide.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.