Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for a non-existent element in Schematron

Is it just me or is it impossible to look for a non existent element via Schematron. I also don't seem to be able to find any documentation on this as well.

Take the following rule:

    <sch:rule context="/A/B/C[@TYPE='TEST1']" id="identifier-required">
    identifier must be present
        <sch:assert test="not(.)" id="identifier-required">
    identifier-required: identifier must be present
        </sch:assert>
    </sch:rule>

And apply it against the following document:

<A>
   <B>
      <C TYPE="TEST2">TEST</C>
      <C TYPE="TEST3">TEST</C>
   </B>
</A>

In theory this should fail, however I've found it doesn't. Anyone know if this is correct behaviour?

like image 282
user898465 Avatar asked Nov 29 '25 16:11

user898465


1 Answers

It's certainly possible to check for the absence of an element in Schematron.

Your assertion doesn't fail because its rule context doesn't match.

If your rule matches, then necessarily . will exist, so <sch:assert test="not(.)"> will never pass anyway.

You might instead set your context instead to the parent of C and then assert that such a C not exist as a child:

<sch:rule context="/A/B" id="identifier-required">
    <sch:assert test="not(C[@TYPE='TEST1'])" id="identifier-required">
      identifier-required: identifier must be present
    </sch:assert>
</sch:rule>

But your diagnostic message suggests that you actually wish to assert that such a C be present, so perhaps what you really want is:

<sch:rule context="/A/B" id="identifier-required">
    <sch:assert test="C[@TYPE='TEST1']" id="identifier-required">
      identifier-required: identifier must be present
    </sch:assert>
</sch:rule>

which would fail with your given XML with the message, "identifier-required: identifier must be present".

like image 151
kjhughes Avatar answered Dec 02 '25 06:12

kjhughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!