Top Banner
www.monash.edu.au CSE3201/CSE4500 XPath
39

Www.monash.edu.au CSE3201/CSE4500 XPath. 2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

Dec 28, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

CSE3201/CSE4500

XPath

Page 2: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

2

XPath

• A locator for elements or attributes in an XML document.

• XPath expression gives direction of navigation to the parser.

• Assume an XML document as a “tree”• Any part of a document, eg element, attribute, is

considered as a “node”• Current version XPATH 1.0

Page 3: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

3

XPath

• Syntax (full form):axis :: node-test [predicate]

• Axis– describing the relationship between nodes, eg child,

parents, etc.• Node test

– condition for selecting nodes.• Predicate:

– further condition refinement of the set of nodes resulted from the node test.

Page 4: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

4

XPath Axes

Ancestor

Parent/ancestor

sibling

node

child/descendant

descendantattribute

sibling

context node

Page 5: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

5

Node Test

• A node test identifies nodes in the document that meet the criteria of the test.

• The simplest type of test is nodes that match an element name.

• Example:

child::book => to find any child element with the name “book”.

child::author

Page 6: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

6

Predicate

• Predicate further refine or filter the node-set produced by the node test.

• Example:– Find the third book in the list

> child::book[position( )=3]

– Find all the books that has <isbn> element> child::book[isbn]

Page 7: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

7

Abbreviations

Formal Short Description

child::book book Select all children of the context node that has <book> element nodes.

child::* * Select all element nodes of the context node.

self::node() . Select the context node.

parent::node() .. Select the parent of the context node.

child::book[position()=1]

book[1] Select the first child element that has <book> element.

attribute::* @* select all the attributes of the context node

attribute::number @number Find the number of attributes in the context node.

Page 8: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

8

Location Path

Document Root

<name>

<first>

<middle>

“John”

“Little”

<last>

“Howard”

/name/first

Uses “/” to build path, eg

Page 9: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

9

Relative vs Absolute Path

• Absolute Path– full path needs to be included, starting from the

root node.> eg: /name/first

• Relative Path– path is declared starting from the current context

node.> eg: assume our current context is “name”, the XPath

expression for the node first => first

Page 10: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

10

Recursive Decent Operator

• Locating nodes based on their names, regardless of where their positions in the document.

• Uses “//”• Example: //first

– Select any <first> element in the document (regardless how far down the tree).

• Decrease the performance of the stylesheet.– The entire document must be searched by the XSLT

parser.

Page 11: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

11

Filtering Nodes

• It is done using XPath’s predicate.– the “[ ]” symbol.

• Using element as a filter: – book[price] matches any <book> element that

has a <price> child element.• Using attribute as a filter:

– book[@id] matches any <book> element that has an id attribute.

Page 12: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

12

XPath Expression

• Some possible operators to build an XPath Expression:

and Logical AND

or Logical OR

not() logical negation

= Equal

!= Not equal

< Less than

<= Less than equal

> Greater than

>= Greater than equal

| Union

Page 13: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

13

XPath Expression - Examples

• <xsl:template match="/">• <xsl:if test=“not(position()=last())”>

Page 14: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

14

XPath Function

• XPath functions can be used to:– manipulate node set

> eg: count, last, name, position

– manipulate string> eg: concat, substring, contains

– test boolean value> eg: language, false, true

– perform numeric operations> eg: ceiling, floor, number, round, sum

– XSLT specific manipulation> eg: current

Page 15: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

15

XPath Function - Examples

• <xsl:if test=“not(position()=last())”>• substring(‘abcde’,2,3) => returns ‘bcd’

Page 16: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

CSE3201/CSE4500 Information Retrieval Systems

XSLT

Page 17: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

17

Manipulating XML Documents

parser

data

data

data

Applications

Page 18: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

18

What is XSL

• Extensible Stylesheet Language• Developed by W3C XSL Working Group• Motivation: to handle the manipulation and

presentation of XML documents• Consists of: XSLT and XSL-FO

Page 19: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

19

XSL

Stylesheet processor

XML document

XSL document

Presentation document

Transformation process

Page 20: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

20

Transformation Tools

• XPath• XSL(Extensible Stylesheet Languages)

– XSLT(XSL Transformation)– XSL-FO(XSL Formatting Object)

Page 21: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

21

Transformation Process

Page 22: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

22

XSLT Processing

• Type of processings:– Change of vocabulary– Reorder data elements– Combine data elements– Filter and exclude data elements

• Output– Other XML vocabularies or fragments– Non-XML formats

• Uses– Display and printing– Transformation of data

Page 23: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

23

Example of a Stylesheet

<?xml version="1.0"?>

<!-- Query: List the surname of all author-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">

<html>

<body>

<h1>Monash Bookshop</h1>

<h2>Authors Surname</h2>

<xsl:apply-templates select="bookshop/book"/>

</body>

</html>

</xsl:template>

<xsl:template match="bookshop/book">

<xsl:apply-templates select="author"/>

</xsl:template>

<xsl:template match="author">

<xsl:apply-templates select="surname"/>

</xsl:template>

<xsl:template match="surname">

<p>

<xsl:value-of select="."/>

</p>

</xsl:template>

</xsl:stylesheet>

<bookshop><book><title> Harry Potter and the Sorcerer stone </title><author> <initials>J.K</initials> <surname> Rowling</surname></author><price value=“$16.95”></price></book>…</bookshop>

Page 24: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

24

Usage of XPath in XSLT

• XSLT uses XPath expression to:– Match node sets in order to execute templates.

> <xsl:template match=“/">

– Select node sets to change current context and direct the flow of the execution through the source document.

> <xsl:apply-templates select = bookshop/book/>

– Select node sets to obtain an output value> <xsl:value-of select=“."/>

Professional XML, page 379.

Page 25: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

25

Structure of Stylesheet

• An XSLT stylesheet is an XML document.• Root element is stylesheet element

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

</xsl:stylesheet>

• Consists of a set of rules.• Rules are made up of patterns and templates.

Page 26: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

26

Attaching an XSL to an XML doc

<?xml-stylesheet type="text/xsl" href="books.xsl"?>

• href refers to the filename of the XSL document.

Page 27: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

27

Selecting Output Type

• Possible outputs:– XML, HTML, Text

• Syntax:<xsl-output method=“xml”/>

<xsl-output method=“text”/>

<xsl-output method=“html”/>

Page 28: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

28

Templates

• To create a template, we need:– To declare the location in the source tree where

the template will be applied.– Rules of matching to be applied.

> can be another template

• The location is declared using the XPath expression.

Page 29: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

29

Using Templates

• Templates are executed when the condition in the <xsl:apply template> are met.

• <xsl:apply-templates  select = node-set-expression> </xsl:apply-templates>

• The “select” attribute is optional.• Without the “select” attribute, the XSL

processor will apply the templates to all the child elements of the current context node.

Page 30: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

30

Template Examples

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

version="1.0"><xsl:output method="html"/><xsl:template match="/"> <html> <body> <h1>Book</h1> <xsl:apply-templates/> </body> </html></xsl:template></xsl:stylesheet>

Page 31: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

31

Selecting Templates

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

version="1.0">

<xsl:output method="html"/>

<xsl:template match="/"> <html> <body> <h1>Monash Bookshop</h1> <xsl:apply-templates select="bookshop/book"/> </body> </html></xsl:template>

Page 32: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

32

Selecting Templates- cont’d

<xsl:template match=“bookshop/book" >

<xsl:apply-templates select="author"/>

<xsl:value-of select="@type"/>

</xsl:template>

<xsl:template match="author">

<h2>Author</h2>

<xsl:value-of select="."/>

</xsl:template>

</xsl:stylesheet>

Page 33: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

33

Getting the Value of a Node

xsl:value-of select=XPath expression

Examples:Element

<xsl:value-of select="title"/>

Attribute

<xsl:value-of select=“@type”>

Page 34: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

34

Iteration

• Combination of template and apply-templates

• <xsl:for-each>• Compared the iteration.xsl and iterationTemplate.xsl

Page 35: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

35

Conditional Test

• IF statement is used to change the tree traversing process.– Process a node differently from the rest of the nodes in the same family.– Eg. Print bold only for the first item in the list.

• xsl:if– there is no “else” statement.– takes one attribute, test, which is an XPath expression. – if it evaluates true, the body of the element is executed

• xs:choice– Test for multiple conditions

• Good practises:– Do NOT use the IF statement to select a node in a tree, use the XPATH to do that.

Page 36: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

36

Conditional Test - Example

• Example:

Give yellow colour to alternate rows <xsl:template match=“customers/customer">

<tr>

<xsl:if test="position() mod 2 = 0">

<xsl:attribute name="bgcolor">yellow</xsl:attribute>

</xsl:if>

<th>

<xsl:apply-templates select="name"/>

</th>

<xsl:apply-templates select="order"/>

</tr>

</xsl:template>

Page 37: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

37

Conditional Test - Note

XPath

<xsl:apply-template select=“customers/customer[name=‘Maria’/>

<xsl:template match=“customers/customer”>

<xsl:value-of select=“name”/>

</xsl:template>

xsl:if

<xsl:apply-template select=“customers/customer”/>

<xsl:template match=“customers/customer”><xsl:if test=“name=‘Maria’”>

<xsl:value-of select=“name”></xsl:if>

</xsl:template>

•Avoid using conditional test xsl:if to match a node. Use the XPath expression instead.

Page 38: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

38

Making Copies

• xsl:copy

– It does not copy any child nodes that the context node may have.• xsl:copy-of

– copies all

<xsl:template match="/bookshop"> <html> <body> <h1>Book</h1> <xsl:copy/> </body> </html></xsl:template>

<html><body><h1>Book</h1><bookshop></bookshop> </body> </html>

Page 39: Www.monash.edu.au CSE3201/CSE4500 XPath.  2 XPath A locator for elements or attributes in an XML document. XPath expression gives direction.

www.monash.edu.au

39

Copy-of

<?xml version="1.0" encoding="utf-8"?><Author_List> <author>

<initials>JK</initials><surname> Rowling</surname>

</author> <author>

<initials>J</initials><surname> Rowling</surname>

</author></Author_List>

<?xml version="1.0"?><xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:output method="xml"/>

<xsl:template match="/"><xsl:element name="Author_List"><xsl:apply-templates/></xsl:element></xsl:template>

<xsl:template match="bookshop/book"><xsl:copy-of select="author"/></xsl:template>

</xsl:stylesheet>