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

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

This type therefore allows values such as
red green blue
, or
violet yellow
or even
red red red
. The values are written in textual XML as a list of color names separated by spaces, but once the document has been through schema validation, the typed value of an element with this type will be a sequence of
xs:NCName
values.

The term
simple type
in XML Schema rules out types involving multiple attribute or element nodes, but it does allow composite values consisting of a sequence of atomic values.

Elements with Attributes and Simple Content

One thing that might occur quite frequently in an invoice or purchase order is an amount in money: there might be elements such as:

  • 50.00
  • 1890.00

What these two elements have in common is that they have a
currency
attribute (with a particular range of allowed values) and content that is a decimal number. This is an example of a complex type. I defined
part-number
as a simple type because it didn't involve any nodes. The
money-amount
type is a complex type, because it involves a decimal number and an attribute value. You can define this type as follows:


  

    

    

    

    

  



  

    

      

    

  


Here I have defined two new types in the schema, both of which are named. The first defines the type of the
currency
attribute. I could have used the same name for the attribute and its type, but many people prefer to keep the names of types distinct from those of elements and attributes, to avoid confusing the two. In this case I've chosen to define it (again) as a subtype of
xs:token
, but this time restricting the value to be one of four particular world currencies. In practice, of course, the list might be much longer. The
currency-type
is again a simple type, because it's just a value; it doesn't define any nodes.

The second definition is a complex type, because it defines two things. It's the type of an element that has a
currency
attribute conforming to the definition of
currency-type
, and has content (the text between the element start and end tags) that is a decimal number, indicated by the reference to the built-in type
xs:decimal
. This particular kind of complex type is called a
complex type with simple content
, which means that elements of this type can have attributes, but they cannot have child elements.

Again, the name of the type is quite distinct from the names of the elements that conform to this type. You can declare the two example elements above in the schema as follows:



But although the type definition doesn't constrain the element name, it does constrain the name of the attribute, which must be
currency
. If the type definition defined child elements, it would also constrain these child elements to have particular names.

In an XSLT 2.0 stylesheet, you can write a template rule for processing elements of this type, which means that all the logic for formatting money amounts can go in one place. For example, you could write:


  


This would output the example

element as
EUR 1,890.00
. (The
format-number()
function is described in Chapter 13, on page 788). The beauty of such a template rule is that it is highly reusable; however much the schema is extended to include new elements that hold amounts of money, this rule can be used to display them.

Other books

Mayan Afterglow by A. S. Fenichel
Lady by Viola Grace
Obsession by Maya Moss
Beyond Eighteen by Gretchen de la O
I Think I Love You by Bond, Stephanie
Bloomsbury's Outsider by Sarah Knights
Betrayed by Trust by Hogan, Hailey
Dead Guy's Stuff by Sharon Fiffer
The Last Ride of German Freddie by Walter Jon Williams