Professional Documents
Culture Documents
http://www.tizag.com/xmlTutorial/
Two problems with HTML:
– 1. Fixed set of tags and attributes
• User cannot define new tags or attributes
– 2. There are few restrictions on
arrangement or order of tag
appearance in a document
This is called xml declaration which states that what type xml version you
are using and type of encoding you using.
encoding- character set used in this document is all character set used by
western european languages or it may be UTF- 8.
<root>
<child>
<subchild>.....</subchild>
E. Swathi, </child>
Assist Prof, CSE,
CBIT
</root>
➢ Attributes must have quoted values
<note date="12/11/2007">
<to>Tove</to>
<from>Jani</from>
</note>
➢ Entity References
Some characters have a special meaning in XML.
Example
<?xml version="1.0" encoding="ISO-8859-1"?> <!– a →
<!DOCTYPE note SYSTEM “InternalNote.dtd"> <!– b →
Syntax :-
Examples
gender isE.attribute
Swathi, Assist Prof, CSE, gender is element
CBIT
•
XML tree
XML documents form a tree structure that starts at "the root" and branches to "the
leaves".
• XML document contains a single element. That single element is called root element
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<?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
E. Swathi, Assistweekend</body>
Prof, CSE,
</note> CBIT
The DTD above is interpreted like this:
note.dtd
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
• Elements
• Attributes
• Entities
• PCDATA
• CDATA
CDATA:
In a DTD, XML elements are declared with an element declaration with the
following syntax:
1. Empty Elements
Example:<!ELEMENT br EMPTY>
XML example:<br />
E. Swathi, Assist Prof, CSE,
CBIT
2.Elements with Parsed Character Data
Elements with only parsed character data are declared with #PCDATA
inside
parentheses:
<!ELEMENT element-name (#PCDATA)>
Example: <!ELEMENT from (#PCDATA)>
Elements declared with the category keyword ANY, can contain any
combination of parsable data:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
Elements with one or more children are declared with the name of the
children elements inside parentheses:
The example above declares that the child element "message" must
occur once, and only once inside the "note" element.
The + sign in the example above declares that the child element
"message" must occur one or more times inside the "note" element.
E. Swathi, Assist Prof, CSE,
CBIT
Occurrences cont…..
The * sign in the example above declares that the child element
"message" can occur zero or more times inside the "note" element.
The ? sign in the example above declares that the child element
E. Swathi,
"message" can occur zero or one timeAssist Prof,
inside theCSE,
"note" element.
CBIT
6. Declaring either/or Content
The example above declares that the "note" element must contain a "to"
element, a "from" element, a "header" element, and either a "message" or
a "body" element.
The example above declares that the "note" element can contain zero or
more occurrences of parsed character data, "to", "from", "header", or
"message" elements.
E. Swathi, Assist Prof, CSE,
CBIT
• Location of modifiers
1. If modifiers put after the parenthesis it applies to whole group
of elements
<!ELEMEMT student (name|rollno)*>
2. Any no of links or any no of img elements but not both
<!ELEMENT body (link*|img*)>
3. Any no of child link and img elements but they must come in
pairs
<!ELEMENT body (link, img)*>
4. Any no fo child link followed by any number of img element
<!ELEMENT body ( link*, img*)>
5. <!ELEMENT person ( name|(firstname, lastname))>
A person element can have either single element name or a
firstname and lastname.
Value Explanation
value The default value of the attribute
#REQUIRED The attribute is required
#IMPLIED The attribute is not required
#FIXED value The attribute value is fixed
Syntax
Use the #REQUIRED keyword if you don't have an option for a default
value, but still want to force the attribute to be present.
Syntax
Example
DTD: <!ATTLIST contact fax CDATA #IMPLIED>
Valid XML: <contact fax="555-667788" />
Valid XML: <contact />
Use the #IMPLIED keyword if you don't want to force the author to
include an attribute, and you don't have an option for a default value.
Syntax
<!ATTLIST element-name attribute-name attribute-type #FIXED "value">
Example
Use the #FIXED keyword when you want an attribute to have a fixed
value without allowing the author to change it. If an author includes
another value, the XML parser will return an error.
E. Swathi, Assist Prof, CSE,
CBIT
2. Enumerated Attribute Values
Syntax
Use enumerated attribute values when you want the attribute value to be
one of a fixed set of legal values.
Syntax:
DTD Example:
<!ENTITY writer SYSTEM "http://www.w3schools.com/entities.dtd">
or <!ENTITY writer SYSTEM “d:\test.txt”
<!ENTITY copyright SYSTEM
"http://www.w3schools.com/entities.dtd">
XML example:
<author>&writer; ©right;</author>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
E. Swathi, Assist Prof, CSE,
In the example above, there will beCBIT
no conflict because the two
<table> elements have different names. 55
Disadvantages of DTD
1. DTD does not follow the XML syntax it
requires new syntax.
2. Namespace does not supported
3. No data types.
4. No modularity and no reuse of elements.
5. No inheritance for elements or
attributes
An XML Schema:
DTD file
<xs:schema>
...
...
</xs:schema>
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>
E. Swathi, Assist Prof, CSE,
CBIT
The <schema> element may contain some attributes.
A schema declaration often looks something like this:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>
xmlns:xs= "http://www.w3.org/2001/XMLSchema"
this elements indicates that the elements and data types used in this XML
schema are come from the "http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
elements in this schema comes from "http://www.w3schools.com" namespace.
syntax
<xs:element name="xxx" type="yyy"/>
xs:element name=“rollno">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value=“1"/>
<xs:maxInclusive value=“70"/>
</xs:restriction> <xs:element name="car">
</xs:simpleType> <xs:simpleType>
/xs:element> <xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
E. Swathi, Assist Prof, CSE,
CBIT
</xs:element>
E. Swathi, Assist Prof, CSE,
CBIT
XSD- Complex elements
A complex element contains other elements and/or attributes.
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
he "employee" element can be declared directly by naming the element, like this:
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
➢only the "employee" element can use the specified complex type.
➢<sequence> indicator. This means that the child elements must
appear in the same order as they are declared.
E. Swathi, Assist Prof, CSE,
CBIT
. The "employee" element can have a type attribute that refers to
the name of the complex type to use:
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
If you use the method described above, several elements can refer to the
same complex type
<xs:element name="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:element name="member" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>E. Swathi, Assist Prof, CSE,
CBIT
1.empty elements
<product prodid="1345" />
<xs:element name="product">
<xs:complexType>
<xs:attribute name="prodid“
type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
XML schema
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
XML schema
<xs:element name=“address">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name=“aptname" type="xs:string"/>
<xs:element name=“city" type="xs:hyderabad"/>
</xs:sequence>
</xs:complexType>
</xs:element>
E. Swathi, Assist Prof, CSE,
CBIT
XSD indicators
➢ We can control HOW elements are to be used in documents
with indicators.
7 indicators are there
order indicators
1.All 2.Choice 3.Sequence
Occurrence indicators:
• maxOccurs
• minOccurs
Group indicators:
• Group name
• attributeGroup name
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:group ref="persongroup"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
E. Swathi, Assist Prof, CSE,
CBIT
Attribute Groups
<xs:attributeGroup name="groupname">
...
</xs:attributeGroup> <xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname"
type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>
<xs:element name="person">
<xs:complexType>
<xs:attributeGroup ref="personattrgroup"/>
</xs:complexType>
</xs:element>
E. Swathi, Assist Prof, CSE,
CBIT
Presenting XML
➢ XSL stands for extensible stylesheet language used to present XML as
HTML.
CSS = Style Sheets for HTML
➢ HTML uses predefined tags, and the meaning of each tag is well understood
➢ Adding styles to HTML elements are simple. Telling a browser to display an element in
a special font or color,
XSL = Style Sheets for XML
XSL describes how the XML document should be displayed!
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
example
<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>
<xsl:if test="expression">
...some output if the expression is true...
</xsl:if>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
<xsl:template match="name">
<xsl:value-of select="last_name"/>,
<xsl:value-of select=“middle_name"/>,
<xsl:value-of select="first_name"/>
</xsl:template>
<xsl:template match="person">
<xsl:apply-templates select="name"/>
</xsl:template>
E. Swathi, Assist Prof, CSE,
CBIT
</xsl:stylesheet>
xsl:choose, xsl:when, xsl:otherwise:
<xsl:choose>
<xsl:when test=criteria>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>