1XML
1XML
UNIT –I
INTRODUCTION
XML is Extensible mark up Language that defines a set of rules for encoding documents. Although the
design focuses on documents the language is widely used for representation of random structures.
Features
Designed to be self-descriptive.
<body> <From>PETER</from>
<p>To:SIMON <heading>Reminder</
heading>
<br> <body>Meeting at
8am</body>
From:PETER
</note>
</p>
</pre>
<h1>Reminder</h1>
<p>Meeting at
8am</p>
</body>
</html>
Output
Note
To : SIMON
From : PETER
Reminder
Meeting at 8
am
Note :Here the output for both html and xml is same the difference is that in html we have p,h1 tags
but in XML we have self-defined tags like From, To and so on.
Advantages
!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"
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
The next line describes the root element of the document (like saying: "this document is a note"):
<note>
The next 4 lines describe 4 child elements of the root (to, from, heading, and body).
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
And finally the last line defines the end of the root element.
</note>
If DTD is declared in external file <!DOCTYPE> definition must contain reference to the DTD file .
CSS is used to add style and display information to an XML document. It can
format the whole XML document.
Employee
Background-color: pink;
Font-size:12pt;
Display: block;
Color: blue;
Margin-left: 50px;
Employee.xml file
<?xml version="1.0"?>
<employee>
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>[email protected]</email>
</employee>
1.4 Namespace
XML Namespace is used to avoid element name conflict in XML document.
Namespace Declaration
Syntax:
Example:
<cont:contact xmlns:cont="http://sssit.org/contact-us">
<cont:name>Vimal Jaiswal</cont:name>
<cont:company>SSSIT.org</cont:company>
<cont:phone>(0120) 425-6464</cont:phone>
</cont:contact>
Elements name are defined by the developer, hence there is a chance for
conflict in name of the elements.
Example
Table 1:
<table>
<tr>
<td>Aries</td>
<td>Bingo</td>
</tr>
</table
Table 2:
<table>
<name>Computer table</name>
<width>80</width>
<length>120</length>
</table>
Here conflict arises because both have table , though they have different
name and meaning.
Using Prefix
<h:table>
<h:tr>
<h:td>Aries</h:td>
<h:td>Bingo</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>Computer table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
Xmlns Attribute
Example:
<root>
<h:table xmlns:h="http://www.abc.com/TR/html4/">
<h:tr>
<h:td>Aries</h:td>
<h:td>Bingo</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="http://www.xyz.com/furniture">
<f:name>Computer table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
URI
1.5 Schema
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
xmlns="https://www.w3schools.com"
elementFormDefault="qualified">
<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>
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<?xml version="1.0"?>
<note
xmlns="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3schools.com/xml note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The formatting language allows you to apply styles but browser support is
limited
XSLT allows you to transform your XML document into another form.
For example, you could use XSLT to dynamically output some (or all) of the
contents of your XML file into an HTML document containing other content.
Declare XSL
<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">
Open the XML file (click on the link below) - The XML document will be
displayed with color-coded root and child elements (except in Safari).
There is a plus (+) or minus sign (-) to the left of the elements that can be
clicked to expand or collapse the element structure.
To view the raw XML source, right-click in XML file and select "View
Source"!
<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>