Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be...

33
Schematron Tim Bornholtz

Transcript of Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be...

Page 1: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Schematron

Tim Bornholtz

Page 2: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Schema languages• Many people turn to schema languages

when they want to be sure that an XML instance follows certain rules– DTD– XML Schema– Relax NG

Page 3: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

What is wrong with that?• These schema languages have very

complicated grammars

• Not flexible enough to accurately reflect real business rules

• Often very difficult to map the business rules written in English to the technical rules defined by the Schema

Page 4: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

What is Schematron• Schematron is a language that allows you to

directly express rules• Without a whole grammatical infrastructure• Schematron is an ISO standard• Largest benefit is the ability to describe dynamic

constraints• Schematron can describe syntax constraints like

XML Schema • Can also describe semantic constraints which

XML Schema cannot describe

Page 5: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Comparing Schema Languages

• White is core capability• Light gray represents

capabilities that arepossible butinconvenient

• Dark gray capabilitiesrequire specializedschemaorganization

Page 6: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Attributes & ElementsAttributesFeature XSD SchematronRequired Yes YesOptional Yes YesAttribute conditional definition No YesElement conditional definition No Yes

ElementsFeature XSD SchematronCardinality Yes YesRepetition Yes YesSequence Yes YesInterleave Yes YesList Yes YesMixed Content Yes YesEmpty Content Yes YesAttribute conditional definition No YesElement conditional definition No YesChoice Yes YesExplicit null Yes Yes

Page 7: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Data TypesData TypesFeature XSD SchematronNumber of built-in types 46 0User defined data types Yes NoDomain constraint Yes Yes

Expressive PowerFeature XSD SchematronLocal tree grammar Yes PartialUnique typing Yes NoEfficient validation Yes Yes

NormalizationFeature XSD SchematronDefault value for attributes Yes NoDefault value for elements Partial NoWhitespace normalization Yes No

Page 8: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

General FeaturesGeneral Language FeaturesFeature XSD SchematronXML based Yes YesNamespace support Yes YesRule-based No YesGrammar-based Yes NoDynamic (semantic) constraints No YesFormal semantic No YesSize of spec (in pages) 307 35

Page 9: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Why Schematron in Financial Aid?

• We trade many files with our partners and these are increasingly XML.

• Schematron is more expressive than other schema languages

• Can handle more complicated dependencies that XML Schema cannot.

Page 10: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Pre-requisites• I'm going to assume that you're familiar

with:– XML– XML Namespaces– XPath– XSLT

Page 11: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Components of a Schematron file• <schema>

– <phase> - top level construct

• <pattern>

–<rule> - defines the context

»<assert>

»<report>

Page 12: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

<assert>• Assert is the basic rule within a Schematron file to

test for a valid condition• Message will be displayed whenever the test is

false• Syntax:

<assert test=”expression to evaluate”>Message to display if the expression is false</assert>

• Any XSLT or XPath expression can be used including boolean logic and complicated formulas

Page 13: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

<report>• Use the <report> instruction to

communicate information apart from the validation errors

• The message will be displayed when the test condition is true.

• Syntax:<report test=”expression to

evaluate”>Message to display if the expression is true</report>

Page 14: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

<rule>• Basic building block of a Schematron file.• Syntax:

<rule context=”Location in the file”></rule>– <rule context=”Award”>

– <rule context=”Student”>

• The context can be any valid XPattern– XPattern is a subset of XPath

– XPattern is the way to identify templates in XSLT

• All of the <assert> statements for a particular rule context are grouped together

Page 15: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

<pattern>• A <pattern> is a collection of related rules.• Syntax:

<pattern name=”Descriptive name of the rules”>• The rules within a pattern do not need to work

on the same elements.• The name will be displayed in the output.• The name will help you identify which section

of the document is failing the rules.

Page 16: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

<schema>• Root element of the document

• Schematron 1.5 must use the namespace: http://www.ascc.net/xml/schematron

• ISO Schematron must use the namespace:http://purl.oclc.org/dsdl/schematron

Page 17: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Very simple example <?xml version="1.0" encoding="UTF-8"?>

<schema xmlns="http://www.ascc.net/xml/schematron"> <title>Example 1</title> <pattern name="Document root"> <rule context="/"> <assert test="doc">Root element should be "doc".</assert> </rule> </pattern></schema>

Page 18: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Basic assertions

• Schematron can validate many basic conditions– Presence of elements– Absence of elements– Sequence of elements within an element– Relative order of elements to each other– Validate for a certain number of elements

Page 19: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Presence or absence of tags<assert test=”LoanType”>• Require that the loan type be an element that is a

direct child element of the current context

<assert test=”count(LoanType) = 0”>• Require that the LoanType element is not

present

<assert test=”count(*) = 5”>• Require that there are exactly 5 child elements

of the current context

Page 20: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Order of elements

<rule context="Student">

<assert test="following-sibling::*[1]/self::FFELPLUS">

A "title" must be immediately followed by a "subtitle".

</assert>

</rule>

Page 21: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Taking your validation further

• Schematron can do may things that XML Schemas cannot handle– Cross field relationships– Meaningful error messages

Page 22: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Cross Field Relationships

• XML Schemas can validate the contents and the data type of one field

• Not able to easily validate relationships between fields.– If field A contains a value X then field B

must contain a value Y

Page 23: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

PLUS Student cannot be Borrower• <rule context="FFELPPLUS">

<assert test="Borrower/Index/SSN != ../Student/Index/SSN">Borrower for a PLUS loan must not be the student</assert></rule>

• FFEPGradPLUS the borrower must be the student

• <rule context="FFELPGradPLUS"> <assert test="Borrower/Index/SSN = ../Student/Index/SSN">Borrower for a Grad PLUS loan must be the student</assert></rule>

Page 24: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Grad PLUS Grade Level• If a student is getting a GradPLUS loan, they must be a

graduate student.

<rule context="FFELPGraduatePLUS"> <assert test="StudentLevelCode = 'FirstYearGraduate' or StudentLevelCode='SecondYearGraduate' or StudentLevelCode='ThirdYearGraduate' or StudentLevelCode='FourthYearGraduate' or StudentLevelCode='ContinuingGraduate'"> Student must be a Graduate level for FFELPGradPLUS award</assert></rule>

Page 25: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Stafford Loan Borrower• For a Stafford loan, the Borrower is the

Student• No Borrower section is sent (different than

GradPLUS)

<rule context="FFELPStafford"> <assert test="count(Borrower) = 0"> Borrower section not necessary for Stafford loan</assert></rule>

Page 26: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Date Ranges

• Checking validity of two related dates• <xsl:import>date.difference.function.xsl</xsl:import>

<assert test=" date:difference(FinancialAwardBeginDate, ../FFELPSubsidized/Disbursement[@Number=1] /DisbursementDate) le 30"> Award Begin Date must be within 30 days of first disbursement</assert>

Page 27: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Meaningful messages• To get the name of the XML element used in

an <assert> or <report> statement use the <name/> tag

• The <value-of> tag will display the actual value of the element.

<rule context="FFELPStafford"> <assert test=“length(Email) > 128"> <name/> value ‘<value-of/>’ is not valid </assert></rule>

Page 28: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Phases

• If we were to combine all of the business rules for CR:C into one schema it would be a very large file.

• A <phase> is a simple collection of patterns that are executed together.

Page 29: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Namespaces• Schematron can be used to validate XML that

uses namespaces• Declare the namespace as a child of the

<schema> element<ns

uri=”urn:org:pesc:message:CommonLineRequest:v1.1.0” prefix=”req”>

• Then use the namespace like normal<assert test=”count(req:LoanType) = 1”>

Page 30: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Running the examples• There are implementations available for many

languages: .Net, Java, Python, Perl, Ruby• The 1.5 reference implementation compiles

the Schematron file into a XSLT file that can be used against the XML instance document.

• Most any XSLT engine can be used with the reference implementation but the fastest are Saxon for Java and MSXSLT for .Net

Page 31: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Running the examples (cont.)

I'm using Saxon for Java but the process is similar for most all XSLT processors

1) java -jar saxon8.jar -o temp.xsl file.sch schematron-basic.xsl

2) java -jar saxon8.jar instance.xml temp.xsl

3) rm temp.xsl

Page 32: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

• Schematron provides rule processing capabilities that other schema languages cannot provide

• Schematron can be used in conjunction with XML Schemas– Continue to use the XML Schemas that are

already defined – Define complex rules with Schematron

Page 33: Schematron Tim Bornholtz. Schema languages Many people turn to schema languages when they want to be sure that an XML instance follows certain rules –DTD.

Contact Information

Tim Bornholtz

President, The Bornholtz GroupEmail: [email protected]

Phone: (540) 446-8404Web: http://www.bornholtz.com