cis 375—web app dev ii
DESCRIPTION
CIS 375—Web App Dev II. XPath. XPath Introduction. What is XPath? XPath is a syntax for defining parts of an _____ document XPath uses paths to define XML elements XPath defines a library of standard __________ XPath is a major element in XSLT XPath is not written in XML - PowerPoint PPT PresentationTRANSCRIPT
CIS 375—Web App Dev II
XPath
2
XPath Introduction What is XPath?
XPath is a syntax for defining parts of an _____ document
XPath uses paths to define XML elements XPath defines a library of standard __________ XPath is a major element in XSLT XPath is not written in XML XPath is a W3C ____________
Without XPath knowledge you will not be able to create XSLT documents.
XPath was released as a W3C _______________, November 16, 1999, as a language for addressing parts of an XML document.
XML
functions
Standard
Recommendation
3
XPath Example<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd country="USA">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price>
</cd>
…
Given the XML code above, the XPath expression /catalog/cd[price>10.80] selects all CD’s with price greater than $10.80.
/catalog/cd/title selects all title elements.
These “elements” are also called ________.nodes
4
XPath Syntax The following code makes the corresponding
selection //cd …all CD elements /catalog/cd/* … all child elements of the CD element //* … all elements in the document /catalog/cd[1] … the first CD child element /catalog/cd[last()] … the last CD child element /catalog/cd[price] … all CD elements with a price /catalog/cd[price=10.90]/price … all price
elements with a price of $10.90 /catalog/cd/title | /catalog/cd/artist … all
title and artist elements //cd[@country='UK'] … all CD’s where country=‘UK’
5
XPath Location Paths An absolute location path starts with a slash ( /
) and a relative location path does not. The syntax for a location step is: axisname::nodetest[predicate]
Examples of selection child::cd … all CD elements that are children of the
current node (child: can be omitted from a location step)
cd[position()=1] … first CD child of the current node cd[@type="classic"] is short forchild::cd[attribute::type="classic"]
../@src … the src attribute of the parent of the current node
6
XPath Expressions / Functions
XPath supports expressions. Numerical: +, -, x, div, _____ Equality: =, != Relational: <, <=, >, >= Boolean: or, and
XPath contains a function library for converting data. Node set functions: count(), last(), name(),
position() String functions: concat(), string(), substring() Number functions: ceiling(), round(), sum() Boolean functions: boolean(), false(), true()
mod
7
XPath Examples Selecting nodes:
http://www.w3schools.com/xpath/tryit.asp?filename=try_xpath_select_cdnodes
Selecting nodes using criteria: http://www.w3schools.com/xpath/tryit.asp?filename=try_xpath_select_pricenodes_high
8
A Final XPath Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"><html><body><h2>My CD Collection</h2> <xsl:apply-templates/></body></html></xsl:template>
<xsl:template match="cd"><p><xsl:apply-templates
select="title | artist | price“ /> </p></xsl:template>
<xsl:template match="title">Title: <span style="color:#ff0000"><xsl:value-of select="."/></span><br /></xsl:template>
<xsl:template match="artist">Artist: <span style="color:#00ff00"><xsl:value-of select="."/></span><br /></xsl:template>
<xsl:template match="price">10% discount price:<span style="color:#0000ff"><xsl:value-of select= ".*.9“/></span><br /></xsl:template>
</xsl:stylesheet>
9
Output From Final Example