Schematron - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Schematron

Description:

title Example 1 /title pattern name='Document root' rule context ... Most any XSLT engine can be used with the reference implementation but the ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 34
Provided by: timb170
Category:

less

Transcript and Presenter's Notes

Title: Schematron


1
Schematron
  • Tim Bornholtz

2
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

3
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

4
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

5
Comparing Schema Languages
  • White is core capability
  • Light gray representscapabilities that
    arepossible butinconvenient
  • Dark gray capabilitiesrequire
    specializedschemaorganization

6
Attributes Elements
7
Data Types
8
General Features
9
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.

10
Pre-requisites
  • I'm going to assume that you're familiar with
  • XML
  • XML Namespaces
  • XPath
  • XSLT

11
Components of a Schematron file
  • ltschemagt
  • ltphasegt - top level construct
  • ltpatterngt
  • ltrulegt - defines the context
  • ltassertgt
  • ltreportgt

12
ltassertgt
  • Assert is the basic rule within a Schematron file
    to test for a valid condition
  • Message will be displayed whenever the test is
    false
  • Syntaxltassert testexpression to
    evaluategtMessage to display if the expression is
    falselt/assertgt
  • Any XSLT or XPath expression can be used
    including boolean logic and complicated formulas

13
ltreportgt
  • Use the ltreportgt instruction to communicate
    information apart from the validation errors
  • The message will be displayed when the test
    condition is true.
  • Syntax
  • ltreport testexpression to evaluategtMessage to
    display if the expression is truelt/reportgt

14
ltrulegt
  • Basic building block of a Schematron file.
  • Syntax
  • ltrule contextLocation in the filegtlt/rulegt
  • ltrule contextAwardgt
  • ltrule contextStudentgt
  • 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 ltassertgt statements for a particular
    rule context are grouped together

15
ltpatterngt
  • A ltpatterngt is a collection of related rules.
  • Syntax
  • ltpattern nameDescriptive name of the rulesgt
  • 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.

16
ltschemagt
  • Root element of the document
  • Schematron 1.5 must use the namespace
    http//www.ascc.net/xml/schematron
  • ISO Schematron must use the namespacehttp//purl
    .oclc.org/dsdl/schematron

17
Very simple example
  • lt?xml version"1.0" encoding"UTF-8"?gtltschema
    xmlns"http//www.ascc.net/xml/schematron"gt
    lttitlegtExample 1lt/titlegt ltpattern
    name"Document root"gt ltrule context"/"gt
    ltassert test"doc"gtRoot element should
    be
    "doc".lt/assertgt lt/rulegt
    lt/patterngtlt/schemagt

18
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

19
Presence or absence of tags
  • ltassert testLoanTypegt
  • Require that the loan type be an element that is
    a direct child element of the current context
  • ltassert testcount(LoanType) 0gt
  • Require that the LoanType element is not present
  • ltassert testcount() 5gt
  • Require that there are exactly 5 child elements
    of the current context

20
Order of elements
  • ltrule context"Student"gt
  • ltassert test"following-sibling1/selfFFELPL
    US"gt
  • A "title" must be immediately followed by a
    "subtitle".
  • lt/assertgt
  • lt/rulegt

21
Taking your validation further
  • Schematron can do may things that XML Schemas
    cannot handle
  • Cross field relationships
  • Meaningful error messages

22
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

23
PLUS Student cannot be Borrower
  • ltrule context"FFELPPLUS"gt ltassert
    test"Borrower/Index/SSN !
    ../Student/Index/SSN"gtBorrower for a PLUS
    loan must not be the studentlt/assertgtlt/rulegt
  • FFEPGradPLUS the borrower must be the student
  • ltrule context"FFELPGradPLUS"gt ltassert
    test"Borrower/Index/SSN
    ../Student/Index/SSN"gtBorrower for a Grad
    PLUS loan must be the studentlt/assertgtlt/rulegt

24
Grad PLUS Grade Level
  • If a student is getting a GradPLUS loan, they
    must be a graduate student.
  • ltrule context"FFELPGraduatePLUS"gt ltassert
    test"StudentLevelCode 'FirstYearGraduate' or
    StudentLevelCode'SecondYearGraduate' or
    StudentLevelCode'ThirdYearGraduate' or
    StudentLevelCode'FourthYearGraduate' or
    StudentLevelCode'ContinuingGraduate'"gt Student
    must be a Graduate level for FFELPGradPLUS
    awardlt/assertgtlt/rulegt

25
Stafford Loan Borrower
  • For a Stafford loan, the Borrower is the Student
  • No Borrower section is sent (different than
    GradPLUS)?
  • ltrule context"FFELPStafford"gt ltassert
    test"count(Borrower) 0"gt Borrower
    section not necessary for Stafford
    loanlt/assertgtlt/rulegt

26
Date Ranges
  • Checking validity of two related dates
  • ltxslimportgtdate.difference.function.xsllt/xslimpo
    rtgtltassert test" datedifference(FinancialAwardB
    eginDate, ../FFELPSubsidized/Disbursement_at_Number
    1 /DisbursementDate) le 30"gt Award Begin
    Date must be within 30 days of first
    disbursementlt/assertgt

27
Meaningful messages
  • To get the name of the XML element used in an
    ltassertgt or ltreportgt statement use the ltname/gt
    tag
  • The ltvalue-ofgt tag will display the actual value
    of the element.
  • ltrule context"FFELPStafford"gt ltassert
    testlength(Email) gt 128"gt ltname/gt
    value ltvalue-of/gt is not valid lt/assertgtlt/rulegt

28
Phases
  • If we were to combine all of the business rules
    for CRC into one schema it would be a very large
    file.
  • A ltphasegt is a simple collection of patterns that
    are executed together.

29
Namespaces
  • Schematron can be used to validate XML that uses
    namespaces
  • Declare the namespace as a child of the ltschemagt
    element
  • ltns uriurnorgpescmessageCommonLineRequestv1
    .1.0 prefixreqgt
  • Then use the namespace like normal
  • ltassert testcount(reqLoanType) 1gt

30
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

31
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

32
  • 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

33
Contact Information
  • Tim Bornholtz
  • President, The Bornholtz GroupEmail
    tim_at_bornholtz.comPhone (540) 446-8404Web
    http//www.bornholtz.com
Write a Comment
User Comments (0)
About PowerShow.com