Web Technologies Unit-Ii: .SGML .XSLT .Soap SAX - Dom - Use of XML
Web Technologies Unit-Ii: .SGML .XSLT .Soap SAX - Dom - Use of XML
UNIT-II
XM
L
INTRODUCTION TO XML:
(eXtensible markup language) the World Wide Web consortium (W3C) begins their work on
XML, another meta markup language. The first XML version 1.0, published in 1998. The
difference between XML and HTML is HTML is a mark up language where as XML is a mark
up language used to create new mark up languages. HTML limits you to use only fixed number
of tags, where as XML allows to create new tags. For example, you are developing website for
a college, then you have tags like
<SNO>,<STUDENTNAME>,<DOB> etc. HTML,XML languages are derived from
standard generalized markup language (SGML).
XML and related technologies and those are:
SGML,XSL,W3C,SOAP,XSLT,DOM,SAX
.SGML, standard generalized markup language is basis for all markup language.
XSL,extensible stylesheet language is a combination of XML and style sheets.
.XSLT,XSL transformations provides rules for transformations from one XML to
another.
.SOAP, simple object access protocol is communication protocol for internet to
XML documents and it provides notifications for events.
.SAX , simple APL for XML, predefined application package interface.
. DOM, document object model.
---------------use of XML------------------------
-simplifies the data exchange procedure
-easy to organize the document
-tags or document elements are reusable
-XML provides consistency in display of information
Applications of XML
1.electronic commerce (popularly known as E-commerce)
2. financial funds transfer
multimedia messages and messaging exchange
3.better environment for data transfer
The XML document is language neutral. That means a java program can generate an
XML document and this document can be parsed by perl.
XML files are independent of an operating system.
1
3.1.3 Differences between XML and HTML
XML HTML
XML features:
XML allows the user to define his own tags and his own document structure.
XML document is pure information wrapped in XML tags.
XML is a text based language, plain text files can be used to share data. XML
provides a software and hardware independent way of sharing data.
simple XML document:
XML tags and attributes depend on the user. XML declaration always starts with xml key word.
<?xml version =’’1.0’’ encoding=”UTF-8” standardalone=”no”?>
Processing instruction:<?xml-stylesheet href=”simple.xsl” type=”text/xsl”
?>
Here you can give version along with the xml key word, version is attributing and value
1.0 indicates that xml 1.0 version you are using.
First declaration tag starts with left angular bracket along with question
mark(<?),ending with question mark followed by the right angular braket(?>)second
assignment is comment and then we start with actual xml program. First element<student>
called root element. A root element contains other sub elements.here we used <name>as sub
element and its also known as container element,because it contains other sub elements<first
name>,<last name>.sub elements is also called as children.
Example 1:
<?xml version="1.0"?>
<student>
<name >
<firstname>Raju</firstname>
<lastname>ch</lastname>
</name>
<name>
<firstname>mamatha</firstname>
<lastname>ch</lastname>
</name>
</student>
<!--<para type=”note”>xml data cannot contain <</para> &apso; & "-->
The filename extension used for xml program is .xml. the name of above program is one
.xml.
Empty elements:
<line width=”100” height=”200” />
In XML the basic entity is element the elements are used for defining the tags.
The elements typically consist of opening and closing tag. Mostly only one element is used to
define a single tag.
The syntax of writing any element for opening tag is <element name>
The syntax of writing any closing element for closing tag is </element name>
An empty tag can be defined by putting a /(forward slash)before closing bracket. A
space or a tab character is not allowed in the element name or in attribute name.
Namespace in XML:
Sometime we need to create two different elements by the same name. The xml
document allows creating different elements which are having the common name. This
technique is known as name space. In some web documents it becomes necessary to have the
same name for two different elements. Here different elements mean the elements which are
intended for different purpose.
Example 2:
<?xml version="1.0"?>
< file>
<text fname="photo1.jpg">
<describe> it is a .jpg file</describe>
</text>
<text fname="photo2.bmp">
<describe>it is abmp file</describe>
</text>
</file>
An XML document can be defined as:
• Well-formed: If the XML document adheres to all the general XML rules such as tags
must be properly nested, opening and closing tags must be balanced, and empty tags must
end with '/>', then it is called as well-formed.
OR
Valid: An XML document said to be valid when it is not only well-formed, but it also
conforms to available DTD that specifies which tags it uses, what attributes those tags can
contain, and which tags can occur inside other tags, among other properties
Well formed xml : 1)should contain one and exactly one root element
In HTML:<img> <br><hr>
In XML: i)<hr/> ii)<hr></hr>
Predefined entities:
< > & " '
< > & " '
NAMESPACE:
Client.xml server.xml
<table>
<table><ro><co>javascript</co></ro>
<tr><td>javascript</td></tr>
</table> </table>
<tech>
<table>
<tr><td>javascript</td></tr>
</table>
<table>
<ro><co>javascript</co></ro>
</table>
</tech>
<tr><td>javascript</td></tr>
</table>
</client>
<server>
<table>
<ro><co>javascript</co></ro>
</table>
</server>
</tech>
Solution:
Client.xml
<c:table>
<c:tr><c:td>javascript</c:td></c:tr>
</c:table>
Server.xml: <s:table>
<ro><co>javascript</co></ro>
</s:table>
BINDING:
<c:table xmlns:c=”http://it.site.jusl.ac/client”>
<tr><td>javascript</td></tr>
</c:table>
</c:table>
<s:table>
<s:ro><s:co>javascript</s:co></s:ro>
</s:table>
</tech>
DOCTYPE
Types
DTD can be classified on its declaration basis in the XML document, such as:
Internal DTD
External DTD
Features
Following are some important points that a DTD describes:
the elements that can appear in an XML document.
Validation - It gives a way to check the validity of XML files by checking whether the elements appear
in the right order, mandatory elements and attributes are in place, the elements and attributes have not
been inserted in an incorrect way, and so on.
Elements: the basic entity is element. The elements are used for defining the tags. The
elements typically consist of opening and closing tag. Mostly only one element is used to
define a single tag.
name>Tutorials Point</name>
XML__________________ <
3TYPES OF ELEMENTS:
:1.EMPTY ELEMENTS/standalone Elements
of attributes
2.ANY ELEMENT(unrestricted element)
• An element that can contain any content
• useful when transferring a lot of mixed or unknown data
• Note:It removes all validity checkings.so avoid using this type of declaration
EXAMPLE: <!ELEMENT test ANY>
sequence operator:
in DTD-----<!ELEMENT parent (exp1,exp2,exp3,……..)>
In XML------
<emp>
<fname>uttam</fname>
<midname>kumar</midname>
<lname>roy</lname>
</emp>
Choice operator:[ | ]
<product>
<discountprice>2000</discountprice>
</product>
Composite operator:[ ( ) ]
Combining operators:
<!ELEMENT order(item+)>
<!ELEMENT item(#PCDATA | desc)*>
<!ELEMENT desc(#PCDATA)>
<order>
<item>
XBEE
<desc>WPAN development kit</desc>
</item>
</order>
Entities
There are five built-in entities that play in well-formed XML, they are:
Attributes: it provides extra information about elements. These are always placed inside the
starting tag of an element. Attribute always come in name/value pairs.
Ex:<img src=’raj.jpg”>
<!DOCTYPE address [
<address>
</address>
CDATA: CDATA also 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.
EXAMPLE:
<code>
<![CDATA[
<script language=”javascript”>
function less(x,y){
Return x<y;
A=less(2,3);
</script>
]]>
<code>
CDATA
CDATA attributes are strings , any text is allowed
ATTRIBUTES:
<line width=’100’/>
Attribute types:
1)String type:
CDATA
2)Tokenized type
i)ID ii)IDREF iii)IDREFS iv)NMTOKEN(S) V)Entitie(s)
3)Enumerated type
4)Notation type
ID
The values of an ID attribute must be a name, that name must be unique. ID resembles the
primary key concept
EXAMPLE:<!ATTLIST question no ID #REQUIRED>
<!ATTLIST emp id ID #REQUIRED>
IDREF or IDREFS
It is similar to that of Foreign key concept in data base
It is used to establish connections between elements
Example(in DTD):<!ATTLIST question no ID #REQUIRED>
<!ATTLIST answer qno IDREF #REQUIRED>
(IN XML): <question no=“1”>
What is full form of DTD?
</question>
<question no=“2>”
What is full form of XML?
</question>
<answer qno=“1”>
Document type Definition
</answer>
XML-------<stu roll=’101’>samir</stu>
<sub sid=’s1’>web technology</sub>
<marks ref=’101 s1’>82</marks>
ENTITY or ENTITIES
An ENTITY attribute’s must be the name of a single ENTITY. The value of an
ENTITIES attribute may contain multiple entity names separated by white space.
NMTOKEN or NMTOKENS
Name token attributes are a restricted form of string attribute, but there are no other restrictions
on the word.
ENUMARATED TYPES:
Enumeration
notation
List of Names Enumerated : You can specify that the value of an attribute must be taken from a
specific list of names. This frequently called an enumerated type because each of the possible
values must be explicitely enumerated in the declaration
Notation:
<!NOTATION jpg SYSTEM ‘image/gif’>
<!ENTITY logo SYSTEM ‘ju.jpg NDATA jpg>
Attribute Defaults:
Default value EXPLANATION
Statement)
<line width=’100’/>
#REQUIRED
The attribute must have an explicitly specified value for every occurrence of the element in the
document
DTD---------<!ATTLIST price currency CDATA #REQUIRED>
XML--------<price currency=’INR’>100</price>-----------correct
<price>100</price>-------------------------------incorrect
#IMPLIED
The attribute value is not required and no default value is provided. If a value is not specified
the XMP processor must proceed without one.
“value” An attrubute can be given any legal value as a default. The attribute value is not
required on each element of the document, and if it is not present it will appear to be the
specified default
#FIXED “value” An attribute declaration may specify that an attribute has a fixed value. In
this case, the attribute is not required, but if it occurs, it must have the specified value. If it is
not present, it will appear to be the specified default
1. InternalDTD:
In an internal DTD we enclose the DTD in a <DOCTYPE> element, providing the name
of the root element of the document in the <DOCTYPE>
EXAMPLE 3
<?xml version="1.0"?>
<!DOCTYPE PURCHASE[
<!ELEMENT PURCHASE (CUSTOMER)?>
<!ELEMENT CUSTOMER (name,address,date,order)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT order (items*)>
<!ELEMENT item (product,price)>
<!ELEMENT product (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>
<purchase>
<customer>
<name>vandana</name>
<address>vzm</address>
<date>dec,14,1986</date>
<order>
<item>
<product>GOLD</product>
<price>$.55</price>
</item>
<item>
<product>SAREES</product>
<price>$.55</price>
</item>
</order>
</customer>
<customer>
<name>ramesh</name>
<address>vzm</address>
<date>may,08,1979</date>
<order>
<item>
<product> web technologies</product>
<price>$00</price>
</item>
</order>
</customer>
</purchase>
In this type, an external DTD file is created ans its name must be specified in the
corresponding XML file. XML document illustrate the use of external DTD.
Step1: creation of DTD file[student.dtd]
Open notepad type following code into it-
<!ELEMENT PURCHASE (CUSTOMER*)>
<!ELEMENT CUSTOMER (name,address,date,order)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT order (items*)>
<!ELEMENT item (product,price)>
<!ELEMENT product (#PCDATA)>
<!ELEMENT price (#PCDATA)>
Xml schema is an XML alternative to DTD. The XML schemas are used to represent the
structure of XML document. XML schema language is also referred to as XML schema
definition. The XML schema language is called as XML schema definition language
(XSD).XML schema are written in XML . XML schema supports name spaces.
Limitations of DTD:
There is no built-in datatype in DTD
Strengths of SCHEMA:
The <schema >element is the root element of every XML schema.<schema>element may
contains some attributes.
SCHEMA STRUCTURE:
</xs:schema>
Targetnamespace
elementFormDefault: qualified or unqualified
SCHEMA ELEMENTS:
<all> <attribute> <choice> <union> <complexType> <group>
<element> <list> <schema>
Element Definition:
Xml-----<stu>
1.simple element.
2. complex element.
1.simple element is an XML element that can contain only text. It can’t contain any other
elements or attributes.
Syntax: <xs:element name=”aaa” type=”bbb”/>
--------------------------------------------------------------------------------------------------------------------
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema>
<xs:element name=’greeting’ type=’xs:string’/>
</xs:schema>
IN XML: <?xml version=”1.0”?> ----------------------------save it as sam.xml
<greeting>HELLO EVERYBODY</greeting>
EXAMPLE 4:
XML schema[student schema.xsd]
<?xml version=”1.0”?>
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema>
<xs:element name=”student”/>
<xs:complex type>
<xs:sequence>
<xs:element name=”name”type=”xs:string”/>
<xs:element name=”address” type=”xs:string”/>
<xs:element name=”std” type=”xs:string”/>
<xs:element name=”marks” type=”xs:integer”/>
</xs:sequence>
</xs:complex type>
</xs:element>
</xs:schema>
The xs is qualifier used to identify the schema elements and types.the document
element of schema is xs:schema. The xs:schema is root element.it takes the attribute
xmlns:xs which has the value http://www.w3.org/2001/XML schema. this declaration
indicates that document should follow the rules of XML schema.
Then comes xs:element which is used to define the xml element. In above case the
element student is of complex type who have four chils elements:name,address,std and
marks. All these elements are of simple type string.
Occurrence Indicator:
minOccurs: <xs:element name=”married” type=”xs:boolean” minOccurs=’0’/>
maxOccurs: <xs:element name=”married” type=”xs:boolean” minOccurs=’4’ maxOccurs=’6’
/>
Schema DTD
minOccurs=’0’ maxOccurs=’unbounded’ * Zero or More
minOccurs=’0’ ? optional
none none Exactly once
<xs:complexType>
</xs:complexType>
Ex:<xs:complexType name=’person’>
<xs:sequence>
<xs:element name=’fname’ type=’xs:string’/>
<xs:element name=’lname’ type=’xs:string’/>
</xs:sequence>
</xs:complexType >
Xml: <person>
<fname>uttam</fname>
<lname>Roy</lname>
</person>
Defining Attributes:
EX:
<xs:complexType name=’person’>
<xs:sequence>
<xs:element name=’fname’ type=’xs:string’/>
<xs:element name=’lname’ type=’xs:string’/>
</xs:sequence>
<xs:attribute name=’id’ type=’xs:string’/>
</xs:complexType >
<xs:element name=’emp’ type=’person’/>
Xml:
<emp id=’1234’>
<fname>uttam</fname>
<lname>Roy</lname>
</emp>
Default
fixed
Occurrence Indicator:
Order indicator:
Sequence : the child elements in xml document must appear in the order they are declared in
the XSD schema
all : The child elements described in XSD schema can appear in the XML document in
any order
choice : only one of the child elements describes in XSD schema can in XML Document
Sequence indicator:
<xs:complexType name=’person’> XML
<xs:sequence minOccurs=’0’> <person>
<xs:element name=’fname’ type=’xs:string’/> <fname>uttam</fname>
<xs:element name=’lname’ type=’xs:string’/> <lname>Roy</lname>
</xs:sequence> </person>
</xs:complexType >
all indicator:
<xs:complexType name=’person’> <emp>
<xs:all> <lname>Roy</lname>
<xs:element name=’fname’ type=’xs:string’/> <fname>Uttam</fname>
<xs:element name=’lname’ type=’xs:string’/> </emp>
</xs:all>
</xs:complexType >
<xs:element name=’emp’ type=’person’/>
EX:2<xs:complexType name=’person’>
<xs:all>
<xs:element name=’fname’ type=’xs:string’/>
<xs:element name=’Mname’ type=’xs:string’ minOccurs=’0’/>
<xs:element name=’lname’ type=’xs:string’/>
</xs:all>
</xs:complexType >
<xs:element name=’emp’ type=’person’/>
choice indicator:
<xs:complexType name=’person’>
<xs:choice>
</xs:choice>
</xs:complexType >
XML:
<emp id=121>
<lname>Roy</lname>
</emp>
EX:2
<xs:complexType name=’documenttype’>
<xs:choice maxOccurs=’unbounded’>
</xs:choice>
</xs:complexType >
Composite Indicators:
<xs:complexType name=’person’>
<xs:sequence>
<xs:element name=’fname’ type=’xs:string’/>
<xs:element name=’lname’ type=’xs:string’/>
<xs:choice>
<xs:element name=’dob’ type=’xs:date’/>
<xs:element name=’age’ type=’xs:integer’/>
</xs:choice>
</xs:sequence>
</xs:complexType >
<xs:element name=’emp’ type=’person’/>
XML-------<emp>
<fname>Uttam</fname>
<lname>Roy</lname>
<age>19</age>
</emp>
REFERENCING:
EX:
<xs:complexType name=’stuType’>
<xs:sequence>
<xs:element ref=’fname’/>
<xs:element ref=’roll’/>
</xs:sequence>
<xs:complexType >
<xs:complexType name=’empType’>
<xs:sequence>
<xs:element ref=’fname’/>
<xs:element ref=’salary’/>
</xs:sequence>
<xs:complexType>
GROUP INDICATORS:
1 Group
2 attributegroup
<xs:group name=’groupname’>
…..
…….
</xs:group>
Ex:<xs:group name=’personinfo’>
<xs:sequence>
</xs:sequence>
</xs:group>
<xs:complexType name=’stuType’>
<xs:sequence>
<xs:group ref=’personinfo’/>
</xs:complexType>
<xs:attributeGroup name=’datedata’>
</xs:attributeGroup >
MIXED CONTENT:
<biodata>
</biodata>
<xs:sequence maxOccurs=’unbounded’>
</xs:sequence>
</xs:complexType >
XSLT Syntax
Let's take an example to take a sample XML file and transform it into a well
formatted HTML document.
Define an XSLT stylesheet document for the above XML document. You should follow the criteria give
below:
Create the XSLT document which satisfies the above requirements. Name it as employee.xsl and save
it in the same location of employee.xml.
Employee.xsl
1. <?xml version = "1.0" encoding = "UTF-8"?>
2. <!-- xsl stylesheet declaration with xsl namespace:
3. Namespace tells the xlst processor about which
4. element is to be processed and which is used for output purpose only
5. -->
6. <xsl:stylesheet version = "1.0"
7. xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
8. <!-- xsl template declaration:
9. template tells the xlst processor about the section of xml
10. document which is to be formatted. It takes an XPath expression.
11. In our case, it is matching document root element and will
12. tell processor to process the entire document with this template.
13. -->
14. <xsl:template match = "/">
15. <!-- HTML tags
16. Used for formatting purpose. Processor will skip them and browser
17. will simply render them.
18. -->
19. <html>
20. <body>
21. <h2>Employee</h2>
22. <table border = "1">
23. <tr bgcolor = "#9acd32">
24. <th>ID</th>
25. <th>First Name</th>
26. <th>Last Name</th>
27. <th>Nick Name</th>
28. <th>Salary</th>
29. </tr>
30. <!-- for-each processing instruction
31. Looks for each element matching the XPath expression
32. -->
33. <xsl:for-each select="class/employee">
34. <tr>
35. <td>
36. <!-- value-of processing instruction
37. process the value of the element matching the XPath expression
38. -->
39. <xsl:value-of select = "@id"/>
40. </td>
41. <td><xsl:value-of select = "firstname"/></td>
42. <td><xsl:value-of select = "lastname"/></td>
43. <td><xsl:value-of select = "nickname"/></td>
44. <td><xsl:value-of select = "salary"/></td>
45. </tr>
46. </xsl:for-each>
47. </table>
48. </body>
49. </html>
50. </xsl:template>
51. </xsl:stylesheet>
Update employee.xml document with the following xml-stylesheet tag. Set href value to employee.xsl
1. <?xml version = "1.0"?>
2. <?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
3. <class>
4. ...
5. </class>
Updated "employee.xml"
1. <?xml version = "1.0"?>
2. <?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
3. <class>
4. <employee id = "001">
5. <firstname>Aryan</firstname>
6. <lastname>Gupta</lastname>
7. <nickname>Raju</nickname>
8. <salary>30000</salary>
9. </employee>
10. <employee id = "024">
11. <firstname>Sara</firstname>
12. <lastname>Khan</lastname>
13. <nickname>Zoya</nickname>
14. <salary>25000</salary>
15. </employee>
16. <employee id = "056">
17. <firstname>Peter</firstname>
18. <lastname>Symon</lastname>
19. <nickname>John</nickname>
20. <salary>10000</salary>
21. </employee>
22. </class>
Employee.xsl
1. <?xml version = "1.0" encoding = "UTF-8"?>
2. <xsl:stylesheet version = "1.0"
3. xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
4. <xsl:template match = "/">
5. <html>
6. <body>
7. <h2>Employee</h2>
8. <table border = "1">
9. <tr bgcolor = "purple">
10. <th>ID</th>
11. <th>First Name</th>
12. <th>Last Name</th>
13. <th>Nick Name</th>
14. <th>Salary</th>
15. </tr>
16. <!-- for-each processing instruction
17. Looks for each element matching the XPath expression
18. -->
19. <xsl:for-each select="class/employee">
20. <tr>
21. <td>
22. <!-- value-of processing instruction
23. process the value of the element matching the XPath expression
24. -->
25. <xsl:value-of select = "@id"/>
26. </td>
27. <td><xsl:value-of select = "firstname"/></td>
28. <td><xsl:value-of select = "lastname"/></td>
29. <td><xsl:value-of select = "nickname"/></td>
30. <td><xsl:value-of select = "salary"/></td>
31. </tr>
32. </xsl:for-each>
33. </table>
34. </body>
35. </html>
36. </xsl:template>
37. </xsl:stylesheet>
XSLT <xsl:if> Element
The XSLT <xsl:if> element is used to specify a conditional test against the content of the XML file.
1. <xsl:if test="expression">
2. ...some output if the expression is true...
3. </xsl:if>
Parameter explanation
test: It specifies a condition in xml data to test.
1. <?xml version = "1.0" encoding = "UTF-8"?>
2. <xsl:stylesheet version = "1.0"
3. xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
4. <xsl:template match = "/">
5. <html>
6. <body>
7. <h2>Employee </h2>
8. <table border = "1">
9. <tr bgcolor = "pink">
10. <th>ID</th>
11. <th>First Name</th>
12. <th>Last Name</th>
13. <th>Nick Name</th>
14. <th>Salary</th>
15. </tr>
16.
17. <xsl:for-each select = "class/employee">
18.
19. <xsl:if test = "salary > 30000">
20. <tr>>
21. <td><xsl:value-of select = "@id"/></td>
22. <td><xsl:value-of select = "firstname"/></td>
23. <td><xsl:value-of select = "lastname"/></td>
24. <td><xsl:value-of select = "nickname"/></td>>
25. <td><xsl:value-of select = "salary"/></td>
26. </tr>
27. </xsl:if>
28. </xsl:for-each>
29.
30. </table>
31. </body>
32. </html>
33. </xsl:template>
34. </xsl:stylesheet>
1. <xsl:choose>
2. <xsl:when test="expression">
3. ... some output ...
4. </xsl:when>
5. <xsl:otherwise>
6. ... some output ....
7. </xsl:otherwise>
8. </xsl:choose>
Parameter explanation
test: It specifies a condition in xml data to test.
1. <?xml version = "1.0" encoding = "UTF-8"?>
2. <xsl:stylesheet version = "1.0"
3. xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
4. <xsl:template match = "/">
5. <html>
6. <body>
7. <h2>Employee</h2>
8. <table border = "1">
9. <tr bgcolor = "pink">
10. <th>ID</th>
11. <th>First Name</th>
12. <th>Last Name</th>
13. <th>Nick Name</th>
14. <th>Salary</th>
15. <th>Grade</th>
16. </tr>
17.
18. <xsl:for-each select = "class/employee">
19.
20. <tr>
21. <td><xsl:value-of select = "@id"/></td>
22. <td><xsl:value-of select = "firstname"/></td>
23. <td><xsl:value-of select = "lastname"/></td>
24. <td><xsl:value-of select = "nickname"/></td>
25. <td><xsl:value-of select = "salary"/></td>
26.
27. <td>
28. <xsl:choose>
29. <xsl:when test = "salary > 50000">
30. High
31. </xsl:when>
32.
33. <xsl:when test = "salary > 40000">
34. Medium
35. </xsl:when>
36.
37. <xsl:otherwise>
38. Low
39. </xsl:otherwise>
40. </xsl:choose>
41. </td>
42. </tr>
43. </xsl:for-each>
44. </table>
45. </body>
46. </html>
47. </xsl:template>
48. </xsl:stylesheet>
1. <xsl:apply-template
2. select = Expression
3. mode = QName>
4. </xsl:apply-template>
Parameter explanation
1) selec It is used to process nodes selected by XPath expressions from the list of all
t nodes and its children.
1. <?xml version = "1.0" encoding = "UTF-8"?>
2. <xsl:stylesheet version = "1.0"
3. xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
4. <xsl:template match = "/">
5. <html>
6. <body>
7. <h2>Employees</h2>
8. <xsl:apply-templates select = "class/employee" />
9. </body>
10. </html>
11. </xsl:template>
12.
13. <xsl:template match = "class/employee">
14. <xsl:apply-templates select = "@id" />
15. <xsl:apply-templates select = "firstname" />
16. <xsl:apply-templates select = "lastname" />
17. <xsl:apply-templates select = "nickname" />
18. <xsl:apply-templates select = "salary" />
19. <br />
20. </xsl:template>
21. <xsl:template match = "@id">
22. <span style = "font-size = 25px;">
23. <xsl:value-of select = "." />
24. </span>
25. <br />
26. </xsl:template>
27. <xsl:template match = "firstname">
28. First Name:<span style = "color:brown;">
29. <xsl:value-of select = "." />
30. </span>
31. <br />
32. </xsl:template>
33. <xsl:template match = "lastname">
34. Last Name:<span style = "color:green;">
35. <xsl:value-of select = "." />
36. </span>
37. <br />
38. <</xsl:template>
39.
40. <xsl:template match = "nickname">
41. Nick Name:<span style = "color:blue;">
42. <xsl:value-of select = "." />
43. </span>
44. <br />
45. </xsl:template>
46. <xsl:template match = "salary">
47. Marks:<span style = "color:red;">
48. <xsl:value-of select = "." />
49. </span>
50. <br />
51. </xsl:template>
52. </xsl:stylesheet>
5. EXAMPLE 6:
First create .xml file[demo.xml]
<?xml-stylesheet type="text/xsl" href="demo1.xsl"?>
<menu>
<dvd>
<name>Raju</name>
<actor>chiru</actor>
<place>London</place>
<amount>$00</amount>
</dvd>
<dvd>
<name>Ravi</name>
<actor>pavan</actor>
<place>America</place>
<amount>$00</amount>
</dvd>
</menu>
Demo1.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>name</th>
<th>actor</th>
</tr>
<xsl:for-each select="menu/dvd">
<tr>
<td><xsl:value-of select="name"/>
</td>
<td><xsl:value-of select="actor"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XML WITH CSS:
EXAMPLE 7: [sampcss.xml]
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="student.css"?>
<class>
<student>
<rno>121</rno>
<name>sridhar</name>
<branch>CSE</branch>
<marks>60</marks>
</student>
<student>
<rno>131</rno>
<name>siva ranjini</name>
<branch>IT</branch>
<marks>70</marks>
</student>
<student>
<rno>141</rno>
<name>shruthi</name>
<branch>ECE</branch>
<marks>80</marks>
</student>
<student>
<rno>151</rno>
<name>ramesh</name>
<branch>EEE</branch>
<marks>90</marks>
</student>
<student>
<rno>161</rno>
<name>vandana</name>
<branch>ME</branch>
<marks>40</marks>
</student>
<student>
<rno>171</rno>
<name>babu</name>
<branch>MCA</branch>
<marks>50</marks>
</student>
</class>
DOCUMENT OBJECT MODEL (DOM):
Body
P
PR
<html>
<body>
<p>this is para1</p>
<script language='javascript'>
var a=document.createElement("pr");
var b=document.createTextNode("This is new para");
a.appendChild(b);
document.body.appendChild(a);
</script>
</body>
</html>
THE XML DOM defines the objects and properties of all XML elements ,and the
methods to access them.
all modern browsers have a build –in XML parser that can be used to read and
manuplate XML.
the parser reads XML into memory and converts it into an XML DOM object that can be
access with java script.
There are some differences between Microsoft’s XML parser and the parsers used in other
browsers. The Microsoft parser supports loading of both XML files and XML strings (text),
while other browsers use separate parsers, all parsers contain functions to traverse XML
trees, access, insert, and delete nodes.
The DOM is a programming API for documents. It closely resembles the structure of
the documents it models. For instance, consider this table, taken from an HTML document:
<TABLE>
<BODY>
<TR>
<TD> shady grove</TD>
<TD>Aeolian</TD>
</TR>
<TR>
<TD> over the river, Charlie</TD>
<TD> dorian</TD>
</TR>
</TBODY>
</TABLE>
The DOM represents this table likes this:
(figure)
Example 8:[overseas.html]
<html>
<body>
<script type =”text/javascript”>
Try
{
xmlDocument=new ActiveXObject(“Microsoft.XMLDOM”);
}
Catch(e)
{
Try
{
xmlDocument=document.implementation.createDocument(“,”null);
}
Catch(e)
{
Alert(e.message)
}
}
Try
{
xmlDocument.async=false;
xmlDocument.load(“example3.xml);
document.write(“xml document example3.xml is loaded”);
}
Catch(e)
{
Alert(e.message);
}
</script>
</body>
</html>
(figure)
using XML processors: DOM AND SAX
S.NO DOM(Document object model) SAX (simple API for xml parsing)
<sname>guru charan</sname>
<branch>IT</branch>
</member>
<member>
<sno>07</sno>
<sname>K SV K srikanth</sname>
<branch>IT</branch>
</member>
<member>
<sno>08</sno>
<sname>Praveen kumar</sname>
<branch>IT</branch>
</member>
<member>
<sno>09</sno>
<sname> santhosh</sname>
<branch> IT</branch>
</member>
<member>
<sno>10</sno>
<sname> murthy</sname>
<branch> IT</branch>
</member>
<member>
<sno> 11</sno>
<sname>suresh </sname>
<branch>IT</branch>
</member>
<member>
<sno>12</sno>
<sname>raju</sname>
<branch>IT</branch>
</member>
<member>
<sno>13</sno>
<sname>praveen</sname>
<branch>IT</branch>
</member>
<member>
<sno>14</sno>
<sname>praneeth</sname>
<branch>IT</branch>
</member>
<member>
<sno>15</sno>
<sname>praveen</sname>
<branch>IT</branch>
</member>
</members>
Regmembers.html
<html>
<head><title>registered members:portal team</title></head>
<body bgcolor="wheat">
<center><font size=5 face="bookman old style" color="blue"> portal team
details</font></center>
<br><br>
<xml id="rRecords" src="D:\Raju\WT Examples\reg.xml"></xml>
<table id ="emptable" width="50%" align="center" border="2" bordercolor="blue"
cellspacing="0" datasrc="#rRecords" dataPageSize="5">
<thead>
<tr style="background-color:cyan ;color:blue;">
<th>S.No.</th>
<th>Name</th>
<th>branch</th>
</tr>
</thead>
<tbody>
<tr>
<td align ="center"><span datafld="sno"></span></td>
<td><span datafld="sname"></span></td>
<td><span datafld="branch"></span></td>
</tr>
</tbody>
</table>
<br><br>
<center>
<input type="button" value="<<first" onClick="emptable.firstPage()">
<input type="button" value="<previous" onClick="emptable.previousPage()">
<input type="button" value="next>" onClick="emptable.nextPage()">
<input type="button" value="last>>" onClick="emptable.lastPage()">
</center>
</body>
</html>