Tesing number of decimal digits in schematron

Questions about XML that are not covered by the other forums should go here.
Iwicka
Posts: 11

Tesing number of decimal digits in schematron

Fri Sep 08, 2017 5:21 pm

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: 95
Location: Hamburg/Germany

Re: Tesing number of decimal digits in schematron

Sat Sep 09, 2017 5:21 pm

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>
Join the DOCTALES DITA development team :arrow: https://doctales.atlassian.net/wiki
Iwicka
Posts: 11

Re: Tesing number of decimal digits in schematron

Mon Sep 11, 2017 11:43 am

Great, it works.

Helpful as always -= Thank you :-)

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 1 guest