XML 4
XML 4
XML
formatted
XML data
XSLT
Schema /
DTD
<contacts>
<contact>
<name>Rajiv Agarwal</name>
<address>
C-56/1, Anusandhan Bhavan
Institutional Area, Sector-62
Noida - 201307
</address>
<phoneNumber>91-120-2444711</phoneNumber>
<email>[email protected]</email>
</contact>
<contact>
<name>Varun Mehta</name>
<address>
No. 12 Sudder Street
Kolkata
West Bengal 700016
</address>
<phoneNumber>91-33-22521031</phoneNumber>
<email>[email protected]</email>
</contact>
…
…
…
</contacts>
Hierarchical Representation
of
Data
<contacts>
<contact>
<name>Rajeev
<address>C-56
<phoneNumber>91
<email>rajiv
</contact>
</contacts>
XML
Advantages of XML
Both machine and human readable.
Supports Unicode. <नाम></नाम>
Represents data hierarchically.
Strict syntax rules make it easy to make parsers.
Disadvantages of XML
Verbose
XML Syntax Rules
<p>First paragraph
<p>Second paragraph
In XML
<p>First paragraph</p>
<p>Second paragraph</p>
<br />
<phoneNumber>123456</phonenumber>
Tags must be properly nested
Properly Nested
<contact>
<name>Rajiv Agarwal</name>
</contact>
Improperly Nested
<contact>
<name>
Rajiv Agarwal
</contact>
</name>
<contacts>
<contact>...</contact>
<contact>...</contact>
</contacts>
<contacts>
<contact sex="F">...</contact>
</contacts>
Comments
CDATA Sections
<script language="javascript">
<![CDATA[
function min(a,b)
{
if (a < b) return a;
return b;
}
]]>
</script>
Element Attribute
<contacts>
<contact sex=”M”>
<name></name>
<address></address>
<phoneNumber></phoneNumber>
<email>[email protected]</email>
</contact>
Character Data
Contacts
o Contact ( Zero or More)
Sex (Attribute) - must be M or F
Name
First Name
Middle Name (Optional)
Last Name
Address
Street 1
Street 2
City
State
Pin
Phone Numbers
Phone Number (One or more)
Emails
Email (One or more)
Example XML
<contacts>
<contact sex="M">
<name>
<firstName>Rajiv</firstName>
<lastName>Agarwal</lastName>
</name>
<address>
<street1>C-56/1, Anusandhan Bhavan</street1>
<street2>Institutional Area, Sector-62</street2>
<city>Noida</city>
<state>Uttar Pradesh</state>
<pin>201307</pin>
</address>
<phoneNumbers>
<phoneNumber>91-120-2444711</phoneNumber>
</phoneNumbers>
<emails>
<email>[email protected]</email>
</emails>
</contact>
</contacts>
Making the DTD
Declaring Elements
Syntax :
<!ELEMENT element-name (element-contents)>
contact Element:
<!ELEMENT contact (name, address, phoneNumbers,
emails)>
name Element:
<!ELEMENT name (firstName, middleName?,
lastName)>
name Element:
<!ELEMENT address (street1, street2, city, state,
pin)>
firstName Element:
<!ELEMENT firstName (#PCDATA)>
phoneNumbers Element:
<!ELEMENT phoneNumbers (phoneNumber+)>
Elements with any content (text or elements)
<abcd></abcd>
<abcd />
Element should contain one of the following elements –
<!ELEMENT phoneNumbers (
(mobileNumber | landline)+
)>
<phoneNumbers>
<mobileNumber>
9087654321
</mobileNumber>
<landline>
1234567890
</landline>
<landline>
1234567891
</landline>
<landline>
1234567891
</landline>
</phoneNumbers>
Declaring Attributes
<!ATTLIST element-name
attribute-name
attribute-type
default-value >
<contact
yahooID="[email protected]">
<!ATTLIST contact
yahooID
CDATA
"not specified">
Inline DTDs
<!DOCTYPE contacts [
]>
<contacts>
<contact>
<name>
External DTDs
xmlDoc.validateOnParse="true";
if(!xmlDoc.load(sPath))
{
var msg = 'An error has occurred: \n';
msg += "After: " + xmlDoc.parseError.filepos + " bytes\n";
msg += "Line Number: " + xmlDoc.parseError.line + "\n";
msg += "Column Number: " + xmlDoc.parseError.linepos + "\n";
msg += "Column Number: " + xmlDoc.parseError.srcText + "\n";
msg += "Reason: " + xmlDoc.parseError.reason + "\n";
alert(msg);
}
else
{
alert('Document validated successfully');
}
}
Usage:
<input type="button"
onclick="validateDocument('file://c:/contacts.xml')"
value="Validate"/>
Using Visual Basic
xmlDoc.validateOnParse = True
MsgBox(xmlDoc.parseError.reason)
End If
End Sub
Using Java
db.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException e) throws SAXException {
XML Namespaces
Need for namespaces –
FINANCIAL
CIRCUIT
APPLICATION
LED DESIGN
ACCOUNT
LOAN
FLIPFLOP
Original XML file – without namespaces
<?xml version="1.0"?>
<circuit>
<seriesConnection>
<resistor>15</resistor>
<resistor>20</resistor>
<bank type="LED" number="3"></bank>
</seriesConnection>
</circuit>
<?xml version="1.0"?>
<ckt:circuit xmlns:ckt="http://www.mec.com/ckt">
<ckt:seriesConnection>
<ckt:resistor>15</ckt:resistor>
<ckt:resistor>20</ckt:resistor>
<ckt:bank type="LED" number="3"></ckt:bank>
</ckt:seriesConnection>
</ckt:circuit>
<?xml version="1.0"?>
<circuit xmlns="http://www.mec.com/ckt">
<seriesConnection>
<resistor>15</resistor>
<resistor>20</resistor>
<bank type="LED" number="3"></bank>
</seriesConnection>
</circuit>
We need to use xmlns:xyz when we are going to mix elements from two namespaces
– otherwise just use the default namespace attribute.
Examples where we mix elements from separate namespaces – XML Schemas and
XSL.
URL is only a unique name provided to the parser – it is not really accessed. Often,
the URL points to a webpage where the namespace is described.
Logically, Schemas and DTDs define namespaces.
XML Schemas
1. XML Schemas use XML as the underlying language (unlike DTDs which
have their own language)
o The editor to make a schema can be a normal XML editor (not a big
deal since even notepad can be used to edit XML files).
o The parser used to parse schema is an XML parser (a big deal
because we don’t need two types of parsers).
2. XML Schemas have better support for data types.
3. XML Schemas are object-oriented in their approach - They allow
restricting and extending existing data types to define our own.
schema contacts
element contact
restriction
name
pattern
…
firstName
…
Schema file - *.xsd (XML Schema Definition)
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
targetNamespace=”http://www.abcdefgh.com”
xmlns="http://www.abcdefgh.com"
elementFormDefault="qualified" >
…
…
</xsd:schema>
<contacts xmlns="http://www.sunero.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sunero.com contacts.xsd">
…
…
</contacts>
firstName element that contains a string containing the first name of the
contact
<xsd:element name="firstName"
type="xsd:string"
fixed="fixed value"
default="default value" />
Different data types supported
Name Values
string “this is a string”
boolean { true, false, 0, 1 }
decimal 3.14
float 314e-3, 314, 0, -0, +INF, -INF, NaN
double “
dateTime CCYY-MM-DD hh:mm:ss
CC ---- 00 to 99
YY ---- 00 to 99
MM ---- 01 to 12
DD ---- Depends on MM and YY
date CCYY-MM-DD
time hh:mm:ss
gYear, gYearMonth, gMonthDay, gDay, gMonth Part of dateTime – but may need extra
hyphens
integer
long, int, short, byte
unsignedLong, unsignedInt,
unsignedShort, unsignedByte
positiveInteger, negativeInteger,
nonPositiveInteger, nonNegativeInteger
hexBinary, base64Binary, anyURI
Facets
Allow you to create new data types from existing ones, by placing
restrictions.
<xsd:element name="sex">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="M" />
<xsd:enumeration value="F" />
</xsd:restriction>
</xsd:simpleType> </xsd:element>
price element should contain maximum of 2 decimal places
List containing atoms which are either single digits or single alphabets –
example for named types (vs. anonymous types), lists and unions.
<xsd:simpleType name="singleDigit">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="singleAlphabet">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[a-z]" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="singleDigitOrAlphabet">
<xsd:union memberTypes="singleDigit singleAlphabet" />
</xsd:simpleType>
<xsd:element name="listOfItems">
<xsd:simpleType>
<xsd:list itemType="singleDigitOrAlphabet" />
</xsd:simpleType>
</xsd:element>
<xsd:element name="name">
<xsd:complexType>
<xsd:sequence> Indicator
1. Any number of attribute tags may follow after sequence – but they should be
inside complexType tag.
2. Use can take the values required, optional, prohibited .
3. xsd:attribute tag can also have ‘fixed’, ‘default’ and ‘type’ attributes like
xsd:element.
<length units="cm">3.14</length>
1. Length element has simple data because it will contain values like 3.14
2. But elements with attributes are complex data
3. Its actually simple data ‘extended’ by some complex data
<xsd:element name="length">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="units">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="cm" />
<xsd:enumeration value="in" />
<xsd:enumeration value="ft" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="font">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="b" type="xsd:string"
minOccurs="0" maxOccurs="unbounded"
/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Indicators
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<style type="text/css">
.rowHeader
{
font-weight: bold;
color: white;
background-color: black;
}
</style>
</head>
<body>
<table border="1">
<tr class="rowHeader">
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr>
<td>-</td>
<td>-</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
For-each & value-of
<xsl:for-each select="/contacts/contact">
<tr>
<td>
<xsl:value-of select="./name/firstName"/>
</td>
<td>
<xsl:value-of select="./name/lastName"/>
</td>
</tr>
</xsl:for-each>
If
<xsl:for-each select="/contacts/contact">
<xsl:if test="(position() mod 2) = 0">
<tr>
<td>
<xsl:value-of select="./name/firstName"/>
</td>
<td>
<xsl:value-of select="./name/lastName"/>
</td>
</tr>
</xsl:if>
<xsl:if test="position() mod 2 = 1">
<tr bgcolor="#EEEEEE">
<td>
<xsl:value-of select="./name/firstName"/>
</td>
<td>
<xsl:value-of select="./name/lastName"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
Choose
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>