Week 7
Week 7
JUNIOR HIGH
SCHOOL-G8
Programming
Using Web
Technology
Quarter 3 – Module 7
WEEK 7
1
6 – XML
XML
What is XML?
XML is a meta-language, which can be used to store data & act as a mechanism to transfer
information between dissimilar systems.
XML stands for EXtensible Markup Language.
XML is a markup language much like HTML.
XML was designed to describe data.
XML tags are not predefined in XML. You must define your own tags.
XML is self describing.
XML uses a DTD (Document Type Definition) to formally describe the data.
<?xml version=”1.0”?>
<Person>
<Firstname>Ralph</Firstname>
<Lastname>Mosely</Lastname>
</Person>
Use of XML
Used to exchange data between dissimilar systems.
Used to describe content of document.
XML can be used as database to store data.
Features of XML
XML has its own tag so it’s self describing.
Language Independent:Any language is able to read & write XML.
OS Independent: can be work on any platform.
Readability: It’s a plain text file in user readable format so you can edit or viewin simple editor.
2
6 – XML
Hierarchical: It has hierarchical structure which is powerful to express complex data and simple to
understand.
<bookstore>
<book category="CHILDREN">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
<file type="gif">computer.gif</file>
3
6 – XML
Xmlns=”http://www.mydomian.com/ns/animals/1.1”
4
6 – XML
<?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>Ravi</to>
<from>Ketan</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Ravi</to>
<from>Narendra</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
o This is a copy of the file "note.dtd" containing the Document Type Definition:
5
6 – XML
<?xml version="1.0"?>
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
DTD - Elements
In the DTD, XML elements are declared with an element declaration. An element declaration has the
following syntax:
Empty elements
o Empty elements are declared with the keyword EMPTY inside the parentheses:
o #CDATA means the element contains character data that is not supposed to be parsed bya
parser.
o #PCDATA means that the element contains data that IS going to be parsed by a parser.
o The keyword ANY declares an element with any content.
o If a #PCDATA section contains elements, these elements must also be declared.
Elements with children (sequences)
o Elements with one or more children are defined with the name of the children
elementsinside the parentheses:
6
6 – XML
o When children are declared in a sequence separated by commas, the children must appearin
the same sequence in the document. In a full declaration, the children must also bedeclared,
and the children can also have children. The full declaration of the notedocument will be:
Wrapping
o If the DTD is to be included in your XML source file, it should be wrapped in aDOCTYPE
definition with the following syntax:
7
6 – XML
o The example declaration above declares that the child element message can only occurone
time inside the note element.
Declaring minimum one occurrence of the same element
<!ELEMENT element-name (child-name+)>
example
<!ELEMENT note (message+)>
o The + sign in the example above declares that the child element message must occur oneor
more times inside the note element.
Declaring zero or more occurrences of the same element
<!ELEMENT element-name (child-name*)>
example
<!ELEMENT note (message*)>
o The * sign in the example above declares that the child element message can occur zeroor
more times inside the note element.
Declaring zero or one occurrences of the same element
<!ELEMENT element-name (child-name?)>
example
<!ELEMENT note (message?)>
o The? Sign in the example above declares that the child element message can occur zeroor
one times inside the note element.
DTD – Attributes
Attributes provide extra information about elements.
Attributes are placed inside the start tag of an element.
Declaring Attributes
o In the DTD, XML element attributes are declared with an ATTLIST declaration. Anattribute
declaration has the following syntax:
o As you can see from the syntax above, the ATTLIST declaration defines the elementwhich
can have the attribute, the name of the attribute, the type of the attribute, and thedefault
attribute value.
o The attribute-type can have the following values:
8
6 – XML
Value Explanation
CDATA The value is character data
(eval|eval|..) The value must be an enumerated value
ID The value is an unique id
IDREF The value is the id of another element
IDREFS The value is a list of other ids
NMTOKEN The value is a valid XML name
NMTOKENS The value is a list of valid XML names
ENTITY The value is an entity
ENTITIES The value is a list of entities
NOTATION The value is a name of a notation
xml: The value is predefined
o In the above example the element square is defined to be an empty element with
theattributes width of type CDATA. The width attribute has a default value of 0.
XML Schema
An XML Schema describes the structure of an XML document.
XML Schema is an XML-based alternative to DTD.
The XML Schema language is also referred to as XML Schema Definition (XSD).
XML Schema is a W3C Recommendation.
XSD Elements
XML Schemas define the elements of your XML files. It’s of two types:
o Simple
o Complex Type
9
6 – XML
<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>
<product pid="1345"/>
o A complex XML element, "employee", which contains only other elements:
1
0
6 – XML
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
<description>
It happened on <date lang="norwegian">03.03.99</date> ....
</description>
XSD Attributes
Simple elements cannot have attributes. If an element has attributes, it is considered to be of a
complex type. But the attribute itself is always declared as a simple type.
How to Define an Attribute?
o The syntax for defining an attribute is:
XSL
What is XSL?
XSL stands for EXtensible Stylesheet Language.
XSL = Style Sheets for XML
XSL describes how the XML document should be displayed!
XSL ‐ More Than a Style Sheet Language
XSL consists of three parts:
o XSLT‐a language for transforming XML documents
o XPath‐a language for navigating in XML documents
o XSL‐FO‐a language for formatting XML documents
What is XSLT?
XSLT stands for XSL Transformations.
XSLT is the most important part of XSL.
XSLT transforms an XML document into another XML document.
XSLT uses XPath to navigate in XML documents.
Linking
The style sheet is linked into the XML by adding the connecting statement to the XML document:
<?xml‐stylesheet type=”text/xsl” href=”libstyle.xsl” ?>
XSL Transformations
XSLT is the most important part of XSL.
11
6 – XML
XSLT is used to transform an XML document into another XML document, or another type of
document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by
transforming each XML element into an (X)HTML element.
With XSLT you can add/remove elements and attributes to or from the output file. You can also
12
6 – XML
rearrange and sort elements, perform tests and make decisions about which elements to hide and
display, and a lot more.
A common way to describe the transformation process is to say that XSLT transforms an XML
source‐tree in to an XML result‐tree.
XSLT Uses XPath:
o XSLT uses XPath to find information in an XML document.
o XPath is used to navigate through elements and attributes in XML documents.
XSLT Works as:
o In the transformation process, XSLT uses XPath to define parts of the source document
that should match one or more predefined templates.
o When a match Is found, XSLT will transform the matching part of the source document
into the result document.
XSL Elements
XSL contains many elements that can be used to manipulate, iterate and select XML, for output.
o value‐of
o for‐each
o sort
o if
o choose
<xsl:value-of> Element
The <xsl:value‐of> element extracts the value of a selected node.
The <xsl:value‐of> element can be used to select the value of an XML element and add it to the
output.
Syntax
<xsl:value‐of select="expression" />
o expression: This is Required. An XPath expression that specifies which node/attribute to
extract the value from. It works like navigating a file system where a forward slash (/) selects
subdirectories.
<xsl:for-each> Element
The XSL <xsl:for‐each> element can be used to select every XML element of a specified node‐set.
<xsl:if> Element
To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL
document.
Syntax
<xsl:if test="expression">
...some output if the expression is
true...
</xsl:if>
<xsl:sort> Element
The <xsl:sort> element is used to sort the output.
13
6 – XML
<xsl:sort select="artist"/>
The select attribute indicates what XML element to sort on.
14
6 – XML
Example using value-of, for-each and if
<?xml version="1.0" encoding="ISO‐8859‐1"?>
<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">
<xsl:if test="price > 10">
<tr>
<td>
<xsl:value‐of select="title"/>
</td>
<td>
<
xsl:value‐of select="artist"/>
</td>
</tr>
</xsl:if>
</xsl:for‐each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<xsl:choose> Element
The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express
multiple conditional tests.
15
6 – XML
Syntax
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates> Element
The <xsl:apply-templates> element applies a template to the current element or to the current
element's child nodes.
If we add a select attribute to the <xsl:apply-templates> element it will process only the child
element that matches the value of the attribute. We can use the select attribute to specify the order
in which the child nodes are processed.
Look at the following XSL style sheet:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
</xsl:stylesheet>
16
6 – XML
sheet available!
The answer to this process the document and style sheet outside of the browser’s own
mechanism for doing this task.
This task can be done either on the client side or the server side.
Using JavaScript
One way to process and transform XML on the client side is using JavaScript,
which has several features for doing the task very well.
<html>
<body>
<script type=”text/javascript”>
//Load the XML document
var xml= new
ActiveXObject(“Microsoft.XMLDOM”)
xml.async=false
xml.load(“lib.xml”)
//Load the XSL document
var xsl= new
ActiveXObject(“Microsoft.XMLDOM”)
xsl.async= false
xsl.load(“libstyle.xsl”)
//Do the actual transform
document.write(xml.transfor
mNode(xsl))
</script>
</body>
</html>
Above example shows one way to transform with JavaScript using Microsoft’s
proprietary Application Programming Interface (API) for the Internet Explorer browser.
It is also possible to process XML using the DOM.
Using both the of these mechanisms it is possible to also traverse an XML document
and process either according to a style sheet or simply using the JavaScript to make the
stylistic decisions.
Apart from JavaScript, it is also possible to use other programming languages (such as
Java and .Net) to process and then output a transformed document.
17