Page 1 of 1

Tesing number of decimal digits in schematron

Posted: Fri Sep 08, 2017 5:21 pm
by Iwicka
Hi,

I have another interesting Schematron issue: I need to test that the numeric value should have not more than 3 decimal positions. The data type is xsd:decimal, so there may be also no decimal digits, which makes it more difficult to check (to me anyway). My XML is:

Code: Select all


<A>
<measurement>
<height>5.11</height>
</measurement>
<measurement>
<height>5</height>
</measurement>
</A>
The correct values would be: 5, 5.1, 5.11, but not 5.111

I created the following test using regular expression:

Code: Select all

    
<sch:rule context="//measurement">
<sch:assert test="height[matches(., '[\.][\d]{2}$')]">Maximum 2 decimal digits are allowed</sch:assert>
</sch:rule>
but this requires exactly 2 decimals, which is not the intention. How can I say 0, 1 or 2 decimal digits?

Best Regards,

Ewa

Re: Tesing number of decimal digits in schematron

Posted: Sat Sep 09, 2017 5:21 pm
by xephon
Hi Ewa,

this should work

The test counts the chars after the '.'.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2"
xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
<sch:pattern>
<sch:rule context="measurement">
<sch:report test="string-length(normalize-space(substring-after(., '.'))) gt 3">
More than 3 decimal positions
</sch:report>
</sch:rule>
</sch:pattern>
</sch:schema>

Re: Tesing number of decimal digits in schematron

Posted: Mon Sep 11, 2017 11:43 am
by Iwicka
Great, it works.

Helpful as always -= Thank you :-)