3

Is it possible to write a schematron rule/assertion that will identify a space(s) at the start of an element? I need a way to flag elements that begin with a space for possible removal of the space, but I don't want to forcibly remove such spaces via XSLT, etc. Here's an example:

<section>
<paragraph> Here's some text.</paragraph>
</section>
wolfmason
  • 399
  • 1
  • 13

1 Answers1

3

Yes, Schematron uses XPath for assertions, so testing an element's string value for a leading space is easy:

  <pattern>
    <title>Paragraphs starting with a space</title>
    <rule context="paragraph">
      <report test="starts-with(., ' ')">
        This paragraph starts with a space: <value-of select="paragraph"/>
      </report>
    </rule>
  </pattern>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks! This asserts a space at the start, rather than reporting it, so I changed the to and it works well. (I may not have been clear, I'm looking to get rid of spaces when they don't belong.) – wolfmason Jun 23 '15 at 21:08
  • 1
    You're welcome. If this helped (modulo report vs assert), please remember to [**accept**](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). Thank you. – kjhughes Jun 23 '15 at 21:14
  • I do have a brief follow-up though (sorry!). After a bit more testing, I'm seeing that my validation runs into issues when there are more than one elements in the context of a single
    . I read up a bit more on starts-with, and I see that it retrieves a sequence of all matched nodes, which isn't allowed as the first arg. Is there a way to use this pattern to retrieve one match at a time? Or am I approaching this completely wrong now...
    – wolfmason Jun 23 '15 at 22:36
  • 2
    Sounds like you'd really like the context to be `paragraph` rather than `section`. If so, also adjust the `assert/@test` to be `starts-with(., ' ')`. – kjhughes Jun 23 '15 at 22:43
  • Perfect! Thanks again! – wolfmason Jun 24 '15 at 15:22
  • You're welcome. I went ahead and adjusted the pattern to use a context of `paragraph` and a test against the string value of the `paragraph` per your comments. Switched from assert to report too. – kjhughes Jun 24 '15 at 15:55