ITEC-60-Module
ITEC-60-Module
ITEC60 LAB:
Integrated Programming
and Technologies 1
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.
▪ 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.
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:
• Inside <person>, there are three child elements: <name>, <age>, and <city>, each
containing specific information about a person.
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:
• 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.
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,
< 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:
❖ <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".
1. Element Names:
- 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:
- 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.
5. Case Sensitivity:
- 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.
• 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.
• 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
In this example:
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)
The XML Schema language is alternatively known as XML Schema Definition (XSD).
In this XSD example:
• 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.
The purpose of an XML Schema is to define the legal building blocks of an XML document:
XML Schemas offer robust support for data types, which is considered one of their key strengths.
Despite being well-formed, XML documents can still harbor errors, which may lead to significant
consequences.
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:
• 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
XML
Explanation:
• The text content of each child element represents specific information about the book, such as
its title, author, and published year.
XSD
Explanation:
• 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
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:
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).
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.
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.
➢ 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.
• <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
Syntax
The provided code will assign a pink background-color to the "Artist" column when the price of the
CD exceeds 10.
References