0% found this document useful (0 votes)
9 views18 pages

Week 7

The document provides an overview of XML (EXtensible Markup Language), detailing its purpose as a meta-language for storing and transferring data, its differences from HTML, and its key components such as elements, attributes, and namespaces. It also discusses Document Type Definition (DTD) for defining XML document structure and XML Schema (XSD) as an alternative for describing XML document structure. The document includes examples and explanations of XML elements, attributes, and the differences between DTD and XML Schema.

Uploaded by

Ranzu Maestro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views18 pages

Week 7

The document provides an overview of XML (EXtensible Markup Language), detailing its purpose as a meta-language for storing and transferring data, its differences from HTML, and its key components such as elements, attributes, and namespaces. It also discusses Document Type Definition (DTD) for defining XML document structure and XML Schema (XSD) as an alternative for describing XML document structure. The document includes examples and explanations of XML elements, attributes, and the differences between DTD and XML Schema.

Uploaded by

Ranzu Maestro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

6 – XML

COMPUTER ARTS AND TECHNOLOGICAL COLLEGE-LIGAO CITY, INC.


Washington Street, Binatagan, Ligao city

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>

Difference between XML and HTML


XML HTML
XML was designed to store data and transferthe HTML was designed to display data.
data.
XML focuses on what data is. HTML focus on how data looks.
In XML you can design your own tag. HTML has predefined tags.
XML uses parser to check & read xml fileseg. DOM, HTML don’t use any kind of parser
SAX

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.

XML Key Component


1.) XML Root Element
o XML must have root element. The first element after xml version declaration in file is a root
element.

<bookstore>
<book category="CHILDREN">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>

o In above example <bookstore> is root element.

2.) XML Element


o An XML element is everything from (including) the element's start tag to (including) the
element's end tag.
o An element can contain:
 other elements
 text
 attributes
 or a mix of all of the above...
o In above example <title>, <author>, <year> and <price> are elements.

3.) XML Attribute


o Attributes provide additional information about an element.
o Attributes often provide information that is not a part of the data. In the example below,the
file type is irrelevant to the data, but can be important to the software that wants to
manipulate the element

<file type="gif">computer.gif</file>

o XML Attributes Must be Quoted


o Attribute values must always be quoted. Either single or double quotes can be used. For a
person's sex, the person element can be written like this:

<person sex="female"> or <person sex='female'>

3
6 – XML

4.) XML Namespace


o The XML namespace is a special type of reserved XML attribute that you place in an XML tag.
o The reserved attribute is actually more like a prefix that you attach to any namespace you
create.
o This attribute prefix is "xmlns:” which stands for XML Namespace.
o The colon is used to separate the prefix from your namespace that you are creating.
o xmlns must have a unique value that no other namespace in the document has. What is
commonly used is the URI (Uniform Resource Identifier) or the more commonly used URL.

Xmlns=”http://www.mydomian.com/ns/animals/1.1”

Create a XML file that contains Book Information.


<xml version=”1.0”?>
<bookstore>
<book>
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
<book>
<title>WAD</title>
<author>Ralph Mosely</author>
<year>2001</year>
<price>395</price>
</book>
</bookstore>

DTD (Document Type Definition)


 The purpose of a DTD is to define the legal building blocks of an XML document. Itdefines the
document structure with a list of legal elements. A DTD can be declaredinline in your XML
document, or as an external reference.
 Internal DTD
o This is an XML document with a Document Type Definition:

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>

o The DTD is interpreted like this:


 !ELEMENT note (in line 2) defines the element "note" as having four elements:
"to,from,heading,body".
 !ELEMENT to (in line 3) defines the "to" element to be of the type "CDATA".
 !ELEMENT from (in line 4) defines the "from" element to be of the type "CDATA"
 External DTD
o This is the same XML document with an external DTD

<?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:

<!ELEMENT element-name (element-content)>

 Empty elements
o Empty elements are declared with the keyword EMPTY inside the parentheses:

<!ELEMENT img (EMPTY)>


 Elements with data
o Elements with data are declared with the data type inside parentheses:

<!ELEMENT element-name (#CDATA)>


or
<!ELEMENT element-name (#PCDATA)>
or
<!ELEMENT element-name (ANY)>
example:
<!ELEMENT note (#PCDATA)>

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

<!ELEMENT element-name (child-element-name)>


or
<!ELEMENT element-name (child-element-name,child-element-name,.....)>
example:
<!ELEMENT note (to,from,heading,body)>

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:

<!ELEMENT note (to,from,heading,body)>


<!ELEMENT to (#CDATA)>
<!ELEMENT from (#CDATA)>
<!ELEMENT heading (#CDATA)>
<!ELEMENT body (#CDATA)>

 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:

<!DOCTYPE root-element [element-declarations]>


example:
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#CDATA)>
<!ELEMENT from (#CDATA)>
<!ELEMENT heading (#CDATA)>
<!ELEMENT body (#CDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body></note>

 Declaring only one occurrence of the same element

7
6 – XML

<!ELEMENT element-name (child-name)>


example
<!ELEMENT note (message)>

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:

<!ATTLIST element-name attribute-name attribute-type default-value>

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 The attribute-default-value can have the following values:


Value Explanation
#DEFAULT value The attribute has a default value
#REQUIRED The attribute value must be included in the
element
#IMPLIED The attribute does not have to be included
#FIXED value The attribute value is fixed

 Attribute declaration example


DTD example:
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
XML example:
<square width="100"></square>

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

XSD Simple Elements


 A simple element is an XML element that can contain only text. It cannot contain any other elements
or attributes.
 Defining a Simple Element
o The syntax for defining a simple element is:

<xs:element name="aaa" type="bbb"/>


o Where aaa is the name of the element and bbb is the data type of the element.
o XML Schema has a lot of built-in data types. The most common types are:
 xs:string
 xs:decimal
 xs:integer
 xs:Boolean
 xs:date
 xs:time
 Example
o Here are some XML elements:

<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>

o And here are the corresponding simple element definitions:

<xs:element name="lastname" type="xs:string"/>


<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>

XSD Complex Elements


 A complex element is an XML element that contains other elements and/or attributes.
 There are four kinds of complex elements:
o empty elements
o elements that contain only other elements
o elements that contain only text
o elements that contain both other elements and text
 Examples of Complex Elements
o A complex XML element, "product", which is empty:

<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>

o A complex XML element, "food", which contains only text:

<food type="dessert">Ice cream</food>


o A complex XML element, "description", which contains both elements and text:

<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:

<xs:attribute name="aaa" type="bbb"/>


o Where aaa is the name of the attribute and bbb specifies the data type of the attribute.
 Default and Fixed Values for Attributes
o Attributes may have a default value OR a fixed value specified.
o A default value is automatically assigned to the attribute when no other value is specified.
o In the following example the default value is "EN":

<xs:attribute name="lang" type="xs:string" default="EN"/>


o A fixed value is also automatically assigned to the attribute, and you cannot specifyanother
value.
o In the following example the fixed value is "EN":

<xs:attribute name="lang" type="xs:string" fixed="EN"/>

 Optional and Required Attributes


o Attributes are optional by default. To specify that the attribute is required, use the
"use"attribute:

<xs:attribute name="lang" type="xs:string" use="required"/>

Difference between DTD and XML Schema

XML Schema DTD


Markup Any global element can be root. Can specify only the root
Prof. Arjun Bala, CE Department | 2160708 – Web Technology 10
6 – XML
validation No ambiguous content support. element in the instance
document. No ambiguous
content support.
Namespace Yes. Declarations only where No.
support multiple namespaces are used.
Code reuse Can reuse definitions using Poorly supported. Can use
named types. parameter entities.
Data type Provides flexible set of data types. No real data type support.
Validation Provides multi-field key cross
references. No co-occurrence
constraints.

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.

Explain XSL Transformation and XSL Elements


 The style sheet provides the template that transforms the document from one structure to another.
 In this case <xsl:template> starts the definition of the actual template, as the root of the source XML
document.
 The match = “/” attribute makes sure this begins applying the template to the root of the source
XML document.

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 &gt; 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>

Explain transforming with XSLT.


 It is possible to convert an XML document to XHTML using the browser’s own parser. However, this
is not always possible:
o The browser at the client end may not be suitable or equipped to do the transformation.
o It may not be a good idea to include the reference to the style sheet or even have the style

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

You might also like