Tesing number of decimal digits in schematron

Questions about XML that are not covered by the other forums should go here.
Iwicka
Posts: 11
Joined: Thu Nov 21, 2013 3:34 pm

Tesing number of decimal digits in schematron

Post 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
xephon
Posts: 140
Joined: Mon Nov 24, 2014 1:49 pm
Location: Greven/Germany

Re: Tesing number of decimal digits in schematron

Post 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>
stefan-jung.org – Your DITA/DITA-OT XML consultant
Iwicka
Posts: 11
Joined: Thu Nov 21, 2013 3:34 pm

Re: Tesing number of decimal digits in schematron

Post by Iwicka »

Great, it works.

Helpful as always -= Thank you :-)
Post Reply