Create a schematron to check for fig elements NOT to be wrapped in P elements

106 Views Asked by At

I am trying to create a schematron that will point out where a fig element has been wrapped around a p element. I prefer to have the p element end and then the fig element begin.

I have found a template to ensure the fig IS wrapped by the p element but I don't know how to change it to make it do the reverse.

Here is the template:

<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron"
queryBinding="xslt2">
  <sch:pattern>
      <sch:rule context="*[contains(@class, ' topic/fig ') and not(contains(@class, ' topic/fig 
   ut-d/imagemap '))]" role="warn">
      <sch:let name="precedingText" value="preceding-sibling::text()"/>
      <sch:let name="followingText" value="following-sibling::text()"/>
      <sch:assert test=".[parent::p][count(parent::node()/child::*) = 1]
          [not($precedingText) or $precedingText[normalize-space()='']]
          [not($followingText) or $followingText[normalize-space()='']]" >
          The fig element should be wrapped in a paragraph.</sch:assert>
      </sch:rule>
  </sch:pattern>
</sch:schema>
3

There are 3 best solutions below

0
On BEST ANSWER

You could use this:

<sch:pattern>
  <sch:rule context="*[contains(@class, 'topic/fig')]
                      [not(contains(@class, 'topic/fig ut-d/imagemap'))]"
            role="warn">
    <sch:assert test="not(parent::p)">Figures should be outside p elements.</sch:assert>
  </sch:rule>
</sch:pattern>

And if you want more exact matching of the @class string sections, you can try the tokenize() function (which is an XPath2 function and therefore available for your XSLT2-based language bindinging.)

1
On

If you change sch:assert with sch:report you will get the reverse check. Or add the not() function around the entire XPath value of the @test attribute.

0
On

Maybe the rule should be something like this:

<sch:pattern>
<sch:rule
    context="*[contains(@class, ' topic/p ')]/*[contains(@class, ' topic/fig ') and not(contains(@class, ' topic/fig ut-d/imagemap '))]">
    <sch:report test="true()" role="warn">The fig element should NOT be wrapped in a
        paragraph.</sch:report>
</sch:rule></sch:pattern>

I ignored in the rule the part with the text before and text after the fig element. I am not sure if this is important in your case.