0% found this document useful (0 votes)
16 views

Extensible Markup Language Store and Transport Data

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Extensible Markup Language Store and Transport Data

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

XML

● XML stands for eXtensible Markup Language.

● XML was designed to store and transport data.

● XML was designed to be both human- and machine-readable.

● XML is a markup language much like HTML.


● XML was designed to be self-descriptive.

XML does not do anything. It is just information wrapped in tags.

Ex:

<message>

<to>Meena</to>

<from>Deepa</from>

<heading>Invitation Reminder</heading>

<body>Dont forget to come to my place in Saturday evening</body>

</message>

In above example,

<to> is a receiver information

<from> is a sender information

<heading> title for message

<body> message for the person.

So XML is a self-desccriptive.It is just a information wrapped in the tags.

● The XML language has no predefined tags.


● The tags in the example above (like <to> and <from>) are not defined in any XML
standard. These tags are "invented" by the author of the XML document.

● HTML works with predefined tags like <p>, <h1>, <table>, etc.

● With XML, the author must define both the tags and the document structure.

● XML does not carry any information about how to be displayed.


● The same XML data can be used in many different presentation scenarios.

● Because of this, with XML, there is a full separation between data and
presentation.

In many HTML applications, XML is used to store or transport data, while


HTML is used to format and display the same data.

XML documents form a tree structure that starts at "the root" and branches to
"the leaves".

The XML Tree Structure

XML documents are formed as element trees.

An XML tree starts at a root element(parent) and branches from the root to child
elements.

All elements can have sub elements (child elements).

The terms parent, child, and sibling are used to describe the relationships between
elements.

Self-Describing Syntax:
XML uses a much self-describing syntax.
prolog defines the XML version and the character encoding:

<?xml version="1.0" encoding="UTF-8"?>

The next line is the root element of the document:

<bookstore>

The next line starts a <book> element:

<book category="cooking">

The <book> elements have 4 child elements: <title>, <author>, <year>, <price>.

<title lang="en">Everyday Italian</title>


<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>

The next line ends the book element:

</book>

XML Syntax Rules


The syntax rules of XML are very simple and logical.

XML Documents Must Have a Root Element.

XML documents must contain one root element that is the parent of all other
elements:

<root>
<child>
<subchild>.....</subchild>
</child>
</root>

The XML Prolog


This line is called the XML prolog:

<?xml version="1.0" encoding="UTF-8"?>

The XML prolog is optional. If it exists, it must come first in the document.
XML documents can contain international characters, like Norwegian øæå or French
êèé.

To avoid errors, you should specify the encoding used, or save your XML files as UTF-8.

UTF-8 is the default character encoding for XML documents.

Note: The XML prolog does not have a closing tag! This is not an error. The prolog is not
a part of the XML document.

All XML Elements Must Have a Closing Tag


In XML, it is illegal to omit the closing tag. All elements must have a closing tag.

XML Tags are Case Sensitive


XML tags are case sensitive. The tag <Letter> is different from the tag <letter>.

Opening and closing tags must be written with the same case:

<message>This is correct</message>

XML Elements Must be Properly Nested


In XML, all elements must be properly nested within each other.

XML Attribute Values Must Always be Quoted


XML elements can have attributes in name/value pairs just like in HTML.

In XML, the attribute values must always be quoted:

<note date="12/11/2007">
<to>Tove</to>
<from>Jani</from>
</note>

Entity References
Some characters have a special meaning in XML.

If you place a character like "<" inside an XML element, it will generate an error because
the parser interprets it as the start of a new element.

This will generate an XML error:

<message>salary < 1000</message>


To avoid this error, replace the "<" character with an entity reference:

<message>salary &lt; 1000</message>

There are 5 pre-defined entity references in XML:

&lt; < less than

&gt; > greater than

&amp; & ampersand

&apos; ' apostrophe

&quot; " quotation mark

White-space is Preserved in XML


XML does not truncate multiple white-spaces.

XML Elements
An XML element is everything from (including) the element's start tag to (including) the
element's end tag.

An element can contain:

● text
● attributes
● other elements
● or a mix of the above
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

<title>, <author>, <year>, and <price> have text content because they contain text (like
29.99).

<bookstore> and <book> have element contents, because they contain elements.

<book> has an attribute (category="children").

Empty XML Elements:


An element with no content is said to be empty.

In XML, you can indicate an empty element like this:

<element></element>

XML Naming Rules


XML elements must follow these naming rules:

● Element names are case-sensitive


● Element names must start with a letter or underscore
● Element names cannot start with the letters xml (or XML, or Xml, etc)
● Element names can contain letters, digits, hyphens, underscores, and periods
● Element names cannot contain spaces.

Best Naming Practices


● Create descriptive names, like this: <person>, <firstname>, <lastname>.

● Create short and simple names, like this: <book_title> not like this:
<the_title_of_the_book>.
● Avoid "-". If you name something "first-name", some software may think you
want to subtract "name" from "first".

● Avoid ".". If you name something "first.name", some software may think that
"name" is a property of the object "first".

● Avoid ":". Colons are reserved for namespaces.

Naming Conventions

Style Example Description

Lower <firstname> All letters lower case


case

Upper <FIRSTNAME> All letters upper case


case

Snake <first_name> Underscore separates words (commonly used in SQL databases)


case

Pascal <FirstName> Uppercase first letter in each word (commonly used by C


case programmers)

Camel <firstName> Uppercase first letter in each word except the first (commonly
case used in JavaScript)

XML Attributes
XML elements can have attributes, just like HTML.

Attributes are designed to contain data related to a specific element.


XML Attributes Must be Quoted
Attribute values must always be quoted. Either single or double quotes can be used.

For a person's gender, the <person> element can be written like this:

<person gender="female">

or like this:

<person gender='female'>

If the attribute value itself contains double quotes you can use single quotes, like in this
example:

<person name='George "Geo" Ziegler'>

or you can use character entities:

<person name="George &quot;Geo&quot; Ziegler">

XML Elements vs. Attributes


Take a look at these two examples:

<person gender="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

<person>
<gender>female</gender>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

In the first example, gender is an attribute. In the last example, gender is an element.
Both examples provide the same information.

There are no rules about when to use attributes or when to use elements in XML
Features and Advantages of XML

1) XML separates data from HTML

2) XML simplifies data sharing

XML data is stored in plain text format. This provides a software- and hardware-
independent way of storing data.

This makes it much easier to create data that can be shared by different applications.

3) XML simplifies data transport

4) XML simplifies Platform change


Upgrading to new systems (hardware or software platforms), is always time consuming.
Large amounts of data must be converted and incompatible data is often lost.

XML data is stored in text format. This makes it easier to expand or upgrade to new
operating systems, new applications, or new browsers, without losing data.

5) XML increases data availability

Different applications can access your data, not only in HTML pages, but also from XML
data sources.

With XML, your data can be available to all kinds of "reading machines" (Handheld
computers, voice machines, news feeds, etc), and make it more available for blind people,
or people with other disabilities.

XML Namespaces:
XML Namespaces provide a method to avoid element name conflicts.
Declaration:
An XML namespace is declared using the reserved XML attribute. This
attribute name must be started with "xmlns".

<element xmlns:name = "URL">


Here, namespace starts with keyword "xmlns". The word name is a
namespace prefix. The URL is a namespace identifier
<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table xmlns:f="https://www.abc.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>

The namespace URI is not used by the parser to look up information.


The purpose of using an URI is to give the namespace a unique name.

Default Namespaces
Defining a default namespace for an element saves us from using prefixes in all the child
elements. It has the following syntax:

xmlns="namespaceURI"

This XML carries HTML table information:

<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>

This XML carries information about a piece of furniture:

<table xmlns="https://www.w3schools.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>

XML DTD
DTD stands for Document Type Definition.

A DTD defines the structure and the legal elements and attributes of an XML document.
1.A Document Type Declaration is a statement embedded in an XML document whose
purpose is to acknowledge the existence and location of Document Type
Definition(DTD).

2.Document Type Definition is a set of rules that defines the structure of an XML
document where as a DTD is a statement that tells the parser which DTD to use for
checking and validation.

4.All Document Type Declaration starts with a string “<!DOCTYPE ”

5.The Document Type Declaration can be external or internal.

Internal DTD declaration


<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>

● !DOCTYPE note defines that the root element of this document is note
● !ELEMENT note defines that the note element must contain four elements:
"to,from,heading,body"
● !ELEMENT to defines the to element to be of type "#PCDATA"
● !ELEMENT from defines the from element to be of type "#PCDATA"
● !ELEMENT heading defines the heading element to be of type "#PCDATA"
● !ELEMENT body defines the body element to be of type "#PCDATA"

An External DTD Declaration


If the DTD is declared in an external file, the <!DOCTYPE> definition must contain a
reference to the DTD file:

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Note.dtd

<!ELEMENT note (to,from,heading,body)>


<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

The Building Blocks of XML Documents


Seen from a DTD point of view, all XML documents are made up by the following
building blocks:

● Elements
● Attributes
● Entities
● PCDATA
● CDATA

Elements
● Elements are the main building blocks of both XML and HTML documents.
● Examples of HTML elements are "body" and "table". Examples of XML elements
could be "note" and "message". Elements can contain text, other elements, or be
empty. Examples of empty HTML elements are "hr", "br" and "img".

Examples:

<body>some text</body>

<message>some text</message>

Attributes
Attributes provide extra information about elements.

Attributes are always placed inside the opening tag of an element. Attributes always
come in name/value pairs. The following "img" element has additional information
about a source file:
<img src="computer.gif" />

PCDATA
PCDATA means parsed character data.

Think of character data as the text found between the start tag and the end tag of an
XML element.

PCDATA is text that WILL be parsed by a parser. The text will be examined by the
parser for entities and markup.

Tags inside the text will be treated as markup and entities will be expanded.

However, parsed character data should not contain any &, <, or > characters; these need
to be represented by the &amp; &lt; and &gt; entities, respectively.

CDATA
CDATA means character data.

CDATA is text that will NOT be parsed by a parser. Tags inside the text will NOT be
treated as markup and entities will not be expanded.

Declaring Attributes
An attribute declaration has the following syntax:

<!ATTLIST element-name attribute-name attribute-type attribute-value>

DTD example:

<!ATTLIST payment type CDATA "check">

XML example:

<payment type="check" />


The attribute-value can be one of the following:

Value Explanation

value The default value of the attribute

#REQUIRED The attribute is required

#IMPLIED The attribute is optional

#FIXED value The attribute value is fixed

#REQUIRED
Syntax
<!ATTLIST element-name attribute-name attribute-type #REQUIRED>

#IMPLIED
Syntax
<!ATTLIST element-name attribute-name attribute-type #IMPLIED>

#FIXED
Syntax
<!ATTLIST element-name attribute-name attribute-type #FIXED "value">

Enumerated Attribute Values


Syntax
<!ATTLIST element-name attribute-name (en1|en2|..) default-value>

Example
DTD:
<!ATTLIST payment type (check|cash) "cash">
XML example:
<payment type="check" />
or
<payment type="cash" />

Use enumerated attribute values when you want the attribute value to be one of a fixed
set of legal values.

XML Schema

What is XML schema


XML schema is a language which is used for expressing constraint about XML
documents. There are so many schema languages which are used now a days for
example Relax- NG and XSD (XML schema definition).

An XML schema is used to define the structure of an XML document. It is like DTD
but provides more control on XML structure.

XML Schemas Support Data Types


One of the greatest strength of XML Schemas is the support for data types.

● It is easier to describe allowable document content


● It is easier to validate the correctness of data
● It is easier to define data facets (restrictions on data)
● It is easier to define data patterns (data formats)
● It is easier to convert data between different data types

XML Schema Data types


There are two types of data types in XML schema.

1. simpleType
2. complexType
simpleType

The simpleType allows you to have text-based elements. It contains less attributes,
child elements, and cannot be left empty.

complexType

The complexType allows you to hold multiple attributes and elements. It can
contain additional sub elements and can be left empty.

ex:

<xs:element name="note">

<xs:complexType>

<xs:sequence>

<xs:element name="to" type="xs:string"/>

<xs:element name="from" type="xs:string"/>

<xs:element name="heading" type="xs:string"/>

<xs:element name="body" type="xs:string"/>

</xs:sequence>

</xs:complexType>

</xs:element>

● <xs:element name="note"> defines the element called "note"


● <xs:complexType> the "note" element is a complex type
● <xs:sequence> the complex type is a sequence of elements
● <xs:element name="to" type="xs:string"> the element "to" is of type string
(text)
● <xs:element name="from" type="xs:string"> the element "from" is of type
string
● <xs:element name="heading" type="xs:string"> the element "heading" is of
type string
● <xs:element name="body" type="xs:string"> the element "body" is of type
string
XML Schemas are More Powerful than DTD
● XML Schemas are written in XML
● XML Schemas are extensible to additions
● XML Schemas support data types
● XML Schemas support namespaces

The <schema> Element


The <schema> element is the root element of every XML Schema:

<?xml version="1.0"?>

<xs:schema>

...

...

</xs:schema>

ex:

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

targetNamespace="https://www.library.com"

xmlns="https://www.library.com"

elementFormDefault="qualified">

...

...

</xs:schema>
XML Schema creates the new elements and attributes and puts it in a namespace
called {target namespace}. But what if we don't specify a {target namespace} in
the schema? When we don't specify the attribute targetNamespace at all, no
{target namespace} exists which is legal but specifying an empty URI in the
targetNamespace attribute is "illegal."

1.xmlns:xs="http://www.w3.org/2001/XMLSchema"

indicates that the elements and data types used in the schema come from the
"http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the
elements and data types that come from the
"http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs:

2.targetNamespace="https://www.library.com"

indicates that the elements defined by this schema (note, to, from, heading,
body.) come from the "https://www.library.com" namespace.

3.xmlns="https://www.library.com"

indicates that the default namespace is "https://www.library.com".

4.elementFormDefault="qualified"

indicates that any elements used by the XML instance document which were
declared in this schema must be namespace qualified.

Defining a schema:
Schemas are written from a namespace(schema of schemas): The name of this
namespace is

http://www.w3.org/2001/XMLSchema

element, schema, sequence and string are some names from this namespace
Every XML

schema has a single root, schema.

The schema element must specify the namespace for the schema of schemas
from which the schema‘s
elements and its attributes will be drawn. It often specifies a prefix that will be
used for the names in the

schema. This name space specification appears as

xmlns:xsd = http://www.w3.org/2001/XMLSchema

Every XML schema itself defines a tag set like DTD, which must be named with
the targetNamespace

attribute of schema element. The target namespace is specified by assigining a


name space to the target

namespace attribute as the following:

targetNamespace = http://cs.uccs.edu/planeSchema

Every top-level element places its name in the target namespace If we want to
include nested elements(ie if

the names of the elements and attributes that are not defined directly in the
schema element), we must set the

elementFormDefault attribute to qualified.

elementFormDefault = “qualified”

The default namespace which is source of the unprefixed names in the schema is
given

with another xmlns specification without the prefix.

xmlns = "http://cs.uccs.edu/planeSchema “

A complete example of opening tag for a schema element:

<xsd:schema

<!-- Namespace for the schema itself -->

xmlns:xsd = http://www.w3.org/2001/XMLSchema

<!-- Namespace where

elements defined here will be placed -->


targetNamespace = http://cs.uccs.edu/planeSchema

<!-- Default namespace for this document -->

xmlns = http://cs.uccs.edu/planeSchema.

<!-- Specify non-top-level elements to be in the target namespace-->

elementFormDefault = "qualified” >

One alternative to the preceding opening tag would be to make the XMLSchema
names the default so that

they do not need to be prefixed in the schema. Then the names in the target
namespace would need to be

prefixed. The following schema tag illustrates this approach:

<schema xmlns =” http://www.w3.org/2001/XMLSchema”

targetNamespace = “http://cs.uccs.edu/planeSchema”

xmlns:plane =“http://cs.uccs.edu/planeSchema”

elementformdefault= “qualified”>

XSD Simple Elements


A simple element is an XML element that can contain only text. It cannot contain
any other elements or attributes.

The syntax for defining a simple element is:

<xs:element name="xxx" type="yyy"/>

where xxx is the name of the element and yyy is the data type of the element.

XML Schema has a lot of built-in data types. The most common types are:

● xs:string
● xs:decimal
● xs:integer
● xs:boolean
● xs:date
● xs:time

XML Schema defines 44 data types .19 of which are primitive and 25 of which
are derived.

• Primitive datatype: string, Boolean, float, time ...

• Predefined Derived datatype: Examples byte, decimal, positiveInteger,.

• User-defined derived data types – These datatypes are defined by specifying


restrictions on an existing type,which is then called a basetype. Such user defined
types are derived types. Constraints on derived types are given in terms of facets
of the base type .

Ex: interget data type has 8 possible facets : totalDigits,

maxInclusive,,maxExclusive etc.

- Both simple and complex types can be either named or anonymous .

DTDs define global elements (context of reference is irrelevant). But context of


reference is essential in

XML schema

Data declarations in an XML schema can be

1. Local ,which appears inside an element that is a child of schema

2. Global, which appears as a child of schema

ex: <lastname>Refsnes</lastname>

<age>36</age>

<dateborn>1970-03-27</dateborn>

corresponding Simple elements:

<xs:element name="lastname" type="xs:string"/>


<xs:element name="age" type="xs:integer"/>

<xs:element name="dateborn" type="xs:date"/>

• Elements are defined in an XML schema with the element tag, which is from the
XMLSchema namespace. Recall that the prefix xsd is normally used for names
from this namespace. Use the element tag and set the name and type attributes

<xsd:element name = "bird” type = "xsd:string” />

The instance could be :

<bird> Yellow-bellied sap sucker </bird>

• An element can be given default value using default attribute .Default value is
automatically assigned to the element when no other value is specified.

<xsd:element name = "bird” type = "xsd:string” default=”Eagle” />

• An element can have constant value, using fixed attribute .A fixed value is
automatically assigned to the element , and we cannot specify another value.

<xsd:element name = "bird” type = "xsd:string” fixed=”Eagle” />

Declaring User-Defined Types:

• User-Define type is described in a simpleType element, using facets . facets


must be specified in the content of restriction element. Restrictons on XML
elements are called facets.

Restrictions are used to define acceptable values for XML elements or attributes.
Facets values are specified with the value attribute.

For example, the following declares a user-defined type , firstName

<xsd:simpleType name = “firstName" >

<xsd:restriction base = "xsd:string" >

<xsd:maxLength value = "20" />

</xsd:restriction>

</xsd:simpleType>
The length facet is used to restrict the string to an exact number of characters.
The minLength facet is used to specify a minimum length.

<xsd:simpleType name = “phoneNumber" >

<xsd:restriction base = "xsd:decimal" >

<xsd:precision value = “10" />

</xsd:restriction>

</xsd:simpleType>

Declaring Complex Types:

• There are several categories of complex types, but we discuss just one,
element-only elements which can have elements in their content, but no text.
Element-only elements are defined with the complexType tag.

• The sequence tag is used to contain an ordered group of elements.

• Use the all tag if the order is not important .

• Nested elements can include attributes that give the allowed number of
occurrences

(minOccurs, maxOccurs, unbounded)

• For ex:

<xsd:complexType name = "sports_car" >

<xsd:sequence>

<xsd:element name = "make” type = "xsd:string" />

<xsd:element name = "model” type = "xsd:string" />

<xsd:element name = "engine” type = "xsd:string" />

<xsd:element name = "year” type = "xsd:decimal" />

</xsd:sequence>

</xsd:complexType>
A default value is automatically assigned to the element when no other value is
specified.

In the following example the default value is "red":

<xs:element name="color" type="xs:string" default="red"/>

A fixed value is also automatically assigned to the element, and you cannot
specify another value.

In the following example the fixed value is "red":

<xs:element name="color" type="xs:string" fixed="red"/>

Attributes are optional by default. To specify that the attribute is required, use the
"use" attribute:

<xs:attribute name="lang" type="xs:string" use="required"/>


DTD vs XSD

S.NO. DTD XSD

DTD are the declarations that


XSD describes the elements in a
1. define a document type for
XML document.
SGML.

It doesn’t support
2. It supports namespace.
namespace.

It is comparatively harder It is relatively more simpler than


3.
than XSD. DTD.

4. It doesn’t support datatypes. It supports datatypes.

SGML syntax is used for


5. XML is used for writing XSD.
DTD.

6. It is not extensible in nature. It is extensible in nature.


It doesn’t give us much
It gives us more control on
7. control on structure of XML
structure of XML document.
document.

Any element which is made


It specifies only the root
8. global can be done as root as
element.
markup validation.

It doesn’t have any It specifies certain data


9.
restrictions on data used. restrictions.

10. It is not much easy to learn . It is simple in learning.

11. File here is saved as .dtd File in XSD is saved as .xsd file.

It is not a strongly typed It is a strongly typed


12.
mechanism. mechanism.

XSLT Introduction
XSL (eXtensible Stylesheet Language) is a styling language for XML.

XSLT stands for XSL Transformations.

XSLT is a language for transforming XML documents.

XPath is a language for navigating in XML documents.

XQuery is a language for querying XML documents.

XSLT - Transformation:
The root element that declares the document to be an XSL style sheet is
<xsl:stylesheet> or <xsl:transform>.

Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and


either can be used!

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

or:

<xsl:transform version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

XSLT <xsl:template> Element:

The <xsl:template> element is used to build templates.The match attribute is used to


associate a template with an XML element. The match attribute can also be used to
define a template for the entire XML document. The value of the match attribute is an
XPath expression (i.e. match="/" defines the whole document).
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Artist</th>

</tr>

<tr>

<td>.</td>

<td>.</td>
</tr>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

The <xsl:value-of> Element

The <xsl:value-of> element can be used to extract the value of an XML element and add
it to the output stream of the transformation:

ex:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>

<body>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Artist</th>

</tr>

<tr>

<td><xsl:value-of select="catalog/cd/title"/></td>

<td><xsl:value-of select="catalog/cd/artist"/></td>

</tr>

</table>

</body>

</html>
</xsl:template>

</xsl:stylesheet>

The <xsl:for-each> Element


The <xsl:for-each> element allows you to do looping in XSLT.The XSL <xsl:for-
each> element can be used to select every XML element of a specified node-set

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Artist</th>

</tr>

<xsl:for-each select="catalog/cd">

<tr>

<td><xsl:value-of select="title"/></td>

<td><xsl:value-of select="artist"/></td>

</tr>

</xsl:for-each>
</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

XSLT <xsl:sort> Element


To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each>
element in the XSL file:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Artist</th>

</tr>

<xsl:for-each select="catalog/cd">

<xsl:sort select="artist"/>
<tr>

<td><xsl:value-of select="title"/></td>

<td><xsl:value-of select="artist"/></td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

The select attribute indicates what XML element to sort on.

The <xsl:if> Element


To put a conditional if test against the content of the XML file, add an <xsl:if>
element to the XSL document.

Syntax
<xsl:if test="expression">

...some output if the expression is true...

</xsl:if>

To add a conditional test, add the <xsl:if> element inside the <xsl:for-each>
element in the XSL file:

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Artist</th>

<th>Price</th>

</tr>

<xsl:for-each select="catalog/cd">

<xsl:if test="price &gt; 10">

<tr>

<td><xsl:value-of select="title"/></td>

<td><xsl:value-of select="artist"/></td>

<td><xsl:value-of select="price"/></td>

</tr>

</xsl:if>

</xsl:for-each>

</table>

</body>

</html>
</xsl:template>

</xsl:stylesheet>

Note: The value of the required test attribute contains the expression
to be evaluated.

The code above will only output the title and artist elements of the
CDs that has a price that is higher than 10.

XSLT <xsl:choose> Element


The <xsl:choose> element is used in conjunction with <xsl:when> and
<xsl:otherwise> to express multiple conditional tests.

<xsl:choose>

<xsl:when test="expression">

... some output ...

</xsl:when>

<xsl:otherwise>

... some output ....

</xsl:otherwise>

</xsl:choose>
Where to put the Choose Condition
To insert a multiple conditional test against the XML file, add the
<xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to the XSL file:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<h2>My CD Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Artist</th>

</tr>

<xsl:for-each select="catalog/cd">
<tr>

<td><xsl:value-of select="title"/></td>

<xsl:choose>

<xsl:when test="price &gt; 10">

<td bgcolor="#ff00ff">

<xsl:value-of select="artist"/></td>

</xsl:when>

<xsl:otherwise>

<td><xsl:value-of select="artist"/></td>

</xsl:otherwise>

</xsl:choose>

</tr>

</xsl:for-each>

</table>

</body>

</html>
</xsl:template>

</xsl:stylesheet>

The <xsl:apply-templates> Element


The <xsl:apply-templates> element applies a template to the current
element or to the current element's child nodes.

If we add a "select" attribute to the <xsl:apply-templates> element, it will


process only the child elements that matches the value of the attribute. We
can use the "select" attribute to specify in which order the child nodes are
to be processed.

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<h2>My CD Collection</h2>

<xsl:apply-templates/>

</body>
</html>

</xsl:template>

<xsl:template match="cd">

<p>

<xsl:apply-templates select="title"/>

<xsl:apply-templates select="artist"/>

</p>

</xsl:template>

<xsl:template match="title">

Title: <span style="color:#ff0000">

<xsl:value-of select="."/></span>

<br />

</xsl:template>

<xsl:template match="artist">
Artist: <span style="color:#00ff00">

<xsl:value-of select="."/></span>

<br />

</xsl:template>

</xsl:stylesheet>
DTD vs XSD

N DTD XSD
o.

1) DTD s ta nds for Document XSD s ta nds for XML Schema Definition.

Type Definition.

2) DTDs a re derived from SGML XSDs a re written in XML.

s ynta x.

3) DTD does n't s upport XSD s upports datatypes for elements a nd

datatypes . a ttributes .

4) DTD does n't s upport XSD s upports names pace.

names pace.

5) DTD does n't define order for XSD defines order for child elements .

child elements .
6) DTD is not extens ible. XSD is extens ible.

7) DTD is not s imple to learn. XSD is s imple to learn beca us e you don't

need to lea rn new la ngua ge.

8) DTD provides les s control on XSD provides more control on XML

XML s tructure. s tructure.

You might also like