I'm a newbie to XSLT. I've an XML document and I need to come up with xslt to validate certain rules in the XML document. The XML and xsl file will be used in xsltproc tool and the output will be a simple Pass or Fail.
Sample XML:
...
<Manager mincount="4" grade="10"...>
<Employee id="1" grade="9" .... />
<Employee id="2" grade="8" .... />
.....
</Manager>
- The number of children under Manager (Employee in this case) must be equal to or greater than the value of mincount attribute.
- All the employee's grade must be less than the Manager grade.
Appreciate your help! TIA!
Use an XSD schema instead. It's designed to validate XML.
In particular you might be interested in XSD 1.1 assertions.
See http://www.w3schools.com/schema/ for a good tutorial.
Here's an XSLT 1.0 option that gives a pass/fail. There is additional detail in the "Fail", but that can be removed. It also outputs the message to both stdout and stderr and terminates processing.
Here are a few XML/output examples:
Here's an XSLT that checks mincount values vs actual number of occurrences of Employee. Note that xsl:function is used so this requires XSLT 2.0.
When applied on the following input:
the result is:
An alternative in XSLT 1.0 could be the following:
with result