lecture 4
lecture 4
Lecture 4
XPath Tester :
1) http://www.freeformatter.com/xpath-tester.html
2) http://xpath.online-toolz.com/tools/xpath-editor.php
XML Example (Notebook)
<?xml version="1.0" encoding="UTF-8"?> attribute node
root element
<Notebook code="A205" color="pink">
<Processor>Intel Core i7-2710QE</Processor>
element node <Memory>DDR3L 1600</Memory>
</Notebook>
<!-- simple XML document --> comment node
XPath Tree for XML Example (Notebook)
/ root node /document node
Steven Kok
(Report)
Mary
Dara Goldman
Database Basic
David Tan
C++ Programming
Jordan Ong
Path Expression
Expression Description
/ Selects the root node (absolute path)
node name Selects all nodes with the specified node name
(relative path)
// Selects all descendent nodes of the current node
that match the selection
. Selects the current node
.. Selects the parent of the current node
@ Selects attribute nodes
• "absolute path" refers to descriptions that made from the root node
• "relative path" refers to descriptions that made relative to the current node
Path Expression
/
Example:
Java Programming
Current node: book
Steven Kok
Destination place: first-name
Mary Absolute path: /report/book/author/first-name
Lee Relative path: //author/first-name
Watty Piper
David Tan
C++ Programming
Jordan Ong
Path Expression
/
Java Programming
Question Path Expression
Steven Kok
Selects the root node /
Mary
Selects the root element node /report
Lee
Selects all book element nodes 1. /report/book
2. //book
Watty Piper
Selects all title element nodes 1. /report/book/title
2. //book/title
3. //title
Database Advanced Selects all id attribute nodes 1. /report/book/title/@id
2. //@id
Dara Goldman
Select all first-name element 1. /report/book/author/first-name
nodes 2. //author/first-name
Database Basic 3. //first-name
David Tan
C++ Programming
Jordan Ong
Brackets and Asterisk
/
A number in brackets selects a particular matching child
An asterisk - “all the elements at this level”
Java Programming
Path Expression Description Result
Steven Kok
/report/book/author[1] element of first <author>Steven Kok</author>
author of every <author>Dara Goldman</author>
Mary
book <author>David Tan</author>
Lee
//book[3]/author[2]/text() name of second Jordan Ong
Watty Piper
author of third
book element
/report/book/author/* all child of every <first-name>Mary</first-name>
Database Advanced author for every <last-name>Lee</last-name>
book
Dara Goldman
Database Basic
David Tan
C++ Programming
Jordan Ong
Attributes
You can select attributes by themselves, or elements that have
certain attributes
Remember: an attribute consists of a name-value pair, for example in
< book price = "56.70" >, the attribute is named price
To choose the attribute itself, prefix the name with @
Example: /report/book/@price will choose attribute named price
Result: price="56.70"
price="109.60"
Lee
//title[not(@id)] title element that does <title> Java Programming </title>
Watty Piper not have id attribute <title> C++ Programming </title>
Database Basic
David Tan
C++ Programming
Jordan Ong
Text() vs Node()
node() matches any node (= * or @* or text())
text() matches the text value
Text() Node()
/report/book/author/text() /report/book/author/node()
Steven Kok Steven Kok
Watty Piper <first-name> Mary </first-name>
Dara Goldman <last-name> Lee </last-name>
David Tan Watty Piper
Jordan Ong Dara Goldman
David Tan
Jordan Ong
Part 2 XSLT Part I
XSLT element use a standardized namespace to avoid conflict with other element
<?xml version="1.0"?>
<xsl:transform version=“1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
...
</xsl:transform>
Starting an XSLT stylesheet
Output element:
The output element specifies the desired output, such as
text or html.
<xsl:output method="xml" />
<xsl:output method="text" />
<xsl:output method="html" />
Example:
https://www.youtube.com/watch?v=w3WibDOie1Y
Example of XSLT Processor in Web Browser
The stylesheet is specified for an XML document using the
xml-stylesheet element.
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:text>Article - </xsl:text>
<xsl:value-of select="/Article/Title"/>
<xsl:text>
Authors: </xsl:text>
<xsl:apply-templates select="/Article/Authors/Author"/>
</xsl:template> test.xml
<?xml version="1.0"?>
<xsl:template match="Author">
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<xsl:text>
- </xsl:text>
<Article>
<xsl:value-of select="." />
<Title>Programming Article</Title>
</xsl:template>
<Authors>
</xsl:stylesheet>
<Author>Mr. John</Author>
<Author>Mr. Bar</Author>
<Author>Mr. Peter</Author>
<?xml-stylesheet type="text/xsl" href="test.xsl"?> </Authors>
<Body>This is my article text.</Body>
</Article>
Example of XSLT Processor in Web Browser cont..
Setup of the Xampp Server
Open the xampp server, press the “Start” of the Apache.
<xsl:output method="xml"/>
<xsl:template match="staff">
staff.xml
<b> <?xml version="1.0"?>
<xsl:apply-templates select="firstName"/> <record>
</b> <staff>
<b> <firstName>Alan</firstName>
<xsl:apply-templates select="lastName"/> <lastName>Tan</lastName>
</b> </staff>
</xsl:template> </record>
<xsl:template match="lastName">
<i>
<xsl:value-of select="."/> Output
</i>
</xsl:template>
<?xml version="1.0"?>
</xsl:stylesheet>
<b>Alan</b>
<b><i>Tan</i></b>
XML and XSLT Example 3
<?xml version="1.0"?> XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
xsl:apply-templates
In template that matched class, xsl:apply-templates check for
template matches on all the element of class.
The element of class in the XML document are student and teacher.
XML and XSLT Example 3
xsl:apply-templates select="student"
The select attribute of xsl:apply templates specify only student element,
indirectly remove the teacher element ("Mr. Bean").
The XSLT processor then searching templates that only match student
elements.
xsl:template match="student"
The processor finds the only template in the XSLT - prints "Found a
learner!" for each student element in the XML document.
XSLT finds three student elements, so "Found a learner!" is displayed
three times.
Template Rule
select XML element to
<xsl:template match="XPath expression"> transform
content to produce A mix of contents to produce
… further instruction + further instruction
</xsl:template>
<xsl:template match="name">
<h2 align="center"> <xsl:apply-templates/> </h2>
</xsl:template>
<xsl:template match="content">
<p align="center"> <xsl:apply-templates/> </p> hello.xml
</xsl:template> <?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl"?>
<xsl:template match="remark"> <page>
<hr/> <i><xsl:apply-templates/> </i> <hr/> <name>Welcome from FIST</name>
</xsl:template> <content>Content of the website</content>
<remark>This is the backup website</remark>
</xsl:stylesheet> </page>
XML and XSLT Example 4