3

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?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
user898465
  • 944
  • 1
  • 12
  • 23

2 Answers2

2

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

kjhughes
  • 106,133
  • 27
  • 181
  • 240
1

A rule whose context never matches anything will never fire and will never report an error. You need to define a rule with some context that does exist (e.g. "/") and assert that with that context, there must be at least one node selected by the expression A/B/C[@TYPE='TEST1']

Michael Kay
  • 156,231
  • 11
  • 92
  • 164