>> English << | českyZVON > Tutorials > Schematron Tutorial
>> Example 16 << | Prev | Next | Contents

Author: Jelliffe Rick

Description

Examples of Patterns for ID/IDREF Checking

Schema


<schema xmlns="http://www.ascc.net/xml/schematron" >
     <pattern name="Required ID">
          <rule context="cat | dog">
               <assert test="@id">An element of type
                    <name/> should have an id attribute that is a unique identifier for that animal.
               </assert>
          </rule>
     </pattern>
     <pattern name="Required IDREF">
          <rule context="catowner">
               <assert test="@pet">An element of type
                    <name/> should have a pet attribute.
               </assert>
          </rule>
     </pattern>
     <pattern name="IDREF should reference an ID used in the same document">
          <rule context="catowner[@pet]">
               <assert test="id(@pet)">An element of type
                    <name/> should have a pet attribute that should contain a unique identifier.
               </assert>
          </rule>
     </pattern>
     <pattern name="IDREF should reference an ID of a certain element type only">
          <rule context="catowner[@pet]">
               <assert test="(name(id(@pet)) ='cat')">An element of type
                    <name/> should have a pet attribute that should contain the unique identifier for a cat.
               </assert>
          </rule>
     </pattern>
</schema>

Sources and outputs

Source (XML 1)Output


<!DOCTYPE ideg
[
<!ELEMENT ideg    ( dog, cat, catowner+ )>
<!ELEMENT dog     ( #PCDATA )>
<!ELEMENT cat      ( #PCDATA )>
<!ELEMENT catowner ( #PCDATA )>
<!ATTLIST dog  id ID #REQUIRED >
<!ATTLIST cat   id ID #REQUIRED >
<!ATTLIST catowner  pet IDREF #REQUIRED>
]>
<ideg>
        <dog  id="danny"            >Danny</dog>
        <cat   id="bede"              >Bede</cat>
        <catowner >Harold</catowner>
        <catowner pet="poochi" >Fumiaki</catowner>
        <catowner pet="danny"   >Jimmy</catowner>
        <catowner pet="bede"    >Rick</catowner>
</ideg>

 Pattern: Required ID

 Pattern: Required IDREF
/ideg/catowner[1]: An element of type catowner should have a pet attribute.

 Pattern: IDREF should reference an ID used in the same document
/ideg/catowner[2]: An element of type catowner should have a pet attribute that should contain a unique identifier.

 Pattern: IDREF should reference an ID of a certain element type only
/ideg/catowner[2]: An element of type catowner should have a pet attribute that should contain the unique identifier for a cat.
/ideg/catowner[3]: An element of type catowner should have a pet attribute that should contain the unique identifier for a cat.