CIS 375Web App Dev II XPath XPath Introduction
CIS 375—Web App Dev II XPath
XPath Introduction n What is XPath? n n n n XPath XPath XML document is a syntax for defining parts of an _____ uses paths to define XML elements functions defines a library of standard _____ is a major element in XSLT is not written in XML Standard is a W 3 C ______ Without XPath knowledge you will not be able to create XSLT documents. Recommendation XPath was released as a W 3 C ________, November 16, 1999, as a language for addressing 2 parts of an XML document.
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> … n n n 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. nodes These “elements” are also called ____. 3
XPath Syntax n The following code makes the corresponding selection n n n n //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’ 4
XPath Location Paths n n 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] n Examples of selection n … 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 for child: : cd[attribute: : type="classic"] n . . /@src … the src attribute of the parent of the current node 5
XPath Expressions / Functions n XPath supports expressions. n n n mod Numerical: +, -, x, div, _____ Equality: =, != Relational: <, <=, >, >= Boolean: or, and XPath contains a function library for converting data. n n Node set functions: count(), last(), name(), position() String functions: concat(), string(), substring() Number functions: ceiling(), round(), sum() Boolean functions: boolean(), false(), true() 6
XPath Examples n n Selecting nodes: http: //www. w 3 schools. com/xpath/tryit. asp? filename =try_xpath_select_cdnodes Selecting nodes using criteria: http: //www. w 3 schools. com/xpath/tryit. asp? filename =try_xpath_select_pricenodes_high 7
A Final XPath Example <? xml version="1. 0" encoding="ISO-88591"? > <xsl: stylesheet version="1. 0" xmlns: xsl= "http: //www. w 3. org/1999/XSL/Transform"> <xsl: template match="/"> <html> <body> <h 2>My CD Collection</h 2> <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: #ff 0000"> <xsl: value-of select=". "/> </span> </xsl: template> <xsl: template match="artist"> Artist: <span style="color: #00 ff 00"> <xsl: value-of select=". "/> </span> </xsl: template> <xsl: template match="price"> 10% discount price: <span style="color: #0000 ff"> <xsl: value-of select= ". *. 9“/> </span> </xsl: template> </xsl: stylesheet> 8
Output From Final Example 9
- Slides: 9