Unit - 1 SAX
Unit - 1 SAX
Unit - 1 SAX
Built-in Parsers: All major browsers have built-in parsers to access and
manipulate XML file.
XML Parser: Convert text into XML object
1. DOM defines the properties and methods to access and edit XML elements
Two types:
1. DOM – create an internal structure
2. SAX (Simple API for XML) – not creating an internal structure. Works like
for an event handler
Working Principle of XML parser:
Characteristic of XPath:
1. It is a syntax for defining the parts of the XML document
2. It defines the Path expression to navigate XML documents
3. It contains standard library functions
4. It is recommend by W3C
XPath Expressions:
1. Node-name select all the nodes with the given name
2. / start to select from the root node
3. // select the node from the current node that match the selection
4. . select the current node
5. .. select the parent of the current node
6. @ select the attributes
7. Example select all the nodes with the name Example
8. Class/student select all the student elements that are the children of
class
9. //student select all the student nodes
Example Program:
Testxsl.xml:
<?xml-stylesheet type="text/xsl" href="testxsl.xsl"?>
<sports>
<game>
<name> cricket</name>
<players> 11 </players>
<like> India </like>
</game>
<game>
<name> Kabadi </name>
<players> 7 </players>
<like> India </like>
</game>
<game>
<name> Volleyball</name>
<players> 6 </players>
<like> USA </like>
</game>
</sports>
Testxsl.xls
<xsl-stylesheet version="1.0">
<html>
<head> </head>
<body>
XSL Transforamtion Example
<table border="1">
<tr> <td>Name </td> <td> No. of Players </td> <th>Country </td> </tr>
<xsl:for-each select="/sports/game">
<tr>
<td> <xsl:value-of-select="name"></td>
<td> <xsl:value-of-select="players"></td>
<td> <xsl:value-of-select="like"></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
XQuery
Purpose: It is sued to query the XML document
Characteristics of XQuery:
1. It is the language for querying XML data
2. It is similar to SQL for databases
3. It is built on XPath expressions
4. It is supported by all major databases
5. It is recommended by W3C
Application Area of XQuery:
1. Extract information to use in a Web Service
2. Generate summary reports
3. Transform XML data to XHTML
4. Search Web documents for relevant information
Example Program:
Accessing the value of XML file using PHP:
<?php
$l=simplexml_load_file("test.xml") or die("Failed to load");
foreach($l->children() as $t)
{
echo $t->test."<br>";
echo $t->name."<br>";
}
?>