XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (75 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
6.91Mb size Format: txt, pdf, ePub

However, I wouldn't normally recommend this approach. Creating a copy of the input document is likely to be expensive. It's better to do the validation on the fly while the input document is being parsed in the first place.

The value of the
type
attribute in this example, like the type named in the
instance of
expression in the previous example, is a type that's defined in a schema. Like any other type that's referred to by name in a stylesheet, it must be defined in a schema that has been imported using the

declaration.

Here's an example of a complete stylesheet written to process a validated source document.

Example: Validating the Source Document

This example shows how a stylesheet can make use of the schema for the source document.

Source

The source document is a poem such as
theHill.xml
, which starts:

      xsi:schemaLocation=“http://poetry.org/ns poem.xsd”

      xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”>

   

     Rupert Brooke

     1887-08-03

     1915-04-23

   

   1912

   The Hill

   

      Breathless, we flung us on the windy hill,

      Laughed in the sun, and kissed the lovely grass.

      You said “Through glory and ecstasy we pass;

      Wind, sun, and earth remain, and birds sing still,

      When we are old, are old….“ “And when we die

      All's over that is ours; and life burns on

      Through other lovers, other lips” said I,

      “Heart of my heart, our heaven is now, is won!”

   

The
xsi:schemaLocation
attribute tells the processor where to find a schema for this document. This isn't needed with Saxon, which will validate the document against the schema imported into the stylesheet, provided that validation is requested on the command line. With AltovaXML, the
xsi:schemaLocation
attribute acts as an implicit request for document validation, and is therefore necessary for this example to work.

The
xsi namespace
is defined in the XML Schema specification and contains four attributes:
xsi:schemaLocation, xsi:noNamespaceSchemaLocation, xsi:type, and xsi:nil
. The prefix
xsi
is conventional, but the namespace URI must be
http://www.w3.org/2001/XMLSchema-instance
.

Schema

The schema for poems is in
poem.xsd
:


elementFormDefault=“qualified” targetNamespace=“http://poetry.org/ns”

xmlns:p=“http://poetry.org/ns”>

  

    

      

        

        

        

        

      

    

  

  

    

      

        

        

        

      

    

  

  

  

  

  

  

  

    

      

        

      

    

  

  

  

    

  


Stylesheet

The following stylesheet is in
poem-to-html.xsl
. It relies on the source document being validated.


      xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”

      xmlns:xs=“http://www.w3.org/2001/XMLSchema”

      xpath-default-namespace=“http://poetry.org/ns”

      exclude-result-prefixes=“xs”>

                   schema-location=“poem.xsd”/>



  Input must be a validated poem



    

      

        <xsl:value-of select=“poem/title”/>

      

      

        

        

by 

          (

          - )


        


        

          


            

              

              

            

          


        

      

    



  

  

    

      

    

    

      

    

  



Notice how the template rules in this example are matching nodes by their type. The first rule catches unvalidated documents to report an error. The second rule matches the document node of a poem, while the third matches any element that is typed as a
gendate
—a user-defined type that can be an
xs:date
, an
xs:gYearMonth
, or an
xs:gYear
. There are three elements in the schema that use this type, but we don't need to name them in the stylesheet; the rule can be used to format any element of this type, regardless of its name.

Output

This example can be run using Saxon-SA, by using the command (on one line):

java net.sf.saxon.Transform -val:strict -s:theHill.xml 

-xsl:poem-to-html.xsl -o:theHill.html

It can also be run using the AltovaXML 2008 processor, with the command:

AltovaXML -in theHill.xml -xslt2 poem-to-html.xsl

Figure 4-2
shows what the output looks like in the browser.

Try some experiments with this, so that you can see what effect schema-aware processing has when you make errors. For example, change the path expression on line 23 from
poem/author/name
to
poem/poet/name
. Saxon-SA gives you the warning:

Other books

Something Missing by Matthew Dicks
The Rose Legacy by Kristen Heitzmann
Worry Warts by Morris Gleitzman
0062104292 (8UP) by Anne Nesbet