Você está na página 1de 10

1. An XML Schema describes the structure of an XML document.

2. The purpose of an XML Schema is to define the legal building blocks of an XML
document, just like a DTD.
3. XML Schema is an XML-based alternative to DTD.
4. XML Schemas are much more powerful than DTDs.
5. The XML Schema language is also referred to as XML Schema Definition (XSD).
XML Schema Example
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<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:schema>

An XML Schema:

It defines elements that can appear in a document


It defines attributes that can appear in a document
It defines which elements are child elements
It defines the order of child elements
It defines the number of child elements
It defines whether an element is empty or can include text
It defines data types for elements and attributes
It defines default and fixed values for elements and attributes

Soruce:

Shiporder.xml
<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="4364"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>Krishnan</orderperson>
<shipto>
<name>SMIT College</name>
<address>Muthu Nagar</address>
<city>Chennai</city>
<country>India</country>
</shipto>
<item>
<title>JSP Begins</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>110.90</price>
</item>
<item>
<title>Web Technology</title>
<quantity>1</quantity>
<price>59.90</price>
</item>
</shiporder>
The XML document above consists of a root element, "shiporder", that contains a required
attribute called "orderid". The "shiporder" element contains three different child elements:
"orderperson", "shipto" and "item". The "item" element appears twice, and it contains a "title", an
optional "note" element, a "quantity", and a "price" element.
The line above: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" tells the XML
parser that this document should be validated against a schema. The line: xsi: noNamespace
SchemaLocation="shiporder.xsd" specifies WHERE the schema resides (here it is in the same
folder as "shiporder.xml").

shiporder.xsd

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


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
<!-- definition of attributes -->
<xs:attribute name="orderid" type="xs:string"/>
<!-- definition of complex elements -->
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="item">

<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element ref="item" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="orderid" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>

Output:

XSL/XSLT

XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML
documents.
XSLT stands for XSL Transformations.XSLT to transform XML documents into other
formats, like XHTML.

Element
The <xsl:template> Element
A template contains rules to apply when a specified node is matched
The <xsl:value-of> Element
Extract the value of a selected node
The <xsl:for-each> Element
Select every XML element of a specified node-set with the <xsl:for-each> element
The <xsl:sort> Element
Sort the output in a node-set
The <xsl:if> Element
Put a conditional test against the content of an XML file
The <xsl:choose> Element
This example will add a pink background-color to the "Name" column WHEN the price of the
CLASS is higher than 10:
The <xsl:apply-templates> Element
Apply templates to elements

Source :
Student.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Transfer.xsl"?>
<student>
<class>
<name>Kumar</name>
<rollno>19</rollno>
<dept>IT</dept>
<subject>WT</subject>
<year>1985</year>
</class>
<class>
<name>Ram</name>
<rollno>25</rollno>
<dept>CSE</dept>
<subject>OOAD</subject>
<year>1988</year>
</class>
<class>
<name>Jaya</name>
<rollno>21</rollno>
<dept>ECE</dept>
<subject>DAA</subject>
<year>1982</year>
</class>
<class>
<name>Prabu</name>
<rollno>28</rollno>
<dept>Mech</dept>
<subject>CCM</subject>
<year>1999</year>
</class>
<class>
<name>Kumaran</name>
<rollno>45</rollno>
<dept>EEE</dept>
<subject>ECM</subject>
<year>1995</year>
</class>
</student>

Transfer.xsl
<?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>Student Database</h2>
<table border="2">
<tr bgcolor="#9acd32">
<th style="text-align:left">Student Name</th>
<th style="text-align:left">Dept.</th>
</tr>
<xsl:for-each select="student/class">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="dept"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Output: