33
XSLT and XPath, by Dr. Kh alil 1 XSL, XSLT and XPath Dr. Awad Khalil Dr. Awad Khalil Computer Science Department Computer Science Department AUC AUC

XSL, XSLT and XPath

  • Upload
    jafari

  • View
    85

  • Download
    5

Embed Size (px)

DESCRIPTION

XSL, XSLT and XPath. Dr. Awad Khalil Computer Science Department AUC. Content. What XSL and XSLT are How XSLT differs from imperative programming languages, like JavaScript What templates are, and how they are used How to address the innards of an XML document using XPath. What is XSL?. - PowerPoint PPT Presentation

Citation preview

Page 1: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 1

XSL, XSLT and XPath

Dr. Awad KhalilDr. Awad Khalil

Computer Science DepartmentComputer Science Department

AUCAUC

Page 2: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 2

Content

What XSL and XSLT areWhat XSL and XSLT are

How XSLT differs from imperative How XSLT differs from imperative programming languages, like JavaScriptprogramming languages, like JavaScript

What templates are, and how they are usedWhat templates are, and how they are used

How to address the innards of an XML How to address the innards of an XML document using XPathdocument using XPath

Page 3: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 3

What is XSL? Extensible Style sheet Language, as the name implies, is , as the name implies, is

an XML-based language to create an XML-based language to create style sheets. The successor to Extensible Stylesheet Language (XSL), The successor to Extensible Stylesheet Language (XSL),

XSL Transformations (XSLT) is an XML-based language is an XML-based language that enables you to transform one class of XML document to that enables you to transform one class of XML document to another. another.

XSLT offers tremendous flexibility for presenting and XSLT offers tremendous flexibility for presenting and exchanging data between disparate devices and business exchanging data between disparate devices and business systems. For example, with XSLT style sheets, you can systems. For example, with XSLT style sheets, you can dynamically transform an XML purchase order from one dynamically transform an XML purchase order from one schema to another before sending the order to a supplier.schema to another before sending the order to a supplier.

In addition, with XSLT you can dynamically transform an In addition, with XSLT you can dynamically transform an XML document so it can be rendered on a variety of XML document so it can be rendered on a variety of Internet-enabled devices, including handheld PCs and TV Internet-enabled devices, including handheld PCs and TV set-top boxes. set-top boxes.

XSLT is a powerful tool for e-commerce, as well as any other place where XML might be used.

Page 4: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 4

XSLT Engine

An XSL engine uses style sheets to transform XML An XSL engine uses style sheets to transform XML documents into other document types, and to format the documents into other document types, and to format the output.output.

In these style sheets you define the layout of the output In these style sheets you define the layout of the output document, and where to get the data from within the document, and where to get the data from within the input document.input document.

In XML, the input document is called the In XML, the input document is called the source tree, , and the output document the and the output document the result tree..

There are actually two complete languages under the XSL There are actually two complete languages under the XSL umbrella:umbrella: A transformation language, which is namedA transformation language, which is named XSLT XSLT A language used to describe XML documents for displayA language used to describe XML documents for display, ,

XSL Formatting ObjectsXSL Formatting Objects

Page 5: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 5

How XSLT Works?

XSLT transforms XML documents into whatever other XSLT transforms XML documents into whatever other format – HTML for display on web sites, a different format – HTML for display on web sites, a different XML-based structure for other applications (such as XML-based structure for other applications (such as storing data in a back-end databases), or even just storing data in a back-end databases), or even just regular text files. regular text files.

XSLT relies on finding parts of an XML document that XSLT relies on finding parts of an XML document that match a series of predefined templates, and then match a series of predefined templates, and then applying transformation and formatting rules to each applying transformation and formatting rules to each matched part.matched part.

XPath – another language – is used to help finding and XPath – another language – is used to help finding and querying these matching parts.querying these matching parts.

Page 6: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 6

What Do You Need?

In order to perform an XSLT transformation, you In order to perform an XSLT transformation, you need at least three things: an need at least three things: an XML document to to transform, an transform, an XSLT style sheet, and an , and an XSLT engine.

The style sheet contains the instructions for the The style sheet contains the instructions for the transformation you want to perform.transformation you want to perform.

The engine is the software which will carry out the The engine is the software which will carry out the instructions in the style sheetinstructions in the style sheet

Page 7: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 7

XSLT Engines

XT It’s a Win32 executable

program that you can download from:

http://jclark.com/xml/xml.html

You can run XT from the DOS command prompt, using the general syntax:

C:\>XT source stylesheet result

MSXML It’s XML parser that ships with

IE 5 and also includes an XSLT engine. You can download the latest preview from:

http://msdn.microsoft.com/downloads/webtechnology/xml/msxml.asp

Page 8: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 8

How Do XSLT Style Sheets Work? XSLT style sheets are built on structures called XSLT style sheets are built on structures called templatestemplates.. A A templatetemplate specifies what to look for in the source tree, and what to put into specifies what to look for in the source tree, and what to put into

the result tree.the result tree. XSLT is written in XML, meaning that there are special XSLT elements and XSLT is written in XML, meaning that there are special XSLT elements and

attributes you use to create your style sheets:attributes you use to create your style sheets:<xsl:template match=“first”>First tag found!</xsl:template><xsl:template match=“first”>First tag found!</xsl:template> Templates are defined using the XSLT <xsl:template> element. There are two Templates are defined using the XSLT <xsl:template> element. There are two

important pieces to this template: the important pieces to this template: the matchmatch attribute, and the attribute, and the contentscontents of the of the template.template.

The The matchmatch attribute specifies a pattern in the source tree; this template will be attribute specifies a pattern in the source tree; this template will be applied for any nodes in the source tree that match that pattern – the element applied for any nodes in the source tree that match that pattern – the element named named firstfirst, in this case. If this template was included in a style sheet, every , in this case. If this template was included in a style sheet, every time the XSLT engine fount a <time the XSLT engine fount a <firstfirst> element in the source tree, it would > element in the source tree, it would output the text “output the text “FirstFirst tagtag found!found!”.”.

The The contentscontents of a template can be much more complex than simply of a template can be much more complex than simply outputting text. The element outputting text. The element <xsl:value-of><xsl:value-of> takes information from the takes information from the source tree, and adds it to the result tree, for example:source tree, and adds it to the result tree, for example:

<xsl:template match=“first”><xsl:value-of select=“.’ /></xsl:template><xsl:template match=“first”><xsl:value-of select=“.’ /></xsl:template>

Page 9: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 9

Associating Style Sheets Using PIs

An XSL style sheet can be associated with an An XSL style sheet can be associated with an XML document using a style sheet processing XML document using a style sheet processing instruction:instruction:

<?xml-stylesheet type=“text/xsl” href=“stylesheet.xsl”?><?xml-stylesheet type=“text/xsl” href=“stylesheet.xsl”?>

Page 10: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 10

A Simple E-Commerce Application

Assume there are two companies Assume there are two companies working together, sharing working together, sharing information over the Internet. information over the Internet. CompanyCompany AA is a store, which is a store, which sends purchase orders to sends purchase orders to Company Company BB, who fulfills those orders. , who fulfills those orders. CompanyCompany AA needs information on needs information on the salesperson who made the order the salesperson who made the order for commissioning purposes, but for commissioning purposes, but Company BCompany B doesn’t need it, but doesn’t need it, but instead needs to know the part instead needs to know the part numbers, which numbers, which Company ACompany A’s ’s order doesn’t really care about.order doesn’t really care about.

Company A<?xml version=“1.0” ?><?xml version=“1.0” ?><order><order> <salesperson>John Doe</salesperson><salesperson>John Doe</salesperson> <item>Production-Class Widget</item><item>Production-Class Widget</item> <quantity>16</quantity><quantity>16</quantity> <date><date> <month>1</month><month>1</month> <day>13</day><day>13</day> <year>2000</year><year>2000</year> </date></date> <customer>Sally Finkelstien</customer><customer>Sally Finkelstien</customer></order></order>

Company B<?xml version=“1.0” ?><?xml version=“1.0” ?><order><order> <date>2000/1/13</date><date>2000/1/13</date> <customer>Company A</customer><customer>Company A</customer> <item><item> <part-number>E16-25A</part-number><part-number>E16-25A</part-number> <description> Production-Class Widget <description> Production-Class Widget

</description></description> <quantity>16</quantity><quantity>16</quantity> </item></item></order></order>

Page 11: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 11

The E-Commerce Application – Different Scenarios

Company A can use the same structure for their XML can use the same structure for their XML that that Company B uses. The disadvantage is that they uses. The disadvantage is that they now a separate XML document to accompany the first now a separate XML document to accompany the first one, for their own additional information.one, for their own additional information.

Company B can use the same structure for their XML can use the same structure for their XML that that Company A uses. uses.

Both companies can use whatever XML format they Both companies can use whatever XML format they wish internally, but transform their data to a common wish internally, but transform their data to a common format whenever they need to transmit the information format whenever they need to transmit the information outsideoutside

Page 12: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 12

1- Create Company A’s data (CompanyA.xml)

<?xml version=“1.0” ?><order> <salesperson>John Doe</salesperson> <item>Production-Class Widget</item> <quantity>16</quantity> <date> <month>1</month> <day>13</day> <year>2000</year> </date> <customer>Sally Finkelstien</customer></order>

Page 13: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 13

2- Create the XSLT to contain the instructions for transformation (order.xsl)

<?xml version="1.0"?><?xml version="1.0"?><xsl:stylesheet version="1.0" <xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Tranxmlns:xsl="http://www.w3.org/1999/XSL/Transform">sform">

<xsl:output method="xml" indent="yes"/><xsl:output method="xml" indent="yes"/>

<xsl:template match="/"><xsl:template match="/"> <order><order> <date><date> <xsl:value-of <xsl:value-of

select="/order/date/year"/>/<xsl:value-of select="/order/date/year"/>/<xsl:value-of select="/order/date/month"/>/<xsl:value-of select="/order/date/month"/>/<xsl:value-of

select="/order/date/day"/>select="/order/date/day"/> </date></date> <customer>Company A</customer><customer>Company A</customer> <item><item> <xsl:apply-templates select="/order/item"/><xsl:apply-templates select="/order/item"/> <quantity><xsl:value-of <quantity><xsl:value-of

select="/order/quantity"/></quantity>select="/order/quantity"/></quantity> </item></item> </order></order></xsl:template></xsl:template>

<xsl:template match="item"><xsl:template match="item">

<part-number><part-number>

<xsl:choose><xsl:choose>

<xsl:when test=". = 'Production-Class <xsl:when test=". = 'Production-Class Widget'">E16-25A</xsl:when>Widget'">E16-25A</xsl:when>

<xsl:when test=". = 'Economy-Class <xsl:when test=". = 'Economy-Class Widget'">E16-25B</xsl:when>Widget'">E16-25B</xsl:when>

<!--other part-numbers would go here--><!--other part-numbers would go here-->

<xsl:otherwise>00</xsl:otherwise><xsl:otherwise>00</xsl:otherwise>

</xsl:choose></xsl:choose>

</part-number></part-number>

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

</xsl:template></xsl:template>

</xsl:stylesheet></xsl:stylesheet>

Page 14: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 14

3- Perform the transformation (using XT engine)

C:\>XT CompanyA.xml order.xsl CompanyB.xsl

The output of this transformation will be Company B’s data:

<?xml version=“1.0” encoding=“utf-8” ?><order> <date>2000/1/13</date> <customer>Company A</customer> <quantity>16</quantity> <item> <part-number>E16-25A</part-number> <description> Production-Class Widget </description> <quantity>16</quantity> </item></order>

Page 15: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 15

XPathXPath is a whole separate specification from is a whole separate specification from

W3C which is used for addressing and pointing W3C which is used for addressing and pointing to sections of an XML document, to allow to get to sections of an XML document, to allow to get the exact pieces of information we need.the exact pieces of information we need.

XSL uses uses XPath extensively. extensively.

We use We use XPath expressions to address XML to address XML documents by specifying a documents by specifying a location path..

XPath needs to know a XPath needs to know a context node; that is, the ; that is, the section of the XML document from XPath section of the XML document from XPath should start navigation through the document. should start navigation through the document.

Page 16: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 16

Node? What’s a Node?

XPath uses the term XPath uses the term node to refer to any part of a to refer to any part of a document, whether it be element, attribute, or document, whether it be element, attribute, or otherwise.otherwise.

Node-sets are collections of nodes. Node-sets can are collections of nodes. Node-sets can contain any type of node.contain any type of node.

for example, if you tell XPath to look for any elements for example, if you tell XPath to look for any elements with an with an id attribute, XPath will return a node-set, which attribute, XPath will return a node-set, which will be a collection of all the elements in the source tree will be a collection of all the elements in the source tree that have an that have an id attribute. attribute.

Page 17: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 17

Location Paths The first thing to note about The first thing to note about

XPath is the concept of the XPath is the concept of the document root, which is not the , which is not the root element.

Since there can be other things at Since there can be other things at the beginning or end of an XML the beginning or end of an XML document before or after the root document before or after the root element, hence, the element, hence, the document root acts as a virtual root of the acts as a virtual root of the document’s hierarchy.document’s hierarchy.

Consider the <name> example Consider the <name> example which has the following XML:which has the following XML:

<name> <first>John</first> <middle>Fitzgerald

Johanson</middle> <last>Doe></last></name>

Page 18: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 18

Location Paths (Cont’d) In XPath, the document root is specified by a single “In XPath, the document root is specified by a single “/” ”

as: as: <xsl:template match=“/”> for which XSLT applies for which XSLT applies to the entire document.to the entire document.

If we only wanted to match against the <order> element If we only wanted to match against the <order> element in the order.xml file, instead of the entire document, we in the order.xml file, instead of the entire document, we could write the XPath expression “ could write the XPath expression “ /order”.”.

XPath reads expressions from left to right, so in this case XPath reads expressions from left to right, so in this case it reads “start at the document node, and return the it reads “start at the document node, and return the <<order> element which is a child of that node.> element which is a child of that node.

XPath expressions can be read backward as “select any XPath expressions can be read backward as “select any elements named <order> which are children of the elements named <order> which are children of the document root”.document root”.

Page 19: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 19

Location Paths (Cont’d) XPath expressions can be relative, meaning that they start at the XPath expressions can be relative, meaning that they start at the

context node, or absolute, meaning that they start at the document context node, or absolute, meaning that they start at the document root.root.

Additional steps in instructions to XPath are separated by Additional steps in instructions to XPath are separated by additional “/” characters, for example, additional “/” characters, for example, “/order/item/part-number” which means “select any elements named ” which means “select any elements named <part-number> which are children of which are children of <item> elements, which are elements, which are children of children of <order> elements, which are children of the elements, which are children of the document root.document root.

XPath also allows using the XPath also allows using the “@” symbol, for example, symbol, for example, “@id” ” means “select the id attribute of the context node”.means “select the id attribute of the context node”.

The “The “//” is called the recursive descent operator which locates ” is called the recursive descent operator which locates nodes based on their names, regardless of their locations in the nodes based on their names, regardless of their locations in the document, for example, document, for example, “//customer” selects any <” selects any <customer> > element in the document, and element in the document, and “//@id” selects any id attribute in selects any id attribute in the document.the document.

Page 20: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 20

More Specific Location Paths ““[ ]” brackets are used as a filter on the node being selected, for ” brackets are used as a filter on the node being selected, for

example:example: “order [customer]” matches anymatches any <order> element which is a element which is a

child of the context node and which has achild of the context node and which has a <customer> child child element.element.

“order [@number]” matches anymatches any <order> element which is a element which is a child of the context node and which has achild of the context node and which has a number attributeattribute..

“order[customer=‘Company A’]” matches anymatches any <order> element which is a child of the context node, and which has aelement which is a child of the context node, and which has a <customer> child with a value of “Company A”.child with a value of “Company A”.

““customer [. = ‘Company A”] matches any matches any <customer> element with a value of “Company A”.element with a value of “Company A”.

“//order [customer/@id=‘216A’] says select any says select any <order> elements which have a child element named elements which have a child element named <customer>, which in turn has an which in turn has an id attribute with a value of “216A”. attribute with a value of “216A”.

Page 21: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 21

XPath Functions Node Functions – are used – are used

for working with nodes in general. for working with nodes in general. They can return information about a They can return information about a node, or return specific types of node, or return specific types of nodes.nodes.

name() – used to get the name of a – used to get the name of a node, for example, the following node, for example, the following template will output the name of template will output the name of every element that’s child of a every element that’s child of a <student> element:<student> element:

<xsl:template match=“student”> <xsl:for-each select=“*”> <p><xsl:value-of

select=“name()” /> </p> </xsl:for-each></xsl:template>

node() – returns the node itself. – returns the node itself.

Processing-instruction() – – returns processing instructions. For returns processing instructions. For example, the following template returns example, the following template returns the contents of any processing the contents of any processing instructions in the document:instructions in the document:

<xsl:template match=“processing-instruction()”>

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

</xsl-template> And this one outputs the contents of any And this one outputs the contents of any

PIs in the document with a PITarget of PIs in the document with a PITarget of AppName:AppName:

<xsl:template match=“processing-instruction(‘AppName’)”>

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

</xsl-template>

Page 22: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 22

Node Functions (Cont’d)

comment() – used to return – used to return comments. for example, the following comments. for example, the following template will output the text in any template will output the text in any comment:comment:

<xsl:template match=“comment()”>

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

</xsl-template>

text() – returns the PCDATA content of – returns the PCDATA content of a node, without the PCDATA of the a node, without the PCDATA of the node’s children, if any. For example, if node’s children, if any. For example, if we consider the following XML:we consider the following XML:

<parent>This is some text. <child>And this is some more

text</child></parent>The following template:The following template:<xsl:template match=“parent”><xsl:value-of select=“.” /></xsl-template>returns: returns: This is some text. And this some more textBut, the following template:But, the following template:<xsl:template match=“parent”><xsl:value-of select=“text()” /></xsl-template>returns: returns: This is some text.

Page 23: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 23

Positional Functions

position() – is used to get the node’s – is used to get the node’s position in a node-set, in document-order. position in a node-set, in document-order. For example, if we consider the following For example, if we consider the following XML:XML:

<nodes> <node>a</node> <node>b</node> <node>c</node></nodes>

The template:The template:<xsl:template match=“/nodes/node”>will match any will match any <node> element. element.

The template:The template:<xsl:template

match=“/nodes/node[position()=2]”>will match the second will match the second <node> element. element.or: or: xsl:template match=“/nodes/node[2]”>

last() – returns the position of the last – returns the position of the last node in a node-set. For example, to node in a node-set. For example, to create a template that matches against create a template that matches against the last <node> element, we would do the last <node> element, we would do the following:the following:

<xsl:template match=“/nodes/node[position() = last()]”>

count() – returns the number of – returns the number of nodes in a node-set. For example, to get nodes in a node-set. For example, to get the number of <node> elements, we the number of <node> elements, we would do the following:would do the following:

<xsl:value-of select=“count(node)” />

Page 24: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 24

Numeric Functions

number() – converts PCDATA – converts PCDATA text to a numeric value. For example, text to a numeric value. For example, if we have an element like this:if we have an element like this:

<element>256</element>

According to XPath, this element According to XPath, this element contains a string containing the contains a string containing the characters “2”, “5”, and “6”. In order characters “2”, “5”, and “6”. In order to to treat the PCDATA as a numeric to to treat the PCDATA as a numeric value of 256, we would have to use value of 256, we would have to use the function the function number(element).

sum() – is used to add together all of the – is used to add together all of the numeric values in a node-set. For example, numeric values in a node-set. For example, if we have the following XML:if we have the following XML:

<nodes>

<node>1</node>

<node>5</node>

<node>3</node>

</nodes>

then:then:

<xsl:value-of select=“sum(/nodes/node)” />

returns the sum of all the returns the sum of all the <node> elements. elements.

Page 25: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 25

Boolean Functions

boolean() – simply evaluates an – simply evaluates an XPath expression to be XPath expression to be truetrue or or falsefalse, , using the following rules:using the following rules:

If the value is numeric, it’s If the value is numeric, it’s considered considered falsefalse if it is 0, or the if it is 0, or the special NaN value, otherwise special NaN value, otherwise consideredconsidered true true..

If the value is a string, it isIf the value is a string, it is true true if its length is longer than 0 if its length is longer than 0 characters.characters.

If the value is a node-set, it is If the value is a node-set, it is true true it it’s not empty, otherwise it it’s not empty, otherwise it’s it’s falsefalse..

Any other type of object is Any other type of object is converted to a Boolean in a way converted to a Boolean in a way which is dependent on the type which is dependent on the type of object.of object.

not() – simply takes whatever the result – simply takes whatever the result and reverses it.and reverses it.

true() – always returns – always returns truetrue..

false() – always returns – always returns falsefalse..

<xsl:if test=“first[.=‘John’]”> <!-- some HTML here …--></xsl:if>

<xsl:if test=“true()”> <!-- some HTML here …--></xsl:if>

<xsl:if test=“false()”> <!-- some HTML here …--></xsl:if>

Page 26: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 26

String Functions

string() string-length() concat() contains() starts-with() substring() substring-after() substring-before() translate()

concat(‘mystring’, ‘ ‘, ‘more string’) contains(“This is s string”, “is a”) contains(“This is s string”, “is A”) starts-with(“This is a string”, “This”) starts-with(“This is a string”, “a string”) substring(“This is the main string”, 13) substring(“This is the main string”, 13, 4) substring-after(‘This is the main string’, ‘a’) substring-before(‘This is the main string’, ‘a’) translate(‘QLSM’, ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’,

‘ZYXWVUTSRQPONMLKJIHGFEDCBA’) translate(‘John’, ‘abcdefghijklmnopqrstuvwxyz’,

‘ZYXWVUTSRQPONMLKJIHGFEDCBA’) translate(‘This is a string’, ‘ ‘, ‘+’) translate(‘+bat+’, ‘abc+ ‘, ‘ABC’) <xsl:valueof select = “translate(. ,

‘abcdefghijklmnopqrstuvwxyz’, ‘ZYXWVUTSRQPONMLKJIHGFEDCBA’)” />

Page 27: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 27

Axis Names

In XML, there are numerous directions In XML, there are numerous directions we can travel, in addition to just moving we can travel, in addition to just moving up and down between parents and up and down between parents and children.children.

The way that we move in these other The way that we move in these other directions is through different directions is through different axes..

There are 13 axes defined in XPath, There are 13 axes defined in XPath, which we use in XPath by specifying the which we use in XPath by specifying the axis name, followed by ::, followed by the axis name, followed by ::, followed by the node name.node name.

From our node, we have access to parents From our node, we have access to parents and ancestors, children and descendents, and ancestors, children and descendents, and siblings.and siblings.

If the node is an element, we have also If the node is an element, we have also access to attributes that are attached to access to attributes that are attached to that node.that node.

The last section of the tree, we have The last section of the tree, we have access to the namespace.access to the namespace.

Page 28: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 28

Axis Names (Cont’d) The simplest is the The simplest is the self axis, which refers axis, which refers

to the context node. The to the context node. The “.” notation is a notation is a shortcut for shortcut for self::node().

The The child axis is the default axis and axis is the default axis and contains the children of the context node. contains the children of the context node. “order” gives exactly the same meaning as gives exactly the same meaning as “child::order”.

There are also wildcards available for child There are also wildcards available for child elements and child attributes: elements and child attributes: “*” selects selects any child elements, and any child elements, and “@*” selects any selects any child attributes:child attributes:

<xsl:template match=“*”> <xsl:template match=“@*”> <xsl:for-each select=“*”> TheThe descendant axis specifiesaxis specifies any any

children, or children of children, etc. of the children, or children of children, etc. of the current node, while current node, while descendant-or-self axis contains the descendents of the context axis contains the descendents of the context node, including itself.node, including itself. “//” is the shortcut is the shortcut for for descendant-or-self, relative to the , relative to the document root. document root. “.//” is the is the descendant-or-self axis of the context node. So, axis of the context node. So, “.//description” means the same as means the same as ““descendant-or-self::description”.

Page 29: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 29

Axis Names (Cont’d) TheThe parent axis specifies the parent of axis specifies the parent of

the context node (if any), and the the context node (if any), and the ancestor axis specifies the parent of axis specifies the parent of the context node, or the parent’s parent, the context node, or the parent’s parent, etc. The etc. The ancestor-or-self works the works the opposite way to the opposite way to the descendent-or-self.

The The following-sibling axis contains all axis contains all of the following sibling elements of the of the following sibling elements of the context node, including their context node, including their descendents (but not including descendents (but not including descendents of the context node).descendents of the context node).

The The preceding-sibling axis contains all axis contains all of the preceding elements of the node, of the preceding elements of the node, including their descendents (but not including their descendents (but not including the ancestors of the context including the ancestors of the context node).node).

The The attribute axis axis “@” is used to get at is used to get at the attributes of the context node.the attributes of the context node.

TheThe namespace axis selects the axis selects the namespace nodes of the context node, namespace nodes of the context node, if any.if any.

Page 30: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 30

An Example:

order.xml<?xml version=“1.0” ?>

<order number=“312597”>

<date>2000/1/1</date>

<customer id=“216A”>Company A</customer>

<item>

<part-number warehouse=“warehouse 11”>

E16-25A</part-number>

<description> Production-Class Widget </description>

<quantity>16</quantity>

</item>

</order>

Xpath.xsl<?xml version="1.0"?>

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

Transform">

<xsl:output method="text“ />

<xsl:template match="/">

<xsl:for-each select="//description/following-sibling::quantity">

<xsl:value-of select="name()“ />

<xsl:text>

</xsl:text>

</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

Page 31: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 31

Other XPath Expressions child::order order selects any <order> elements which are selects any <order> elements which are

children of the context node.children of the context node.

order/* selects any elements which are child selects any elements which are child of of <order>.<order>.

order/@* selects any attributes which belong to selects any attributes which belong to <order>.<order>.

descendent-or- .//description selects any <description> elements selects any <description> elements self:description which are a descendent which are a descendent

of the context node, including the context of the context node, including the context node if applicable.node if applicable.

order/attribute::number order/@number selects the number attribute of the selects the number attribute of the <order> node.<order> node.

//date/parent::order //date/../order selects any <order> elements which are a selects any <order> elements which are a

parent of a <date> element.parent of a <date> element. /order/descendent::description selects any <description> elements selects any <description> elements

which are descendents of the which are descendents of the <order> <order> elementelement

//description/following-sibling::quantity selects a <quantity> element, if that selects a <quantity> element, if that element is element is a sibling of a <description> node, and it a sibling of a <description> node, and it

comes after the <description> comes after the <description> node in the node in the document order. document order.

Page 32: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 32

Other XPath Expressions (Cont’d)

//quantity/preceding-sibling::description selects a <description> selects a <description> element, element, if that element is a sibling of if that element is a sibling of the the <quantity> node, and it <quantity> node, and it comes comes before the <quantity> node before the <quantity> node in in document order.document order.

/order/date/following::* selects any elements which selects any elements which come come after the <date> element in after the <date> element in the the document, not including document, not including

descendents.descendents.

/order/item/preceding::* selects any elements which selects any elements which come come before the <item> element, before the <item> element, not not including ancestors. including ancestors.

Page 33: XSL, XSLT and XPath

XSLT and XPath, by Dr. Khalil 33

Thank you