Page 1 of 1

Schematron assertions for specific words/phrases and quickfix help

Posted: Mon Aug 14, 2017 8:38 pm
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>

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

Posted: Tue Aug 15, 2017 9:08 am
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

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

Posted: Tue Aug 15, 2017 3:36 pm
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?

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

Posted: Tue Aug 15, 2017 5:00 pm
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

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

Posted: Tue Aug 15, 2017 6:15 pm
by cedporter
Perfect, thanks!