Schematron assertions for specific words/phrases and quickfix help

Questions about XML that are not covered by the other forums should go here.
cedporter
Posts: 18
Joined: Thu Mar 16, 2017 8:44 pm

Schematron assertions for specific words/phrases and quickfix help

Post by cedporter »

We have several common words or phrases we general wrap with <i> when they appear in text. I am trying to write a schematron rule to enforce this rule and suggest a quick fix to wrap the word or phrase in <i>. The following code works, but the warning underline underlines the beginning of the entire element surrounding the appearance of "Assay", and it necessitates writing an assertion for literally every word separately. Is there a way to generate the warning under "Assay" itself in a text() node?

More importantly, I would love any suggestions for a schematron quickfix that would wrap the offending word in an <i> and preserve the rest of the content in the tag.

Code: Select all


<pattern>
<title>Italic suggestion</title>
<rule context="*[not(ancestor-or-self::title) and not(ancestor-or-self::i)]/text()" role="warn">
<assert test="not(matches(., 'Assay'))">Assay is generally italicized when referenced in text</assert>
</rule>
</pattern>
tavy
Posts: 364
Joined: Thu Jul 01, 2004 12:29 pm

Re: Schematron assertions for specific words/phrases and quickfix help

Post by tavy »

Hello,

If you want to wrap an word in an element I recommend you to use the sqf:stringReplace operation. You can match the word that you want to wrap and replace it with a fragment. The quick fix should look something like this:

Code: Select all


<sch:rule context="*[not(ancestor-or-self::title) and not(ancestor-or-self::i)]/text()" role="warn">
<sch:assert test="not(matches(., 'Assay'))" sqf:fix="wrapWord">Assay is generally italicized when referenced in text</sch:assert>

<sqf:fix id="wrapWord">
<sqf:description>
<sqf:title>Wrap 'Assay' word in italic</sqf:title>
</sqf:description>
<sqf:stringReplace regex="Assay">
<i>Assay</i>
</sqf:stringReplace>
</sqf:fix>
</sch:rule>
Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
cedporter
Posts: 18
Joined: Thu Mar 16, 2017 8:44 pm

Re: Schematron assertions for specific words/phrases and quickfix help

Post by cedporter »

Thanks, I figured out that stringReplace could insert elements yesterday, so we reached the same conclusion!

To my other question, is there any way to make a template for this, and supply a list of words rather than creating separate assertions for numerous words individually?
tavy
Posts: 364
Joined: Thu Jul 01, 2004 12:29 pm

Re: Schematron assertions for specific words/phrases and quickfix help

Post by tavy »

Hello,

You can create an abstract pattern and then for each word you need to instantiate the pattern and pass the word as parameter. Something like this:

Code: Select all


    <sch:pattern id="warap" abstract="true">
<sch:title>Italic suggestion</sch:title>
<sch:rule context="*[not(ancestor-or-self::title) and not(ancestor-or-self::i)]/text()" role="warn">
<sch:assert test="not(matches(., '$word'))" sqf:fix="wrapWord">$word is generally italicized when referenced in text</sch:assert>

<sqf:fix id="wrapWord">
<sqf:description>
<sqf:title>Wrap '$word' word in italic</sqf:title>
</sqf:description>
<sqf:stringReplace regex="$word">
<i>$word</i>
</sqf:stringReplace>
</sqf:fix>
</sch:rule>
</sch:pattern>

<sch:pattern is-a="warap">
<sch:param name="word" value="Assay"/>
</sch:pattern>
<sch:pattern is-a="warap">
<sch:param name="word" value="OtherWord"/>
</sch:pattern>
Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
cedporter
Posts: 18
Joined: Thu Mar 16, 2017 8:44 pm

Re: Schematron assertions for specific words/phrases and quickfix help

Post by cedporter »

Perfect, thanks!
Post Reply