xmlschematronoxygenxml

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


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>

Solution

  • 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.)