Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

3.10 Summary examples

This section presents examples of complete documents that bring together everything we've discussed in the last two chapters (and more). The content is fictitious, but the structure and markup are from real web site projects (somewhat abridged for readability).

3.10.1 Page document

Compared to the master document example (3.10.2), the page document in Example 3.1 is short and simple. This is, in fact, what you should strive for in your project. Page documents are the primary work area for those who will update and maintain the site, so the layout of a page document must be as simple and self-evident as possible. (For instance: Do we need indentation in page documents? Probably not, unless it is taken care of automatically.)

The main rule of thumb is: If you can move a bit of information away from a page document to the master or to the stylesheet, do that.

3.10.2 Master document

Example 3.2 shows a master document that compiles most of the data we discussed in 3.9 but adds a few new twists.

Languages. Our example site is bilingual (English and German), so all titles and labels are provided in two languages, and the languages themselves are listed in a languages element. We add an internal DTD subset with mnemonic entity references (2.2.4.3) for German characters.

Environments. For every installation where the site can be built, an environment element with a unique id supplies the following information:

  • src-path is the base directory of the XML source documents tree.
  • Example 3.1 en/team/index.xml: A page document

    <?xml version="1.0" encoding="us-ascii"?>
    <page keywords="team, people, staff, competences, skills">
    
    <title>Our team</title>
    
    <!-- Main content block: -->
    <block type="body">
    <p>With backgrounds in technology and communications, FooBar's
    experienced management team has - you guessed it -
    <em>the right combination of skills for success</em>.</p>
    
    <section image="mike">
    <head>Mike M. Anager</head> 
    <subhead>CEO</subhead>
    <p>CEO and Co-Founder, Mike leads FooBar towards bringing the vision
    of "personal foobar" to reality. He previously served as Chief
    Architect at <ext link="www.barfoo.com">BarFoo Corporation</ext>.</p>
    </section>
    
    <section image="ed">
    <head>Ed N. Gineer</head> 
    <subhead>VP, Engineering</subhead>
    <p>Ed has over 30 years of foobar design experience under his
    belt. He has personally contributed to the most acclaimed of 
    our <int link="solutions">products</int>, including the famous
    <int link="fbplus">Foobar Plus</int>.</p>
    </section>
    
    <section image="jack">
    <head>Jack J. Anitor</head> 
    
    <subhead>Senior Janitor</subhead>
    <p>Jack's expert janitorial skills and experience have been
    critical in the success of FooBar.</p> 
    </section>
    </block>
    
    <!-- Orthogonal content blocks: -->
    <block idref="subscribe"/>
    <block idref="feature"/>
    
    </page>
    
  • out-path is the directory where the output files will be placed (used in batch mode, 5.6). It is also assumed that the images subdirectory (img-path) is under out-path.

  • img-path is where all the images (both static and generated) are stored. This path is relative to out-path.

  • target-path is the common part of all URIs used in the resulting HTML files to refer to images or other pages of the site. Thus, if you transform and view your pages locally at out-path, then target-path may be the same as out-path. If, however, you are going to upload the transformed site to a directory on a web server and access it at, say, http://www.example.org/test/, then target-path may be either /test/ (for absolute pathnames starting with /) or an empty string (for relative pathnames). URIsabsolute URIsrelative

On Windows, all absolute paths must be given in the file:/ URL format.17 This is the only standard and reliable way to represent an absolute pathname that includes a drive letter. In HTML, URLs with file:/ work for both links and image references in all browsers we tested. Other platforms may use absolute pathnames without the file:/.

Menu. The menu lists all the pages of the site. For each page, the src attribute contains the page's pathname (add .xml for source files or .html for output files) relative to the site's root directory.

Each page has an id attribute used to link to it. To make life easier, you can also provide a space-separated list of aliases in the alias attribute. In internal links to this page, you can use either its id or any of the aliases.

Each menu item has a label child storing the item's visible label. In the menu on a web page, each item is supposed to be linked to its first page child, so there's no need to specify a link in an item.

It is assumed that the English and German versions of the source files are named the same but stored in different directory trees under the root directory. The corresponding directories are named after the language designations defined in languages. So, for instance, the complete path to the German fbplus source page in the staging environment would be constructed as follows:

/home/d/web/de/solutions/foobar_plus.xml

Blocks. The blocks element holds a list of orthogonal content blocks with their identifiers (id), source document locations (src), and block selectors (select, 3.9.1.3). Note that the subscribe page is listed only once as an orthogonal source, while the solutions/foobar_plus page is both in the menu and in the blocks list. For this reason, a block must specify a complete location for the orthogonal content source and not just its id, as all other links do, because not all orthogonal documents are registered in the menu and assigned an id.

Misc. Finally, the master document lists the common part to be prepended to page titles (3.2.4) on all pages (html-title), page footer content (page-footer), and two labels for buttons that need to be created by the stylesheet (buttons).

Note that the mailto links used in page-footer represent a special link type (3.5.2, page 109) with an abbreviated address (the corresponding resolved URI will have mailto: prepended to the email address).

Example 3.2 _master.xml: The master document.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE site [
  <!ENTITY auml "ä">
  <!ENTITY ouml "ö">
  <!ENTITY uuml "ü">
]>
<site>

  <!-- Environments: -->
  <environment id="local">
    <os>Windows</os>
    <src-path>file:/C:/Work/Website/XML/</src-path>
    <out-path>file:/C:/Work/Website/Out/</out-path>
    <target-path>file:/C:/Work/Website/Out/</target-path>
    <img-path>Images</img-path>
  </environment>
  <environment id="staging">
    <os>Linux</os>
    <src-path>/home/d/web/</src-path>
    <out-path>/home/d/web/out/</out-path>
    <target-path>/</target-path>
    <img-path>img</img-path>
  </environment>
  <environment id="final">
    <os>BSD</os>
    <src-path>/var/tomcat/webapps/cocoon/foobar/</src-path>
    <out-path>/var/tomcat/webapps/cocoon/foobar/</out-path>
    <target-path>/cocoon/foobar/</target-path>
    <img-path>img</img-path>
  </environment>

  <!-- Languages: -->
  <languages>
    <lang>en</lang>
    <lang>de</lang>
  </languages>

  <!-- Menu: -->
  <menu>
    <item>
      <label>
        <translation lang="en">Home</translation>
        <translation lang="de">Home</translation>
      </label>
      <page id="home" alias="index front fp frontpage" src="index"/> 
    </item> 
    <item>
      <label> 
        <translation lang="en">Solutions</translation>
        <translation lang="de">L&ouml;sungen</translation>
      </label> 
      <page id="solutions" src="solutions/intro_solutions"/>
      <item>
        <label> 
          <translation lang="en">Life</translation>
          <translation lang="de">Das Leben</translation>
        </label> 
        <page id="life" src="solutions/life"/>
        <page id="fbplus" alias="foobar_plus fb+ foobar+" 
              src="solutions/foobar_plus"/>
        <page id="fbminus" src="solutions/foobar_minus"/>
      </item>
      <page id="universe" src="solutions/universe"/>
      <page id="everything" src="solutions/everything"/>
    </item> 
    <item>
      <label>
        <translation lang="en">Our team</translation>
        <translation lang="de">Unser Team</translation>
      </label>
      <page id="team" src="team/index"/>
      <page id="history" src="team/history"/>      
      <page id="hire" src="team/hire"/>      
    </item>   
    <item>
      <label>
        <translation lang="en">Contact</translation>
        <translation lang="de">Kontakt</translation>
      </label>
      <page id="contact" src="contact/contact"/>
      <page id="map" src="contact/map"/>
    </item> 
  </menu>

  <!-- Orthogonal and dynamic blocks: -->
  <blocks>
    <!-- Extract the 'summary' block from the product page: -->
    <block id="feature" src="solutions/foobar_plus" 
           select="summary"/>
    <!-- Extract the 'last' block from the front page: -->
    <block id="news" src="index" select="last"/>
    <!-- Take the entire subscribe.xml: -->
    <block id="subscribe" src="subscribe"/>
    <!-- Run site map generation: -->
    <block-process id="sitemap" process="sitemap" 
                                mode="text" depth="2"/>
    <!-- Run list-titles on all files in news/: -->
    <block-process id="news-list" process="list-titles" 
                                  dir="news/"/>
  </blocks>

  <!-- The common part of the page titles: -->
  <html-title>
    <translation lang="en">Foobar Corporation AG</translation>
    <translation lang="de">Foobar Corporation AG</translation>
  </html-title>

  <!-- Page footer content: -->
  <page-footer>
    <copyright>
      <translation lang="en">&#169; 2003 by Foobar Corporation AG. 
        All rights reserved.</translation>
      <translation lang="de">&#169; 2003 by Foobar Corporation AG. 
        All rights reserved.</translation>
    </copyright>
    <language-switch>
      <translation lang="en">
        <lang link="de">Diese Seite in deutsch</lang>
      </translation>
      <translation lang="de">
        <lang link="en">This page in English</lang>
      </translation>
    </language-switch>
    <contact-webmaster>
      <translation lang="en">
        Problems using this site? Contact the 
        <mailto link="webmaster@foobar.com">Webmaster</mailto>.
      </translation>
      <translation lang="de">
        Probleme mit dieser Web-Site? Kontaktieren Sie bitte unseren 
        <mailto link="webmaster@foobar.com">Webmaster</mailto>.
      </translation>
    </contact-webmaster>
  </page-footer>

  <!-- Sequence navigation buttons: -->
  <buttons>
    <button id="prev">
      <translation lang="en">prev</translation>
      <translation lang="de">zur&uuml;ck</translation>
    </button>
    <button id="next">
      <translation lang="en">next</translation>
      <translation lang="de">vorw&auml;rts</translation>
    </button>
  </buttons>

</site>

3.10.3 Schematron schema

The schema in Example 3.3 is used to validate both the master document and page documents of our Foobar site. This makes sense because these document types have a lot in common. Still, for readability the schema is broken into three patterns: One tests the master document, another tests page documents, and the last one tests constructs that occur in both document types (this includes links, images, and text markup).

Languages. The lang-check abstract rule checks that the element being checked contains exactly as many translation children as there are languages defined in the languages element. This rule can then be reused for any element that provides information in two languages. A separate rule with context="translation" additionally checks that the lang attributes correspond to the defined languages and that each language version is provided only once.

Element presence. In this schema, many element-presence checks are lumped together for simplicity (e.g., all children of an environment are checked in one assert). This does not have to be that way; if you want your schema to be really helpful, you can write a separate check with its own diagnostic message for each element type, explaining its role and the possible consequences of its being missing from the source.

Context-sensitive checks. Note that there are two different page element types: One is used in the master document, and the other is the root element type in a page document. The same applies to blocks. The schema, however, has no problems differentiating between these element types based on the context.

Reporting unknowns. One function of a schema is to check for unknown element type names (most often resulting from typos). In Schematron, this can be implemented by providing a dummy rule with no tests, listing all defined element types as possible contexts. Following that, a rule with context="*" signals error whenever the rule is activated. This technique is possible because each context will only match one rule per pattern; if an element was not matched by the dummy rule, it is caught by the next rule and reported as unrecognized.

It's only a beginning. This example schema demonstrates only the basic, most critical checks. Your own schema may be significantly larger and more detailed than this, although it will likely use mostly the same techniques. Consider this schema a phrasebook with common expressions for typical situations. Several advanced tricks for validating complex constraints are discussed in Chapter 5 (5.1.3).

Example 3.3 schema.sch: A Schematron schema for validating page documents and the master document.

<schema xmlns="http://www.ascc.net/xml/schematron">

<!-- Checks for the master document: -->
<pattern name="master">

<rule context="site">
  <report test="count(//environment) = 1">
    Only one 'environment' found; you will need to create more if you 
    want to build the site in a different environment.
  </report> 
  <report test="count(//environment) = 0">

    No 'environment' elements found; the stylesheet will be unable to  
    figure out pathnames.
  </report>
  <assert test="languages and menu and html-title and page-footer 
                          and blocks">
    One of the required elements not found inside 'site'.
  </assert>
</rule>

<rule context="page-footer">
  <assert test="copyright and language-switch
                          and contact-webmaster">
    One of the required elements not found inside 'page-footer'.
  </assert>
</rule>

<rule context="environment">
  <assert test="src-path and out-path 
                and target-path and img-path and os">
    One of the required elements not found inside 'environment'.
  </assert>
  <assert test="@id">
    An 'environment' must have an 'id' attribute.
  </assert>
  <assert test="count(//environment/@id[. = current()/@id]) = 1"> 
    The 'id' attribute value of an 'environment' must be unique.
  </assert>
</rule>

<rule context="src-path | img-path | out-path | target-path">
  <report test="*">
    The '<name/>' element cannot have children.
  </report>
  <report test="(normalize-space(.) = '') 
                and not(name() = 'target-path')">
    The '<name/>' element cannot be empty.
  </report>
</rule>

<rule context="languages">
  <assert test="count(lang) = count (*)">
    The 'languages' element can only have 'lang' children.
  </assert>
  <assert test="count(lang) > 0">
    The 'languages' element must have at least one 'lang' child.
  </assert>
</rule>

<rule context="languages/lang">
  <assert test="count(//languages/lang[. = current()]) = 1"> 
    Each language must be specified only once.
  </assert>
</rule>

<rule context="menu">
  <assert test="count(item) = count (*)">
    The 'menu' element cannot contain elements other than 'item'.
  </assert>
</rule>

<rule context="item">
  <assert test="label" diagnostics="label-element">
    A 'label' element is missing.
  </assert>
  <report test="count(label) &gt; 1" diagnostics="label-element">
    There is an extra 'label' element.
  </report>
  <assert test="page">
    At least one 'page' element should be specified within an 'item'.
  </assert>
</rule>

<rule context="menu//page">
  <assert test="@src">
    Each 'page' must have an 'src' attribute.
  </assert>
  <assert test="@id">
    Each 'page' must have a unique 'id' attribute.
  </assert>
  <assert test="count(//page/@id[. = current()/@id]) = 1"> 
    The 'id' attribute value of a 'page' must be unique.
  </assert>
</rule>

<!-- Abstract rule to check 'transformation' children: -->
<rule abstract="true" id="lang-check">
  <assert test="count(translation) = count(//languages/lang)">
    The number of 'translation' children in '<name/>' must correspond
    to the number of defined languages. If this element does not 
    exist in one of the languages, use an empty 'translation' element.
  </assert> 
  <assert test="count(translation) = count(*)">
    There must be no child elements here other than 'translation'.
  </assert>
</rule>

<!-- Applying the abstract rule to all bilingual elements: -->
<rule context="label | html-title | copyright 
               | language-switch | contact-webmaster | button">
  <extends rule="lang-check"/>
</rule>

<rule context="translation">
  <assert test="@lang">
    Each 'translation' must have a 'lang' attribute.
  </assert>
  <assert test="@lang = //languages/lang/text()">
    The value of the 'lang' attribute must correspond to one of the 
    defined languages.
  </assert>
  <report test="@lang = preceding-sibling::translation/@lang">
    There is another 'translation' element under this parent with the 
    same value of the 'lang' attribute.
  </report>
</rule>

<rule context="blocks">
  <report test="*[not(self::block or self::block-process)]">
    A 'blocks' element must only contain one or more 'block' or 
    'block-process' elements.
  </report>
</rule>

<rule context="blocks/block">
  <assert test="@id and @src">
    A 'block' defined in the master document must have both 'id' and 
    'src' attributes.
  </assert>
  <assert test="count(//blocks/block/@id[. = current()/@id]) = 1"> 
    The 'id' attribute value of a 'block' must be unique.
  </assert>
</rule>

</pattern>

<!-- Checks for page documents: -->
<pattern name="page">

<rule context="/page">
  <assert test="@keywords">
    Please consider adding a list of keywords to the page. Use a 
    'keywords' attribute for that.
  </assert>
  <assert test="title">
    Each 'page' must have a 'title'.
  </assert>
  <assert test="count(title) &lt; 2">
    A 'page' may have only one 'title'.
  </assert>
  <assert test="block">
    Each 'page' must have at least one 'block'.
  </assert>
</rule>

<rule context="page//block">
  <assert test="@idref or *">
    A block must have either an 'idref' attribute (referring to an 
    orthogonal block) or children.
  </assert>
  <report test="@idref and *">
    A block cannot have both an 'idref' attribute and children.
  </report> 
  <report test="count(p | section) < count(*)">
    A block can only have 'p' or 'section' children.
  </report> 
</rule>

<rule context="section">
  <assert test="head">
    A section must have a 'head'.
  </assert>
  <assert test="p">
    A section must have at least one 'p' (paragraph).
  </assert>
  <assert test="normalize-space(text()) = ''">
    A section cannot contain text. Use a 'p' element to include a 
    paragraph of text.
  </assert>
</rule>

</pattern>

<!-- Rules common for master and page documents: -->
<pattern name="common">

<rule context="int | link[@linktype='internal']">
  <assert test="@link">
    An internal link must use a 'link' attribute to specify the 
    page being linked.
  </assert>
</rule>

<rule context="p">
  <report test="(normalize-space(text()) = '') and not(*)">
    A paragraph cannot be empty. If you want to increase vertical 
    spacing here, modify the stylesheet.
  </report>
</rule>

<!-- Dummy rule listing all defined element types: --> 
<rule context="
    block | block-process | blocks | button | buttons |
    contact-webmaster | copyright | environment | em | ext | head |
    html-title | img-path | int | item | label | lang |
    language-switch | languages | link | mailto | menu | os |
    out-path | p | page | page-footer | site | section | src-path |
    subhead | target-path | title | translation"/>

<!-- Report error if an element was not matched by the above: -->
<rule context="*">
  <report test="true()">
    Unrecognized element: '<name/>'.
  </report>
</rule>

</pattern>

<diagnostics>
  <diagnostic id="label-element">
    Every 'item' element must contain exactly one 'label' element 
    specifying the corresponding top menu label.
  </diagnostic>
</diagnostics>

</schema>
  • + Share This
  • 🔖 Save To Your Account