title: How to validate XML document with complex validation rules?
body: <p>I have to validate a XML which has many complex validation rules. I was thinking to define a XSD file to do the validation automatically, but there are some complex ones can't be defined in XSD.</p>
  
  <p>Now I have to write a lot of java code to get values by xpath, validate them one by one, which is very boring to code and test.</p>
  
  <p>I'm looking for an easy way to do this, is there any validation libraries for XML? Or can we define some really complex validation rules in java and used with XSD?</p>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>I forgot to mention that, I need to add some error code in the error message that to help another team to handle the errors. I'm not sure if XSD can let me do this.</p>
  
  <hr>
  
  <p>UPDATE AGAIN:</p>
  
  <p>A complex rule: (following paths are under a node of document, say <code>/my-node</code>, not the root)</p>
  
  <pre><code>if /aaa exists
      /bbb should exist
      /ccc should not exist, and
      if /aaa's value is 111:
         /bbb's value should be 222
      if /aaa's value is 555:
         /bbb's value should be 666
  else
      /bbb should not exist
      /ccc should exist, and its value should not be number
  </code></pre>