可选元素的Schematron计算测试

时间:2013-11-18 16:55:13

标签: xml optional schematron

我需要在XML Invoice中创建一组Schematron规则测试计算。问题是一些测试元素是可选的,当测试元素是mjissing时我的规则会失败。如何指定以下内容:“如果元素存在则测试值”?

XML实例摘录:

<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>

我的Schematron检查计算:

<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>

当缺少amountInclusiveAllowancesCharges和invoiceAllowanceChargeAmount时,它会失败(它们是可选的。

我会感激任何有用的提示,

埃娃

1 个答案:

答案 0 :(得分:0)

也许是迟到,但如果没有,或者其他人有类似的问题:

<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:rule>
    </sch:pattern>
    <sch:pattern name="Check calculation in invoicelineItem (inclusive)">
        <sch:rule context="invoiceLineItem[amountInclusiveAllowancesCharges][invoiceAllowanceChargeAmount]">
            <sch:assert test="amountInclusiveAllowancesCharges = (invoicedQuantity * itemPrice) - invoiceAllowanceChargeAmount">The amountInclusiveAllowancesCharges calculation is incorrect.</sch:assert>
        </sch:rule>
    </sch:pattern>
</sch:schema>