Search code examples
xmlxpathjunitxunitxmlunit

JUnit Test if XML Document is sorted on arbitrary column


Given an xml document like

<root>
  <row>
    <a>2</a>
    <b>1</b>
    <c>8</c>
  </row>
  <row>
    <b>5</b>
    <a>2</a>
    <c>8</c>
  </row>
  <row>
    <a>2</a>
    <c>8</c>
    <b>6</b>
  </row>
</root>

Is there an easy way to assert that the XML document is sorted on element B in XMLUnit

Edit: I have an odd problem with a piece of software that I can not change where the value of the XML tags in any given node needs to be in a specific order. I'd like it for my test harness to enforce this set of rules before any other validation.


Solution

  • Use the value of the following simple XPath 1.0 expression:

    not(//b[. > following::b])