Jeff
Jeff

Reputation: 887

Exclusive XPath test

I'm trying to write a Schematron rule to perform the following test...

If no li under root contains "abc" and root contains title, "def" then report.

The problem that I'm having is that I am getting many false positives. Here is my current XPath...

<rule context="li">
                <assert test="not(contains(text(),'abc')) and ancestor::root/descendant::title = 'def'">Report this.</assert>
            </rule>

My output ends up reporting on each li that does not contain "abc" which I understand since it is testing every li and reporting.

However, I don't know how to write the XPath so that I can test if any li contains "abc".

Thanks!

Upvotes: 1

Views: 105

Answers (1)

LarsH
LarsH

Reputation: 28004

The problem, as you hinted at, is that you've expressed this in Schematron as a rule that applies to each li element; whereas the rule you've described in English is one that applies to each root element.

So you could express it as

<rule context="root">
  <assert test=".//li[contains(text(),'abc')] or
                not(.//title = 'def')">Report this.</assert>
</rule>

Note that I've flipped the sense of the test, to match your English description

If no li under root contains "abc" and root contains title, "def" then report.

Since you're using an <assert> element, you assert the opposite of what you want to report. It might make more sense to use a <report> element:

<rule context="root">
  <report test="not(.//li[contains(text(),'abc')]) and
                .//title = 'def'">Report this.</report>
</rule>

Upvotes: 3

Related Questions