XML100-1.0.0-introduction-to-xml-xsl-and-xml-schema_sample
XML100-1.0.0-introduction-to-xml-xsl-and-xml-schema_sample
WEBUCATOR
Copyright © 2023 by Webucator. All rights reserved.
No part of this manual may be reproduced or used in any manner without written permission of the
copyright owner.
Version: 1.0.0
The Author
Nat Dunn
Nat Dunn is the founder of Webucator (www.webucator.com), a company that has provided training
for tens of thousands of students from thousands of organizations. Nat started the company in 2003
to combine his passion for technical training with his business expertise, and to help companies benefit
from both. His previous experience was in sales, business and technical training, and management. Nat
has an MBA from Harvard Business School and a BA in International Relations from Pomona College.
Class Files
Errata
Table of Contents | i
LESSON 5. XSLT Basics................................................................................................................73
eXtensible Stylesheet Language.............................................................................................73
The Transformation Process...................................................................................................74
An XSLT Stylesheet..................................................................................................................75
Output Types..........................................................................................................................85
Elements and Attributes.........................................................................................................89
ii | Table of Contents
LESSON 1
XML Basics
EVALUATION COPY: Not to be used in class.
Topics Covered
Benefits of XML.
Introduction
In this lesson, you will learn what XML is, the benefits of XML, the XML syntax rules, and to create
a simple XML file.
XML is used to store data or information; this data might be intended to be read by people or by
machines. It can be highly structured data such as data typically stored in databases or spreadsheets, or
loosely structured data, such as data stored in letters or manuals.
As an example, in HTML, there is a <u> tag used for underlining text. Very often, it is used for emphasis,
but it also might be used to mark a book title. It would be very difficult to write an application that
searched through such a document for book titles.
In XML, the book titles could be placed in <book_title> tags and the emphasized text could be place
in <em> tags. The XML document does not specify how the content of either tag should be displayed.
Rather, the formatting is left up to an external stylesheet. Even though the book titles and emphasized
text might appear the same, it would be relatively straight forward to write an application that finds
all the book titles. It would simply look for text in <book_title> tags. It also becomes much easier
to reformat a document; for example, to change all emphasized text to be italicized rather than
underlined, but leave book titles underlined.
Code Explanation
It is not hard to tell from looking at this that the XML is describing a person named Paul McCartney,
who is a singer and is male.
Do people read XML documents? Programmers do (hey, we’re people too!). And it is easier for us if
the documents we work with are easy to read.
Storing the content in XML makes it much easier to manage content for two reasons.
1. Content changes, additions, and deletions are made in a central location and the changes will
cascade out to all formats of presentation. There is no need to be concerned about keeping
the Word documents in sync with the website, because the content itself is managed in one
place and then transformed for each output medium.
2. Formatting changes are made in a central location. To illustrate, suppose a company had
many marketing web pages, all of which were produced from XML content being transformed
to HTML. The format for all of these pages could be controlled from a single XSLT and a
sitewide formatting change could be made modifying that XSLT.
SOAP - an XML-based protocol used to transfer Web services over the Internet.
WSDL (Web Services Description Language) - an XML-based language for describing a Web
service and how to call it.
Universal Discovery Description and Integration (UDDI) - the yellow pages of Web services.
UDDI directory entries are XML documents that describe the Web services a group offers.
This is how people find available Web services.
An optional prolog.
A document element, usually containing nested elements.
Optional comments or processing instructions.
An XML declaration
Processing instructions
Comments
A Document Type Declaration
This declares that the document is an XML document. The version attribute is required, but the
encoding and standalone attributes are not. If the XML document uses any markup declarations
that set defaults for attributes or declare entities then standalone must be set to no.
Processing Instructions
Processing instructions are used to pass parameters to an application. These parameters tell the
application how to process the XML document. For example, the following processing instruction tells
the application that it should transform the XML document using the XSL stylesheet beatles.xsl.
As shown above, processing instructions begin with and <? end with ?>.
Comments
Comments can appear throughout an XML document. Like in HTML, they begin with <!-- and end
with -->.
<!--This is a comment-->
The Document Type Declaration (or DOCTYPE Declaration) has three roles.
The DOCTYPE Declaration shown below simply states that the document element of the XML document
is beatles.
<!DOCTYPE beatles>
As shown in the second declaration above, public identifiers are divided into three parts:
1.4.2. Elements
Every XML document must have at least one element, called the document element. The document
element usually contains other elements, which contain other elements, and so on. Elements are denoted
with tags. Let’s look again at the Paul.xml.
Code Explanation
The document element is person. It contains three elements: name, job and gender. Further, the
name element contains two elements of its own: firstname and lastname. As you can see, XML
Empty Elements
Not all elements contain other elements or text. For example, in XHTML, there is an img element
that is used to display an image. It does not contain any text or elements within it, so it is called an
empty element. In XML, empty elements must be closed, but they do not require a separate close tag.
Instead, they can be closed with a forward slash at the end of the open tag as shown below.
<img src="images/paul.jpg"/>
<img src="images/paul.jpg"></img>
1.4.3. Attributes
XML elements can be further defined with attributes, which appear inside of the element’s open tag
as shown below.
<name title="Sir">
<firstname>Paul</firstname>
<lastname>McCartney</lastname>
</name>
1.4.4. CDATA
Sometimes it is necessary to include sections in an XML document that should not be parsed by the
XML parser. These sections might contain content that will confuse the XML parser, perhaps because
it contains content that appears to be XML, but is not meant to be interpreted as XML. Such content
must be nested in CDATA sections. The syntax for CDATA sections is shown below.
<![CDATA[
This section will not get parsed
by the XML parser.
]]>
1. Tab
2. Line-feed
3. Carriage-return
4. Single space
There are several important rules to remember with regards to whitespace in XML.
1. Whitespace within the content of an element is significant; that is, the XML processor will
pass these characters to the application or user agent.
2. Whitespace in attributes is normalized; that is, neighboring whitespaces are condensed to a
single space.
3. Whitespace in between elements is ignored.
xml:space Attribute
The xml:space attribute is a special attribute3 in XML. It can only take one of two values: default
and preserve. This attribute instructs the application how to treat whitespace within the content of
the element. Note that the application is not required to respect this instruction.
3. There is only one other special XML attribute: xml:lang, which is used to specify the language used within an element.
Special Characters
Character Entity Reference
< <
> >
& &
" "
' '
1. Open XMLBasics/Exercises/Xml101.xml
2. Add a required prerequisite: “Experience with computers”.
3. Add the following to the topics list:
XML Documents
The Prolog Elements
Attributes
CDATA
XML Syntax Rules
Special Characters
<?xml version="1.0"?>
<album>
<artist>
<firstname>John</firstname>
<lastname>Doe</lastname>
</artist>
<title>Love is All</title>
<year>2023</year>
</album>
Conclusion
In this lesson, you have learned the benefits of XML, how XML is used, and how to write a well-formed
XML document.
Topics Covered
Introduction
In this lesson, you will learn the difference between well-formed and valid XML documents, the purpose
of DTDs, to validate an XML document according to a DTD, and the limitations of DTDs.
LESSON 2: DTDs | 21
EVALUATION COPY: Not to be used in class.
EVALUATION COPY: Not to be used in class.
Imagine, for example, a company that creates technical courseware and sells it to technical training
companies. Those companies may want to display the outlines for that courseware on their websites,
but they do not want to display it in the same way as every other company who buys the courseware.
By providing the course outlines in a predefined XML format, the courseware vendor makes it possible
for the training companies to write programs to read those XML files and transform them into HTML
pages with their own formatting styles (perhaps using XSLT or CSS). If the XML files had no predefined
structure, it would be very difficult to write such programs.
A DTD outlines what elements can be in an XML document and the attributes and subelements that
they can take. Let’s start by taking a look at a complete DTD and then dissecting it.
22 | LESSON 2: DTDs
EVALUATION COPY: Not to be used in class.
Demo 2.1: DTDs/Demos/Beatles.dtd
1. <!ELEMENT beatles (beatle+)>
2. <!ELEMENT beatle (name)>
3. <!ATTLIST beatle
4. link CDATA #IMPLIED
5. real (yes|no) "yes">
6. <!ELEMENT name (firstname, lastname)>
7. <!ELEMENT firstname (#PCDATA)>
8. <!ELEMENT lastname (#PCDATA)>
The element declaration above states that the beatles element must contain one or more beatle
elements.
Child Elements
When defining child elements in DTDs, you can specify how many times those elements can appear
by adding a modifier after the element name. If no modifier is added, the element must appear once
and only once. The other options are shown in the table below.
Modifier Description
? Zero or one times.
+ One or more times.
* Zero or more times.
It is not possible to specify a range of times that an element may appear (e.g, 2-4 appearances).
Each beatle element must contain a child element name, which must appear once and only once.
LESSON 2: DTDs | 23
EVALUATION COPY: Not to be used in class.
<!ELEMENT beatle (name)>
Each name element must contain a firstname and lastname element, which each must appear once
and only once and in that order.
Some elements contain only text. This is declared in a DTD as #PCDATA. PCDATA stands for parsed
character data, meaning that the data will be parsed for XML tags and entities. The firstname and
lastname elements contain only text.
24 | LESSON 2: DTDs
EVALUATION COPY: Not to be used in class.
2.3.6. Location of Modifier
The location of modifiers in a declaration is important. If the modifier is outside of a set of parentheses,
it applies to the group; whereas, if the modifier is immediately next to an element name, it applies only
to that element. The following examples illustrate.
In the example below, the body element can have any number of interspersed child link and img
elements.
In the example below, the body element can have any number of child link elements or any number
of child img elements, but it cannot have both link and img elements.
In the example below, the body element can have any number of child link and img elements, but
they must come in pairs, with the link element preceding the img element.
In the example below, the body element can have any number of child link elements followed by any
number of child img elements.
LESSON 2: DTDs | 25
EVALUATION COPY: Not to be used in class.
2.3.8. Declaring Attributes
Attributes are declared using the <!ATTLIST > declaration. The syntax is shown below.
<!ATTLIST ElementName
AttributeName AttributeType State DefaultValue?
AttributeName AttributeType State DefaultValue?>
The beatle element has two possible attributes: link, which is optional and may contain any valid
XML text, and real, which defaults to yes if it is not included.
<!ATTLIST beatle
link CDATA #IMPLIED
real (yes|no) "yes">
26 | LESSON 2: DTDs
EVALUATION COPY: Not to be used in class.
Demo 2.2: DTDs/Demos/Beatles.xml
1. <?xml version="1.0"?>
2. <!DOCTYPE beatles SYSTEM "Beatles.dtd">
3. <beatles>
4. <beatle link="http://www.paulmccartney.com">
5. <name>
6. <firstname>Paul</firstname>
7. <lastname>McCartney</lastname>
8. </name>
9. </beatle>
10. <beatle link="http://www.johnlennon.com">
11. <name>
12. <firstname>John</firstname>
13. <lastname>Lennon</lastname>
14. </name>
15. </beatle>
16. <beatle link="http://www.georgeharrison.com">
17. <name>
18. <firstname>George</firstname>
19. <lastname>Harrison</lastname>
20. </name>
21. </beatle>
22. <beatle link="http://www.ringostarr.com">
23. <name>
24. <firstname>Ringo</firstname>
25. <lastname>Starr</lastname>
26. </name>
27. </beatle>
28. <beatle link="http://www.webucator.com" real="no">
29. <name>
30. <firstname>Nat</firstname>
31. <lastname>Dunn</lastname>
32. </name>
33. </beatle>
34. </beatles>
LESSON 2: DTDs | 27
EVALUATION COPY: Not to be used in class.
Exercise 3: Writing a DTD
60 to 90 minutes
In this exercise, you will write a DTD for the business letter shown below. You will then give your
DTD to another student, who will mark up the business letter as a valid XML document according to
your DTD. Likewise, you will markup the business letter according to someone else’s DTD. Make
sure that the XML file contains a DOCTYPE declaration.
Both documents should be saved in the DTDs/Exercises folder. To test whether the XML file is valid,
visit https://www.xmlvalidation.com/ and upload or copy and paste your XML document and
DTD (this will be an option after you submit your XML code).
28 | LESSON 2: DTDs
EVALUATION COPY: Not to be used in class.
Exercise Code 3.1: DTDs/Exercises/BusinessLetter.txt
1. November 29, 2023
2.
3. Joshua Lockwood
4. Lockwood & Lockwood
5. 291 Broadway Ave.
6. New York, NY 10007
7. United States
8.
9. Dear Mr. Lockwood:
10.
11. Along with this letter, I have enclosed the following items:
12.
13. - two original, execution copies of the Webucator Master Services Agreement
14. - two original, execution copies of the Webucator Premier Support for
15. Developers Services Description between
16. Lockwood & Lockwood and Webucator, Inc.
17.
18. Please sign and return all four original, execution copies to me at your
19. earliest convenience. Upon receipt of the executed copies, we will
20. immediately return a fully executed, original copy of both agreements to you.
21.
22. Please send all four original, execution copies to my attention as follows:
23.
24. Webucator, Inc.
25. 4933 Jamesville Rd.
26. Jamesville, NY 13078 USA
27. Attn: Bill Smith
28.
29. If you have any questions, feel free to call me at 800-555-1000 x123
30. or e-mail me at [email protected].
31.
32. Best regards,
33.
34. Bill Smith
35. VP, Operations
Conclusion
In this lesson, you learned to created DTDs to validate XML documents.
LESSON 2: DTDs | 29
EVALUATION COPY: Not to be used in class.
30 | LESSON 2: DTDs
EVALUATION COPY: Not to be used in class.
LESSON 3
XML Schema Basics
EVALUATION COPY: Not to be used in class.
Topics Covered
Introduction
In this lesson, you will learn the purpose of XML Schema, the limitations of DTDs, the power of XML
Schema, and to validate an XML Instance with an XML schema.
XML documents that attempt to adhere to an XML schema are said to be instances of that schema. If
they correctly adhere to the schema, then they are valid instances. This is not the same as being well
formed. A well-formed XML document follows all the syntax rules of XML, but it does not necessarily
adhere to any particular schema. So, an XML document can be well formed without being valid, but
it cannot be valid unless it is well formed.
DTDs are similar to XML schemas in that they are used to create classes of XML documents. DTDs
were around long before the advent of XML. They were originally created to define languages based
on SGML, the parent of XML. Although DTDs are still common, XML Schema is a much more
powerful language.
As a means of understanding the power of XML Schema, let’s look at the limitations of DTD.
Code Explanation
As you can see, an XML schema is an XML document and must follow all the syntax rules of any other
XML document; that is, it must be well formed. XML schemas also have to follow the rules defined
in the “Schema of schemas,” which defines, among other things, the structure of and element and
attribute names in an XML schema.
Although it is not required, it is a common practice to use the xs qualifier4 to identify Schema elements
and types.
The document element of XML schemas is xs:schema. It takes the attribute xmlns:xs with the value
of http://www.w3.org/2001/XMLSchema, indicating that the document should follow the rules of
XML Schema. This will be clearer after you learn about namespaces.
In this XML schema, we see a xs:element element within the xs:schema element. xs:element is
used to define an element. In this case it defines the element Author as a complex type element, which
contains a sequence of two elements: FirstName and LastName, both of which are of the simple type,
string.
4. Qualifiers are used to distinguish between elements and attributes from different namespaces or XML classes.
Code Explanation
This is a simple XML document. Its document element is Author, which contains two child elements:
FirstName and LastName, just as the associated XML schema requires.
The xmlns:xsi attribute of the document element indicates that this XML document is an instance
of an XML schema. The document is tied to a specific XML schema with the
xsi:noNamespaceSchemaLocation attribute.
There are many ways to validate the XML instance. If you are using an XML authoring tool, it very
likely is able to perform the validation for you. Alternatively, there is a simple online XML Schema
validator tool listed below.
Conclusion
In this lesson, you have learned to create a very simple XML Schema and to use it to validate an XML
instance document. You are now ready to learn more advanced features of XML Schema.
Topics Covered
List types.
Union types.
Nil values.
Introduction
In this lesson, you will learn to use XML Schema’s built-in simple types, to derive your own types, to
define list types and union types, to declare global simple-type elements, to set default and fixed values,
and to allow for nil values.
4.1. Overview
Simple-type elements have no children or attributes. For example, the Name element below is a
simple-type element; whereas the Person and HomePage elements are not.
Code Explanation
As the diagram below shows, a simple type can either be built-in or user-derived. In this lesson, we will
examine both.
1. normalizedString
2. token
3. language
4. NMTOKEN
5. NMTOKENS
6. Name
7. NCName
Code Explanation
Notice the FirstName and LastName elements in the code sample above. They are not explicitly
defined as simple type elements. Instead, the type is defined with the type attribute. Because the value
(string in both cases) is a simple type, the elements themselves are simple-type elements.
Simple types are derived by restricting built-in simple types or other user-derived simple types. For
example, you might want to create a simple type called password that is an eight-character string. To
do so, you would start with the xs:string type and restrict its length to eight characters. This is done
nesting the <xs:restriction> element inside of the <xs:simpleType> element.
length
minLength
maxLength
pattern
enumeration
whiteSpace
minInclusive
minExclusive
maxInclusive
totalDigits
fractionDigits
The schema below shows how this is done. The two XML instances shown below it are both valid,
because the length of the password is between six and twelve characters.
5. Regular expressions are not covered in this course. For more information on regular expressions in XML Schema, see
https://www.w3.org/TR/xmlschema-2/#dt-regex.
The following example shows how to derive a simple type called Salary, which is a decimal between
10,000 and 90,000.
Number of Digits
Using totalDigits and fractionDigits, we can further specify that the Salary type should consist
of seven digits, two of which come after the decimal point. Both totalDigits and fractionDigits
4.3.5. Enumerations
A derived type can be a list of possible values. For example, the JobTitle element could be a list of
pre-defined job titles.
4.3.6. Whitespace-handling
By default, whitespace in elements of the datatype xs:string is preserved in XML documents; however,
this can be changed for datatypes derived from xs:string. This is done with the xs:whiteSpace
element, the value of which must be one of the following.
([A-Z0-9][A-Za-z0-9\-']* ?)+
It is also possible to define the type of an element locally. The type is then unnamed and applicable
only to that element. The only reason to do this is to clearly show that the type is specific to that element
and not meant for reuse.
4.5.1. Lists
List types are sequences of atomic types separated by whitespace; you can have a list of integers or a list
of dates. Lists should not be confused with enumerations. Enumerations provide optional values for
an element. Lists represent a space delimited list of multiple values within an element’s text node.
4. At the end of the sequence of elements within the Song element, insert an additional element,
Length, which is of the SongTime datatype.
Code Explanation
In this example, the FirstName and LastName elements are both declared globally. The global elements
are then referenced as children of the Author sequence.
The major disadvantage of declaring elements globally is that all global elements must have unique
names.
Code Explanation
Notice that there are two elements named Title, which can appear in different locations in the XML
instance and are of different types. When the Title element appears at the root of the XML instance,
its value can be any string; whereas, when it appears as a child of Author, its value is limited to “Mr.”,
“Ms.”, or “Dr.”
The example below defines a similar content model; however, because the elements are declared globally,
the name Title cannot be used twice.
Code Explanation
When defaults are set in the XML schema, the following rules apply for the instance document.
1. If the element appears in the document with content, the default value is ignored.
2. If the element appears without content, the default value is applied.
3. If the element does not appear, the element is left out. In other words, providing a default
value does not imply that the element should be inserted if the XML instance author leaves
it out.
Examine the following XML instance. The Title element cannot be empty; it requires one of the
values from the enumeration defined in the JobTitle simple type. However, in accordance with
number 2 above, the schema processor applies the default value of Salesperson to the Title element,
so the instance validates successfully.
Code Explanation
The MinOccurs attribute is used to make the Status element optional. However, if it is used, it must
contain the value current or be left empty, in which case, the value current is implied. The file Simple
Types/Demos/LauraSmith.xml in the Demos folder validates against this schema.
Setting the nillable attribute of xs:element to true indicates that such elements can be set to nil
by setting the xsi:nil attribute to true.
Code Explanation
By including the MiddleName element and setting xsi:nil to true, we are explicitly stating that we
do not know anything about Mark Twain’s middle name. He might have had one, but we don’t know
what it is, and if we do, we’re not saying.
Conclusion
In this lesson, you have learned to work with XML Schema’s built-in simple types, to derive your own
simple types, and to declare simple-type elements. We will now take a look at complex-type elements.
Topics Covered
Whitespace.
Output modes.
Introduction
In this lesson, you will learn the purpose of XSLT, to do a simple XSLT transform, to work with
whitespace, to choose the output mode, and to output elements and attributes.
XSLT documents are well-formed XML documents that describe how another XML document should
be transformed. For XSLT to work, it needs an XML document to transform and an engine to make
the transformation take place. In addition, parameters can be passed in to XSLTs providing further
instructions on how to do the transformation.
Root node
Element nodes
Attribute nodes
Text nodes
Processing instruction nodes
Comment nodes
An XSLT document contains one or more templates, which are created with the <xsl:template />
tag. When an XML document is transformed with an XSLT, the XSLT processor reads through the
XML document starting at the root, which is one level above the document element, and progressing
from top to bottom, just as a person would read the document. Each time the processor encounters a
node, it looks for a matching template in the XSLT.
Default Templates
Node Type Default Template
Root Apply templates for child nodes.
Element Apply templates for child nodes.
Attribute Output attribute value.
Text Output text value.
Processing Instruction Do nothing.
Comment Do nothing.
In this context, attributes are not considered children of the elements that contain them, so attributes
get ignored by the XSLT processor unless they are explicitly referenced by the XSLT document.
This is a straightforward XML document. The processing instruction at the top indicates that the XML
document should be transformed using Beatle.xsl (shown below).
Code Explanation
Note that the document begins with an XML declaration. This is because XSLTs are XML documents
themselves. As with all XML documents, the XML declaration is optional.
The second line (shown below) is the document element of the XSLT. It states that this document is
a version 1.0 XSLT document.
The third line (shown below) indicates that the resulting output will be HTML.
<xsl:output method="html"/>
There are then a few lines of HTML tags followed by two <xsl:value-of /> elements separated by
one <xsl:text> element. The <xsl:value-of /> tag has a select attribute, which takes an XPath
pointing to a specific element or group of elements within the XML document. In this case, the two
<xsl:value-of /> tags point to firstname and lastname elements, indicating that they should
be output in the title of the HTML page. The <xsl:text> element is used to create a space between
the first name and the last name elements.
There are then some more HTML tags followed by the same XSLT tags, re-outputting the first and
last name of the Beatle in the body of the HTML page.
5.3.1. xsl:template
The xsl:template tag is used to tell the XSLT processor what to do when it comes across a matching
node. Matches are determined by the XPath expression in the match attribute of the xsl:template
tag.
Code Explanation
If we use this XSLT to transform XsltBasics/Demos/FirstName.xml, which has the same XML as
XsltBasics/Demos/Paul.xml, the result will be:
The text “We found a first name!” shows up only once, because only one match is found. The text
“McCartneySingerMale” shows up because the XSLT engine will continue looking through an XML
document until it is explicitly told to stop. When it reaches a text node with no matching template, it
uses the default template, which simply outputs the text. “McCartney”, “Singer” and “Male” are the
text node values inside the elements that follow the FirstName element.
Let’s see what happens if we transform an XML document with multiple FirstName elements against
this same XSLT. Take, for example, the XML document below.
Code Explanation
Each time a firstname element is found in the source XML document, the text “We found a first
name!” is output. For the other elements with text (in this case, only lastname elements), the actual
text is output.
5.3.2. xsl:value-of
The xsl:value-of element is used to output the text value of a node. To illustrate, let’s look at the
following example.
Code Explanation
If we use this XSLT to transform XsltBasics/Demos/Beatles-VO1.xml, which has the same XML
as XsltBasics/Demos/Beatles.xml, the result will be:
The select attribute takes an XPath, which is used to indicate which node’s value to output. A single
dot (.) refers to the current node (i.e, the node that was matched by the template).
Don’t let the last names at the end of each line confuse you. These are not part of the output of the
xsl:value-of element. They are there as a result of the default template, which outputs the text value
of the element found.
Code Explanation
If we use this XSLT to transform XsltBasics/Demos/Beatles-VO2.xml, which has the same XML
as XsltBasics/Demos/Beatles.xml, the result will be:
This XSLT has a template for both firstname and lastname and so it never uses the default template.
If you did not want that extra whitespace to show up in the output, you could use the following XSLT
instead.
Code Explanation
Because whitespace between open tags (e.g, between <xsl:template match="blurb"> and
<xsl:text>) and whitespace between close tags (e.g, </xsl:text> and </xsl:template>) is ignored,
the only content that is output is the text between the open and close xsl:text tags.
The examples above illustrate how xsl:text can be used to remove whitespace. It can also be used to
add whitespace where there otherwise wouldn’t be any. Let’s take a look at another example.
Code Explanation
Clearly, this isn’t the desired output. We’d like to have each Beatle on a separate line and spaces between
their first and last names like this:
However, because whitespace between two open tags and between two close tags is ignored, we don’t
get any whitespace in the output. We can use xsl:text to fix this by explicitly indicating how much
whitespace we want in each location.
Code Explanation
Notice that the second close xsl:text hugs the left border. If we were to put any space before it on
that line, that space would show up in the output. To see the desired result, transform XsltBasics/De
mos/Beatles-WS4.xml against XsltBasics/Demos/WhiteSpace4.xsl.
5.4.1. Text
<xsl:output method="text"/>
As we have seen in the examples thus far, XSLT can be used to output plain text. However, XSLT is
much more commonly used to transform one XML structure into another XML structure or into
HTML.
<xsl:output method="xml"/>
The default output method (in most cases) is XML. That is, an XSLT with no xsl:output element
will output XML. In this case, XSLT tags are intermingled with the output XML tags. Generally, the
XSLT tags are prefixed with xsl:, so it is easy to tell them apart. The output XML tags may also be
prefixed, but not with the same prefix as the XSLT tags.
The documents below show how XSLT can be used to output XML.
5.4.3. HTML
<xsl:output method="html"/>
It is very common to use XSLT to output HTML. In fact, XSLT even has a special provision for
HTML: if the document element of the resulting document is html (not case sensitive) then the default
method is changed to HTML. However, for the sake of clarity, it is better code to specify the output
method with the xsl:output tag.
The documents below show how XSLT can be used to output HTML.
Code Explanation
5.5.1. xsl:element
The xsl:element tag can be used to explicitly specify that an element is for output. You will notice
that the xsl:element tag takes the name attribute, which is used to specify the name of the element
being output.
The xsl:element tag is most useful when the tag name itself is generated. If the tag name is not
generated, it is generally easier to use literal result elements.
5.5.2. xsl:attribute
The xsl:attribute element is similar to the xsl:element element. It is used to explicitly output
an attribute. However, it is more commonly used than xsl:element. To see why, let’s first look at
an example in which an attribute is output literally.
Notice that the value of the Name attribute is just “Some Name”. We would like to generate this value
from the source XML document by doing something like this:
Code Explanation
The xsl:attribute tag applies to the element that it is nested within; in this case, the Match element.
When XsltBasics/Demos/NameWithAtt.xml, which has the same XML as XSLTBasics/De
mos/Name.xml, is transformed against XsltBasics/Demos/NameWithAtt.xsl, the output looks like
this:
Code Explanation
This will have the exact same result as XsltBasics/Demos/NameWithAtt.xsl, but it is much quicker
and easier to write. To see the result, transform XsltBasics/Demos/NameWithAttAbbr.xml against
this XSLT.
Conclusion
In this lesson, you have learned the basics of creating XSLTs to transform XML instances into text,
HTML and other XML structures. You are now ready to learn more advanced features of XSLT.