Validate multiple root elements with Schematron

This should cover W3C XML Schema, Relax NG and DTD related problems.
cjhukill
Posts: 11

Validate multiple root elements with Schematron

Mon Mar 27, 2017 6:14 pm

Hi,

I'm building an xml document that conforms to my xsd with two root elements in the file:

[Codebox]<my:Submission xmlns:....

<office:Event>
<office:Date>20170601</office:Date>
.
.
.
</office:Event>

<staff:Person>
<staff:Name>Doe, John</staff:Name>
<staff:Available>
<staff:DateBegin>20170101</staff:DateBegin>
<staff:DateEnd>20170704</staff:DateEnd>
</staff:Available>
.
.
.
</staff:Person>
</my:Submission>
[/Codebox]

I have created a schematron file to look at the event date and see who is available for that event. It seems I can write validation routines for the office:Date to ensure that it is within the calendar date. But I can not check anything else. All the 'context' in the rules that specify the staff elements, such as the available date (staff:Person/staff:Available/staff:DateBegin) will not 'fire'. I can not get to the 'report' or 'assert' statements in the rule. Is there something in the schematron file that we need to add to allow it to go past the office:Event elements and check the other staff:Person elements?

Any help will be greatly appreciated!!
tavy
Posts: 122

Re: Validate multiple root elements with Schematron

Tue Mar 28, 2017 9:00 am

Hello,

You need to send me also a sample Schematron file in order to find the problem.
Is the Schematron embedded in XSD or is in a separate file? Are the rules in different patterns, and you use phases?
Make sure that the namepaces for the "office" and "staff" are declared correctly in the Schematron file.
Does the Schematron example below work for you, if you replace "OFFICE_NS" and "STAFF_NS" with the correct namespace?

Code: Select all

<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2"
    xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
    <sch:ns uri="OFFICE_NS" prefix="office"/>
    <sch:ns uri="STAFF_NS" prefix="staff"/>
    <sch:pattern>
        <sch:rule context="office:Date">
            <sch:assert test="false()">Start date: <sch:value-of select="."/></sch:assert>
        </sch:rule>
       
        <sch:rule context="staff:Person/staff:Available/staff:DateBegin">
            <sch:assert test="false()">Date begin: <sch:value-of select="."/></sch:assert>
        </sch:rule>
    </sch:pattern>
</sch:schema>



Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
cjhukill
Posts: 11

Re: Validate multiple root elements with Schematron

Tue Mar 28, 2017 3:04 pm

My schematron file is separate. I declare it at the beginning of the XML file:

<?xml-model href="events.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>

It looks like this:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:saxon="http://saxon.sf.net">
   <sch:ns uri="http://release.com/office/1.0/" prefix="office"/>
   <sch:ns uri="http://release.com/staff/1.0/" prefix="staff"/>
   <sch:let name="currentDate" value="format-date(current-date(), '[Y0001][M01][D01]')"/>
   <sch:pattern>
      <sch:rule context="office:Date">
         <sch:assert test="(number(.) &gt; number($currentDate))">
            Date must be in the future
         </sch:assert>
      </sch:rule>
   </sch:pattern>
   <sch:pattern>
      <sch:rule context="staff:Person/staff:Available">
         <sch:let name="availableBegin" value="number(staff:DateBegin)"/>
         <sch:let name="availableEnd" value="number(staff:DateEnd)"/>
         <sch:let name="eventDate" value="number(//office:Date)"/>
         <sch:assert test="($availableBegin &lt;= $eventDate) and ($eventDate &lt;= $availableEnd)">
            Employee <sch:value-of select="string(../staff:Name)"/> can not attend event</sch:assert>
      </sch:rule>
   </sch:pattern>
</sch:schema>


Currently, it only gives me an error on the first pattern, office:Date. I have moved the patterns around to have the office:Date checked last, and it still only checks the office:Date. It does not find any errors in the staff data. I tried your suggestion and it only produces the line for the office:Date, not the staff:Available/staff:DateBegin. I have put both rules in the same pattern and it only hits the office:Date rule. I have placed the office:Date below the staff:Available/staff:DateBegin and it only hits on the office:Date. If I remove the office namespace from everything and the xsd, it will test the staff elements. If I remove the staff namespace from everything and the xsd, it test the office elements. But when I have them both together, it only test the one that is the first occurrence (as I have also reversed the occurrence of the staff and office in the xsd). Is there something specific I can look for in my xsd that may be causing this?

Thanks in advance for all the help provided!!
tavy
Posts: 122

Re: Validate multiple root elements with Schematron

Tue Mar 28, 2017 4:00 pm

Hello,

Unfortunately I cannot reproduce the problem. For me it works fine. If I validate the following XML with the Schematron that you provided I get both errors, on "office:Date" and "staff:Available". The only thing that I can think of is to have a different namespace URI for the "staff" in the XML file and Schematron, maybe a space at the end (make sure that are identical).
Please send me an email on support at oxygenxml dot com, with a sample XML file, XSD schema and Schematron, so I can reproduce the problem.
What oXygen version do you use? I have tested on oXygen XML Editor 18.1 build 2016102619.
Do you have any options modified in the oXygen Schematron option page?

Code: Select all

<my:Submission xmlns:my="http://release.com/my/1.0/"
    xmlns:office="http://release.com/office/1.0/"
    xmlns:staff="http://release.com/staff/1.0/">
    <office:Event>
        <office:Date>20170201</office:Date>
    </office:Event>
    <staff:Person>
        <staff:Name>Doe, John</staff:Name>
        <staff:Available>
            <staff:DateBegin>20170101</staff:DateBegin>
            <staff:DateEnd>20170104</staff:DateEnd>
        </staff:Available>
    </staff:Person>
</my:Submission>


Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
cjhukill
Posts: 11

Re: Validate multiple root elements with Schematron

Wed Mar 29, 2017 2:34 pm

Thank you so much for all your help and suggestions. I found that my namespace URI was incorrect. Once I double checked those, I was able to validate my file and produce the correct reports.

Thanks again for all your help!!

Return to “XML Schemas”

Who is online

Users browsing this forum: No registered users and 0 guests