SLIDE 75 The Need for Named Patterns The Need for Named Patterns
- So far, we have defined elements exactly at the
point that they can be used
- There is no equivalent of:
- <!ELEMENT person (name)>
<!ELEMENT name (firstName, lastName)> ...use person several places in the DTD...
- With the RELAX NG we have discussed so far,
each time we want to include a person, we would need to explicitly define both person and name at that point:
<element name="firstName"> <text/> </element> <element name="lastName"> <text/> </element> </element>
- The <grammar> element solves this problem
- So far, we have defined elements exactly at the
point that they can be used
- There is no equivalent of:
- <!ELEMENT person (name)>
<!ELEMENT name (firstName, lastName)> ...use person several places in the DTD...
- With the RELAX NG we have discussed so far,
each time we want to include a person, we would need to explicitly define both person and name at that point:
<element name="firstName"> <text/> </element> <element name="lastName"> <text/> </element> </element>
- The <grammar> element solves this problem
Syntax of <grammar> Syntax of <grammar>
<grammar xmlns="http://relaxng.org/ns/structure/1.0"> <start> ...usual RELAX NG elements, which may include: <ref name="DefinedName"/> </start> <!-- One or more of the following: --> <define name="DefinedName"> ...usual RELAX NG elements, attributes, groups, etc. </define> </grammar> <grammar xmlns="http://relaxng.org/ns/structure/1.0"> <start> ...usual RELAX NG elements, which may include: <ref name="DefinedName"/> </start> <!-- One or more of the following: --> <define name="DefinedName"> ...usual RELAX NG elements, attributes, groups, etc. </define> </grammar>
Use of <grammar> Use of <grammar>
- To write a <grammar>,
- Make <grammar> the root element of your
specification
xmlns="http:/ / relaxng.org/ ns/ structure/ 1.0"
- Use, as the <start> element, a pattern that
matches the entire (valid) XML document
- In each <define> element, write a pattern that you
want to use other places in the specification
- Wherever you want to use a defined element, put
<ref name="NameOfDefinedElement">
- Note that defined elements may be used in
definitions, not just in the <start> element
- Definitions may even be recursive, but
- Recursive references must be in an element,
not an attribute
- To write a <grammar>,
- Make <grammar> the root element of your
specification
xmlns="http:/ / relaxng.org/ ns/ structure/ 1.0"
- Use, as the <start> element, a pattern that
matches the entire (valid) XML document
- In each <define> element, write a pattern that you
want to use other places in the specification
- Wherever you want to use a defined element, put
<ref name="NameOfDefinedElement">
- Note that defined elements may be used in
definitions, not just in the <start> element
- Definitions may even be recursive, but
- Recursive references must be in an element,
not an attribute
Long Example of <grammar> Long Example of <grammar>
- <!ELEMENT name (firstName, lastName)>
- <grammar xmlns="http://relaxng.org/ns/structure/1.0">
<start> <ref name="Name"/> </start> <define name="Name"> <element name="name"> <element name="firstName"> <text/> </element> <element name="lastName"> <ref name="LastName"> </element> </element> </define> <define name="LastName"> <element name="lastName"> <text/> </element> </define> </grammar>
- <!ELEMENT name (firstName, lastName)>
- <grammar xmlns="http://relaxng.org/ns/structure/1.0">
<start> <ref name="Name"/> </start> <define name="Name"> <element name="name"> <element name="firstName"> <text/> </element> <element name="lastName"> <ref name="LastName"> </element> </element> </define> <define name="LastName"> <element name="lastName"> <text/> </element> </define> </grammar>
XML is case sensitive-- Note that defined terms are capitalized differently