0% found this document useful (0 votes)
14 views

ITEC-60-Module

Xml lesson

Uploaded by

Rennan Sinsay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

ITEC-60-Module

Xml lesson

Uploaded by

Rennan Sinsay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Republic of the Philippines

CAVITE STATE UNIVERSITY


CCAT Campus
Rosario, Cavite
(046) 437-9505 / (046) 437-6659
[email protected]
www.cvsu-rosario.edu.ph

ITEC60 LAB:
Integrated Programming
and Technologies 1

John Daryll C. Cajes


Instructor I
Chapter 1: XML (eXtensible Markup Language)

Lesson 1: Introduction to XML

What is XML?

XML serves as a tool that is not tied to specific software or hardware, enabling the storage and
transfer of data.

XML, which stands for eXtensible Markup Language, is a markup language similar to HTML. It was
created with the purpose of storing and transferring data in a self-descriptive manner. XML is not tied to any
specific software or hardware and has been recommended by the W3C (World Wide Web Consortium).

XML operates without predefined tags. Tags such as <to> and <from> in the provided example are
not part of any XML standard; they are introduced by the document author. In contrast, HTML relies on
predefined tags like <p>, <h1>, <table>, and so forth. In XML, the author is responsible for defining both
the tags and the structure of the document.

The Difference of XML and HTML

XML and HTML were conceived with distinct objectives:

▪ XML was created for data transportation, prioritizing the specification of what the data is.
▪ HTML was devised for data presentation, emphasizing the depiction of how the data appears.
▪ Unlike HTML tags, which are predefined, XML tags are not predefined.

How XML is Extensible?

XML demonstrates extensibility, meaning that in most cases, XML applications will function properly
even if new data is introduced or existing data is removed.

Consider an application initially designed to handle the original version of a note.xml document, containing
elements such as <to>, <from>, <heading>, and <body>.
Now envision a revised version of note.xml with additional elements like <date> and <hour>, while
omitting the <heading> element.

Due to XML's flexible structure, older versions of the application can still operate effectively.

Example:

In this example:

• The root element is <person>.

• Inside <person>, there are three child elements: <name>, <age>, and <city>, each
containing specific information about a person.

Lesson 2: XML Tree Structure

XML documents are structured in the form of element trees.

A tree in XML begins with a root element and extends outward with child elements stemming from
the root. Every element has the potential to contain sub-elements, referred to as child elements.
Here is an example:

In this representation:

• library is the root element.

• Under library, there are two book elements, each with its own set of child elements (title, author,
genre, and published_year) and their respective text content or attribute values.

Here is the XML code for it:


Lesson 3: XML Syntax Rules

XML (Extensible Markup Language) has several syntax rules that must be followed for a document to be
considered well-formed. These rules ensure consistency and compatibility across XML documents. Here are
the key syntax rules:

1. Case Sensitivity: XML is case-sensitive. Tags, attribute names, and values must be written with the
correct case. For example, <Book> is different from <book>.

2. Root Element: Every XML document must have a single root element that encapsulates all other
elements. The root element is the top-level element in the hierarchy.

3. Nesting: XML elements must be properly nested within each other. Every opening tag must have a
corresponding closing tag, and elements cannot overlap. For example, <a><b></a></b> is
invalid because the closing tags are in the wrong order.

4. Empty Elements: Empty elements can be represented using a self-closing tag, such as <tag/>.
For example, <linebreak/> is an empty element.

5. Attributes: Element attributes must have values enclosed in quotation marks (" or '). For example,
id="001" is valid, but id=001 is not.

6. Comments: Comments in XML are enclosed within <!-- and -->. Comments can appear anywhere
in the document but cannot be nested.

7. Entity References: Certain characters have special meaning in XML (such as <, >, &, ', and "). To
represent these characters as literal text, they must be replaced with predefined entities. For example,
&lt; represents <.

8. XML Declaration: XML documents can start with an optional XML declaration, which specifies the
XML version and encoding. For example:

9. Whitespace Handling: XML parsers generally ignore whitespace between elements, but whitespace
within text nodes is preserved. However, leading and trailing whitespace in element content is
significant unless explicitly trimmed.

10. Namespace Declarations: XML allows the use of namespaces to avoid element name conflicts.
Namespace declarations can be included in the root element. For example:
Lesson 4: XML Elements

An XML element encompasses everything from its start tag, inclusive, to its end tag, inclusive. An
element can contain text, attributes, other elements, or a mix of these components.

Here is an example:

In this example:

❖ <book> is the XML element, representing a book.

❖ <title>, <author>, <genre>, and <published_year> are child elements of <book>, each
containing specific information about the book.

❖ The text content of each child element represents the corresponding data:

➢ The title of the book is "Harry Potter and the Sorcerer's Stone".

➢ The author of the book is "J.K. Rowling".

➢ The genre of the book is "Fantasy".

➢ The published year of the book is "1997".

XML Naming Rules

1. Element Names:

- Must start with a letter, underscore (`_`), or colon (`:`).

- Can contain letters, digits, hyphens (`-`), underscores (`_`), and periods (`.`).

- Cannot start with the letters "xml" (in any case), as they are reserved for XML-specific constructs.
2. Attribute Names:

- Follow the same rules as element names.

- Attributes are always associated with an element and provide additional information about the
element.

3. Namespace Prefixes:

- If namespaces are used, prefixes can be included in element and attribute names to associate
them with a specific namespace.

- Prefixes must be followed by a colon (`:`) and the local name of the element or attribute.

4. Namespace URIs:

- Namespaces are identified by URIs (Uniform Resource Identifiers) and are used to avoid naming
conflicts between elements and attributes.

- URIs are typically written in a format resembling a web URL.

5. Case Sensitivity:

- XML is case-sensitive, so "MyElement" and "myelement" would be considered different names.

- It's recommended to use consistent casing conventions for better readability and maintenance.

6. Reserved Names:

- Certain names are reserved in XML, such as "xml" (in any case) at the beginning of an element
name, which is reserved for XML-specific constructs like XML declaration and processing instructions.

Best Name Practices

• Create descriptive names, such as <person>, <firstname>, <lastname>.

• Opt for short and simple names, like <book_title>, avoiding longer alternatives like
<the_title_of_the_book>.

• Avoid special characters like "-", ".", and ":" to prevent misinterpretation by software.

• Reserve colons ":" for namespaces.

• While non-English letters like é, ò, and á are permissible, ensure compatibility with your software.

• Ensure that your naming conventions comply with the requirements of your software environment to
avoid potential issues.
Lesson 5: XML Attributes

XML elements, similar to HTML, can possess attributes.

Attributes are intended to store data associated with a particular element.

In this example:

❖ The <book> element has three attributes:

➢ id="001": This attribute provides a unique identifier for the book.

➢ category="fiction": This attribute specifies the category or genre of the book.

➢ ISBN="978-3-16-148410-0": This attribute contains the International Standard Book Number


(ISBN) of the book.

XML Elements vs. Attributes


In the initial instance, "gender" functions as an attribute, whereas in the latter case, it serves as an
element. Despite this distinction, both examples convey identical information.

XML does not impose regulations regarding the utilization of attributes or elements; their usage is
based on individual preference or specific requirements.

Both representations convey the same information, but they use different structures:

• Elements are useful when the information consists of complex data structures or when there are
multiple pieces of related information.

• Attributes are useful when the information is relatively simple and can be represented as key-value
pairs directly within the element. They can also be handy for providing metadata or attributes that
apply to the entire element.


Chapter 2: XSD (XML Schema Definition)

Lesson 1: What is XSD

An XML Schema delineates the organization of an XML document.

The XML Schema language is alternatively known as XML Schema Definition (XSD).
In this XSD example:

• We're defining an XML schema with the namespace


xmlns:xs="http://www.w3.org/2001/XMLSchema".

• We define the <book> element using the <xs:element> tag.

• Inside the <xs:complexType>, we specify the structure of the <book> element.

• We use <xs:sequence> to indicate that child elements (<title>, <author>, <genre>,


<published_year>) must appear in the specified order.

• Each child element is defined using <xs:element>, where we specify the name and data type
using type="xs:string" for text content and type="xs:integer" for the published year.

• We define an optional attribute id for the <book> element using <xs:attribute>.

The purpose of an XML Schema is to define the legal building blocks of an XML document:

• the elements and attributes that can appear in a document


• the number of (and order of) child elements
• data types for elements and attributes
• default and fixed values for elements and attributes

XML XML Schemas Support Data Types

XML Schemas offer robust support for data types, which is considered one of their key strengths.

This support facilitates several advantages:

1. Simplified description of permissible document content.


2. Enhanced validation of data accuracy.
3. Streamlined definition of data facets, including restrictions on data.
4. Facilitated definition of data patterns, encompassing data formats.
5. Streamlined conversion of data between various data types.

XML Schemas use XML Syntax

Another significant advantage of XML Schemas is their composition in XML format.

This characteristic offers several benefits:

• Eliminates the need to learn a new language.

• Enables the use of XML editors to modify Schema files.

• Facilitates parsing of Schema files using XML parsers.

• Allows manipulation of Schemas using the XML DOM.


• Permits transformation of Schemas with XSLT.

• Ensures extensibility, as XML Schemas are themselves written in XML.

With an extensible Schema definition, you can:

• Reuse your Schema in other Schema documents.

• Create custom data types derived from standard types.

• Reference multiple schemas within the same document.

Well-Formed is Not Enough

A well-formed XML document adheres to XML syntax regulations, including:

• Commencing with an XML declaration.

• Featuring a single unique root element.

• Ensuring start-tags correspond with matching end-tags.

• Maintaining case sensitivity in elements.

• Proper closure of all elements.

• Correct nesting of elements.

• Quoting of all attribute values.

• Utilization of entities for special characters.

Despite being well-formed, XML documents can still harbor errors, which may lead to significant
consequences.

Lesson 2: XSD Simple Elements

A simple element in XML is restricted to containing only text, devoid of any other elements or
attributes.

Nevertheless, the term "only text" is somewhat misleading as the text can encompass various types.
It may align with predefined types delineated in the XML Schema definition, such as boolean, string, or date,
or it could represent a custom type crafted by the user.

Furthermore, it is possible to impose restrictions, known as facets, on a data type to confine its
content or to mandate adherence to a specific pattern.
In this XSD example:

• We're defining an XML schema with the namespace


xmlns:xs="http://www.w3.org/2001/XMLSchema".

• We use the <xs:element> tag to define the element named <username>.

• The name attribute specifies the name of the element, which is "username".

• The type attribute specifies the data type allowed for this element. Here, we use xs:string to
indicate that the element can contain a string value.

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

Here is an example of XML and its XSD:

XML

Explanation:

• This XML document adheres to the structure defined in the XSD.

• It contains a <book> element with child elements <title>, <author>, and


<published_year>.

• The text content of each child element represents specific information about the book, such as
its title, author, and published year.
XSD

Explanation:

• This XSD defines a structure for a <book> element.

• The <book> element contains three child elements: <title>, <author>, and
<published_year>.

• The <title> and <author> elements are of type string (xs:string), and the
<published_year> element is of type integer (xs:integer)


Chapter 3: XSLT

Lesson 1: What is XSLT

XSL is an acronym for EXtensible Stylesheet Language.

The development of XSL was initiated by the World Wide Web Consortium (W3C) in response to the
demand for an XML-based Stylesheet Language.

XSLT, which stands for XSL Transformations, constitutes a vital aspect of XSL. Its primary function
revolves around the transformation of XML documents into alternative XML structures. Central to its
operation is the utilization of XPath, a language designed for navigating through XML documents
efficiently. By employing XPath, XSLT can accurately locate and manipulate elements within XML
documents, facilitating seamless transformation processes. Notably, XSLT has garnered recognition as a
W3C Recommendation, underscoring its importance and standardization within the XML ecosystem.
CSS vs XSL

❖ CSS
CSS, standing for Cascading Style Sheets, serves as a styling mechanism for HTML documents. HTML
relies on predefined tags, with each tag possessing a well-defined meaning and presentation style.

CSS is employed to imbue HTML elements with styles, enhancing their appearance and layout.

❖ XSL
In contrast, XSL, or eXtensible Stylesheet Language, serves as a styling tool for XML documents. XML
lacks predefined tags, leading to a less predictable interpretation of each tag's meaning.

For instance, a <table> element in XML could denote an HTML table, a furniture item, or another entity,
and web browsers are unable to ascertain how to display it.

Therefore, XSL delineates how XML elements should be presented, allowing for consistent rendering
across various platforms and applications.
Lesson 2: Correct Style Sheet Declaration

The primary element designating an XML document as an XSL style sheet is xsl:stylesheet or xsl:transform.
It's important to note that xsl:stylesheet and xsl:transform are entirely interchangeable, and either can be utilized.
According to the W3C XSLT Recommendation, the appropriate method to declare an XSL style sheet is:

Here we have a simple example of XLST:

➢ First we create a raw XML file:

XML Document (data.xml):


➢ Now we create the XSLT File:

XSLT Stylesheet (stylesheet.xsl):

➢ Lastly, Link the XSL Style Sheet to the XML Document


Lesson 3: XSLT <xsl:template> Element

An XSL style sheet comprises one or more sets of rules referred to as templates. Each template encapsulates
rules to execute when a designated node is matched.

The xsl:template element serves as a building block for constructing templates in XSLT. It employs the match
attribute to correlate a template with a specific XML element. Additionally, the match attribute can be utilized to define
a template for the entire XML document, where the value of the match attribute represents an XPath expression (for
example, match="/" designates the entire document).

Here is the example:

The example provided illustrates the structure and functionality of an XSL style sheet:

1. The XML declaration <?xml version="1.0" encoding="UTF-8"?> denotes the beginning of the XSL style sheet
document.

2. The xsl:stylesheet element signifies that the document is an XSLT style sheet, accompanied by attributes
specifying the version number and XSLT namespace.

3. Within the xsl:template element, a template is defined. The match="/" attribute links the template with the root
of the XML source document.

4. The content enclosed within the xsl:template element represents HTML markup to be generated in the output.
5. The closing tags </xsl:template> and </xsl:stylesheet> delineate the end of the template and the style sheet,
respectively.

Despite the structure defined, the outcome of this example may be unsatisfactory as it does not involve the copying of
data from the XML document to the output. Subsequent chapters will delve into the utilization of the xsl:value-of element
for selecting values from XML elements, offering more comprehensive functionality.

Lesson 4: XSLT <xsl:value-of> Element

The <xsl:value-of> element is employed to retrieve the value of a specified node.

Here is the example:

The example provided introduces the functionality of the <xsl:value-of> element:

1. The select attribute within the <xsl:value-of> element contains an XPath expression. XPath expressions function akin
to navigating a file system, where a forward slash (/) denotes the selection of subdirectories.

2. Despite the use of the <xsl:value-of> element, the outcome of the example may be deemed unsatisfactory as only
one line of data is transferred from the XML document to the output.

3. Subsequent chapters will delve into the utilization of the <xsl:for-each> element, which enables looping through XML
elements and displaying multiple records. This will enhance the functionality and scope of the transformation process.
Lesson 5: XSLT <xsl:for-each> Element

The <xsl:for-each> element facilitates looping within XSLT, enabling iterative processing of XML elements.

❖ Purpose: The <xsl:for-each> element is used to process each item in a sequence of nodes selected by an
XPath expression.

❖ Syntax: The <xsl:for-each> element has the following syntax:

➢ select: Specifies an XPath expression that selects a sequence of nodes to iterate over.

❖ Behavior:
➢ When the <xsl:for-each> element is encountered, it evaluates the XPath expression specified in the select
attribute to select a sequence of nodes from the input XML document.

➢ It then iterates over each node in the selected sequence, applying the template or performing the actions
specified within the <xsl:for-each> element for each node.

➢ During each iteration, the context node within the template or actions is set to the current node being
processed.

❖ Usage:

➢ <xsl:for-each> is commonly used when you need to process multiple nodes in a sequence and apply the
same template or perform similar actions for each node.

Lesson 6: XSLT <xsl:sort> Element

The <xsl:sort> element is utilized to arrange the output in a sorted order.

Using this example:

Sorting and Iteration:

• <xsl:for-each select="catalog/cd">: This iterates over each <cd> element within the <catalog>
element of the input XML document.

• <xsl:sort select="artist"/>: This sorts the <cd> elements based on the value of their <artist> child
elements in ascending order. This means that the CDs will be displayed alphabetically by artist.

• Inside the loop, for each <cd> element, a table row (<tr>) is created.

• Inside each row, the title and artist of the CD are displayed using <xsl:value-of select="title"/>
and <xsl:value-of select="artist"/>, respectively.
Lesson 7: XSLT <xsl:if> Element

The <xsl:if> element is employed to conduct a conditional evaluation against the content of the XML file.

To implement a conditional "if" test against the content of the XML file, incorporate an <xsl:if> element into
the XSL document.

Syntax

• <xsl:if> is a conditional element that evaluates a specified XPath expression (test) to


determine whether a condition is true or false.
• If the condition evaluates to true, the template or actions inside the <xsl:if> element are
processed.
• If the condition evaluates to false, the template or actions inside the <xsl:if> element are
skipped, and processing continues with the next instruction.
Here is an example:
• In this example, The <xsl:if> element is used to check if the price of each CD (price >
10).
• If the condition is true, the details of the CD (title, artist, and price) are displayed within a
table row (<tr>).
• If the condition is false, the CD is skipped, and processing continues with the next CD in the
loop.
This XSLT stylesheet will generate an HTML table displaying only those CDs from the catalog whose
price is greater than 10.

Lesson 8: XSLT <xsl:choose> Element

The <xsl:choose> element is employed alongside <xsl:when> and <xsl:otherwise> to articulate


multiple conditional tests.

Syntax

• <xsl:choose> is a conditional element that contains one or more <xsl:when> elements


followed by an optional <xsl:otherwise> element.
• Inside <xsl:choose>, each <xsl:when> element specifies a condition to be tested.
• The <xsl:otherwise> element is used to provide a default case if none of the conditions in
the <xsl:when> elements are true.
Here is an Example:

The provided code will assign a pink background-color to the "Artist" column when the price of the
CD exceeds 10.
References

XML Tutorial. (n.d.). https://www.w3schools.com/xml/

XML Introduction. (n.d.). https://www.w3schools.com/xml/xml_whatis.asp

XML Tree. (n.d.). https://www.w3schools.com/xml/xml_tree.asp

XML Syntax. (n.d.). https://www.w3schools.com/xml/xml_syntax.asp

XML Elements. (n.d.). https://www.w3schools.com/xml/xml_elements.asp

XML Attributes. (n.d.). https://www.w3schools.com/xml/xml_attributes.asp

XML Namespaces. (n.d.). https://www.w3schools.com/xml/xml_namespaces.asp

XML Schema Tutorial. (n.d.). https://www.w3schools.com/xml/schema_intro.asp

XML Schema How To. (n.d.). https://www.w3schools.com/xml/schema_howto.asp

XML schema Element. (n.d.). https://www.w3schools.com/xml/schema_schema.asp

XML Schema Simple Elements. (n.d.). https://www.w3schools.com/xml/schema_simple.asp

XML Schema Attributes. (n.d.). https://www.w3schools.com/xml/schema_simple_attributes.asp

XML Schema String Datatypes. (n.d.). https://www.w3schools.com/xml/schema_dtypes_string.asp

XSLT Introduction. (n.d.). https://www.w3schools.com/xml/xsl_intro.asp

XSL(T) Languages. (n.d.). https://www.w3schools.com/xml/xsl_languages.asp

XSLT Transformation. (n.d.). https://www.w3schools.com/xml/xsl_transformation.asp

XSLT <xsl:template> Element. (n.d.). https://www.w3schools.com/xml/xsl_templates.asp

XSLT <xsl:value-of> Element. (n.d.). https://www.w3schools.com/xml/xsl_value_of.asp

XSLT <xsl:for-each> Element. (n.d.). https://www.w3schools.com/xml/xsl_for_each.asp

XSLT <xsl:sort> Element. (n.d.). https://www.w3schools.com/xml/xsl_sort.asp

XSLT <xsl:if> Element. (n.d.). https://www.w3schools.com/xml/xsl_if.asp

XSLT <xsl:choose> Element. (n.d.). https://www.w3schools.com/xml/xsl_choose.asp

XML and XSLT. (n.d.). https://www.w3schools.com/xml/xml_xslt.asp


G. (2020, October 29). XML Basics. GeeksforGeeks. https://www.geeksforgeeks.org/xml-basics/

G. (2022, May 24). XML Elements. GeeksforGeeks. https://www.geeksforgeeks.org/xml-


elements/?ref=ml_lbp

G. (2023, February 22). XML Syntax. GeeksforGeeks. https://www.geeksforgeeks.org/xml-


syntax/?ref=ml_lbp

G. (2023, September 28). XSLT Syntax. GeeksforGeeks. https://www.geeksforgeeks.org/xslt-syntax/

G. (2023, October 12). XSLT foreach Element. GeeksforGeeks. https://www.geeksforgeeks.org/xslt-


foreach-element/?ref=ml_lbp

G. (2023, October 3). XSLT if Element. GeeksforGeeks. https://www.geeksforgeeks.org/xslt-if-


element/?ref=ml_lbp

G. (2023, October 3). XSLT sort Element. GeeksforGeeks. https://www.geeksforgeeks.org/xslt-sort-


element/?ref=ml_lbp

G. (2020, July 10). XSD Full Form. GeeksforGeeks. https://www.geeksforgeeks.org/xsd-full-form/

You might also like