Check the number of words

In this case, we want to create a rule that checks the number of words in the description element of an XML document. The rule should ensure that the description contains less than 50 words. If the description contains more than 50 words, the rule should fail and report an error. The implementation uses XPath functions to tokenize the string and count the words.

Example 10. Rule that verifies the number of words from the shortdesc element

<sch:rule context="shortdesc">
    <sch:report test="count(tokenize(.,'\s+')) > 50">
         The description must contain less than 50 words.</sch:report>
</sch:rule>


You can also provide a fix for the error by suggesting a corrected version of the description that contains less than 50 words. The AI can help you to do this by reformulating the phrase.

Example 11. SQF fix that reformulates the phrase to have less than 50 words

<sqf:fix id="rephrase">
    <sqf:description>
        <sqf:title>Reformulate phrase to contain less than 50 words</sqf:title>
    </sqf:description>
    <sqf:replace match="text()" select="ai:transform-content(
           'Reformulate phrase to contain less than 50 words', .)"/>
</sqf:fix>