Professional Documents
Culture Documents
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
<book price="109.60">
<title id = "234"> Database Advanced</title>
<author> Dara Goldman</author>
</book>
<book>
<title id = "236"> Database Basic</title>
<author> David Tan</author>
<title> C++ Programming </title>
<author> Jordan Ong </author>
</book> Back
/
Steven Kok
(Report)
Mary
The individual elements of Lee
an XPath tree structure are
Watty Piper
referred to as nodes.
The root node is /
The root element node is report Database Advanced
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
M
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
Dara Goldman
Absolute path: /report/book/title/@id
Relative path: //title/@id
Database Basic
David Tan
C++ Programming
Jordan Ong
M
Path Expression
/
Java Programming
Question Path Expression
Steven Kok Selects the root node /
Selects the root element node /report
Mary
Selects all book element nodes 1. /report/book
Lee
2. //book
Watty Piper Selects all title element nodes 1. /report/book/title
2. //book/title
3. //title
Selects all id attribute nodes 1. /report/book/title/@id
Database Advanced
2. //@id
Dara Goldman Select all first-name element 1. /report/book/author/first-name
nodes 2. //author/first-name
3. //first-name
Database Basic
David Tan
C++ Programming
Jordan Ong
M
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
/*/*/*/last-name element of every <last-name>Lee</last-name>
last-name that has
Database Basic
three ancestors
David Tan
//* every element in Entire XML document
the entire
C++ Programming document
Jordan Ong
Path Function
Command Description
node() It returns node value.
text() It returns text value of specific node.
comment() It returns specific comment node.
last() It returns the last matching child node
position() It returns the element node at the specified
position of the specified node type.
M
Path Function Example
/
Mary
//book/author[last()-1] element of second <author>
Lee
last author of every <first-name>Mary</first-name>
book <last-name>Lee</last-name>
Watty Piper </author>
<author> David Tan</author>
//title[position()=2] element of second <title> C++ Programming </title>
or title of every book
Database Advanced //title[2]
Dara Goldman //title[2]/text() name of second title C++ Programming
of every book
Database Basic
David Tan
C++ Programming
Jordan Ong
M
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"
<book price="109.60">
Watty Piper
<title id="234"> Database Advanced</title>
<author> Dara Goldman</author>
</book>
Jordan Ong
M
Attributes Example 2
/
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
M
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
<?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