Schematron calculation tests for optional elements
Posted: Thu Nov 21, 2013 4:08 pm
Hi,
I need to create set of Schematron rules testing calculations in an XML Invoice. The problem is that some of the tested elements are optional and my rules fail when the tested elements are mjissing. How do I specify something like: "test value if the element is present"?
The XML instance excerpt:
My Schematron checking the calculation:
It fails when the amountInclusiveAllowancesCharges and invoiceAllowanceChargeAmount are missing (they are optional).
I'll be grateful for any useful tips,
Ewa
I need to create set of Schematron rules testing calculations in an XML Invoice. The problem is that some of the tested elements are optional and my rules fail when the tested elements are mjissing. How do I specify something like: "test value if the element is present"?
The XML instance excerpt:
Code: Select all
<invoice>
<invoiceLineItem number="1">
<invoicedQuantity>50</invoicedQuantity>
<amountExclusiveAllowancesCharges>500</amountExclusiveAllowancesCharges>
<amountInclusiveAllowancesCharges>455</amountInclusiveAllowancesCharges>
<itemPrice>10</itemPrice>
<invoiceAllowanceChargeAmount>45</invoiceAllowanceChargeAmount>
</invoiceLineItem>
<invoiceLineItem number="2">
<invoicedQuantity>10</invoicedQuantity>
<amountExclusiveAllowancesCharges>150</amountExclusiveAllowancesCharges>
<itemPrice>15</itemPrice>
</invoiceLineItem>
</invoice>
Code: Select all
<sch:schema xmlns:sch="http://www.ascc.net/xml/schematron">
<sch:pattern name="Check calculation in invoicelineItem">
<sch:rule context="invoiceLineItem">
<sch:assert test="amountExclusiveAllowancesCharges = invoicedQuantity * itemPrice">The amountExclusiveAllowancesCharges calculation is incorrect.</sch:assert>
<sch:assert test="amountInclusiveAllowancesCharges = (invoicedQuantity * itemPrice) - invoiceAllowanceChargeAmount">The amountInclusiveAllowancesCharges calculation is incorrect.</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
I'll be grateful for any useful tips,
Ewa