Presentation is loading. Please wait.

Presentation is loading. Please wait.

Beginning XML 4th Edition.

Similar presentations


Presentation on theme: "Beginning XML 4th Edition."— Presentation transcript:

1 Beginning XML 4th Edition

2 Chapter 7: XPath

3 Chapter 7 Objectives Ways of looking at an XML document, including the XPath Data Model How to visualize XPath and how the component parts of XPath syntax fit together to allow you to navigate around the XPath data model The XPath axes—the “directions” that are available to navigate around the XPath data model XPath 1.0 functions XPath 2.0 new functions and features

4 Ways of Looking at an XML Document
Three Specifications: XPath DOM XML Information Set

5 XPath Data Model Root nodes Element nodes Attributes nodes
Comment nodes Processing instruction nodes Text nodes Namespace nodes

6 The Document Object Model
Nodes used in the DOM are different Hierarchical set of tree nodes Manipulation techniques are different a curious thing

7 The XML Information Set
Is a hierarchical tree that is different again Referred to as infosets Uses information items Properties Items characteristics different again

8 Visualizing XPath All legal XPath code can be called an expression.
An XPath expression that returns a node-set is called a location path. Examples

9 Context <Book> <Chapter number="1">This is the first chapter</Chapter> <Chapter number="2">This is the second chapter</Chapter> <Chapter number="3">This is the third chapter</Chapter> <Chapter number="4">This is the fourth chapter</Chapter> <Chapter number="5">This is the fifth chapter</Chapter> </Book>

10 Context <xsl:stylesheet version="1.0"
xmlns:xsl=" > <xsl:template match="/"> <html> <head> <title>This shows the context position and context size.</title> </head> <body> <h3>Context position and context size demo.</h3> <xsl:apply-templates select="/Book/Chapter" /> </body> </html> </xsl:template> <xsl:template match="Chapter"> <xsl:if test="position()=2"> <p>When the context node is the second <b>Chapter</b> element node then</p> <p>the context position is <xsl:value-of select="position()" /></p> <p>and the context size is <xsl:value-of select="last()" />.</p> <p>The text the <b> Chapter</b> element node contains is ‘<xsl:value-of select="." />‘.</p> </xsl:if> </xsl:stylesheet> Refer to the syntax to run these transforms inside the book.

11 Transformation Results

12 What is a Node? XPath has seven types of nodes: Root node Element node
Attribute node Text node Namespace node Comment node Processing Instruction node

13 Root Node <MixedContent>
Mary had a <Emphasis>little</Emphasis> lamb. </MixedContent>

14 Element Node <!DOCTYPE Book [ <!ELEMENT Book (Chapter+)>
<!ELEMENT Chapter (#PCDATA)> <!ATTLIST Chapter number ID #REQUIRED> ]> <Book> <Chapter number="c1">This is the first chapter</Chapter> <Chapter number="c2">This is the second chapter</Chapter> <Chapter number="c3">This is the third chapter</Chapter> <Chapter number="c4">This is the fourth chapter</Chapter> <Chapter number="c5">This is the fifth chapter</Chapter> </Book>

15 Attribute Node Attribute nodes have a name and a string value.

16 Text Node The string value of a text node is its character data.
A text node does not have a name.

17 Namespace Node <library:Book xmlns:library=" <chapter:Chapter xmlns:chapter=" number="1"> Some text content.</chapter:Chapter> <chapter:Chapter xmlns:chapter=" number="2"> Some different text content.</chapter:Chapter> </library:Book>

18 Comment Node You will see them but they are not represented

19 Processing Instruction Node
You will see them but they are not represented

20 XPATH 1.0 Types Boolean Node-set Number String

21 Booleans <true> ... some content </true>

22 Node-Sets <PurchaseOrder> <Date>2005-01-01</Date>
<To>XMML.com</To> <ShippingAddress> <Street>123 Any Street</Street> <City>Anytown</City> <State>AZ</State> </ShippingAddress> <ZipCode>12345</ZipCode> </PurchaseOrder>

23 Numbers Floating point No real representation for integer count( )
Example count( )

24 Strings Unicode character based Dates are just strings
Make sure that they understand that things like date are handled as strings.

25 Abbreviated and Unabbreviated Syntax
Unabbreviated and abbreivated examples <xsl:value-of select="/child::Book/child::Chapter/child::Section" /> xsl:value-of select="/Book/Chapter/Section" /> attribute::attributename @attributename XPath is not written in XML. This is for a few reasons: well-formedness.

26 XPath 1.0 Axes child axis attribute axis ancestor axis
ancestor-or-self axis descendant axis descendant-or-self axis following axis following-sibling axis namespace axis parent axis preceding axis preceding-sibling Axis self axis

27 Child Axis <Invoice> <Date>2004-01-02</Date>
<Item quantity="4">QD123</Item> <Item quantity="5">AC345</Item> </Invoice> Examples child::Item Item child::node() node() child::text() text()

28 attribute Axis Examples attribute::* @* attribute::security @security

29 Using child and attribute Axes
Try It Out Get this activity done here. Do not wait because its going to take them a while to get use to the editor. Using child and attribute Axes

30 XPath 1.0 Boolean Functions
false() lang() not() true()

31 XPath 1.0 Node-Set Functions
count() id() last() local-name() name() namespace-uri() position()

32 XPath 1.0 Numeric Funtions
ceiling() floor() number() round() sum()

33 String Functions concat() contains() normalize-space() starts-with()
string-length() substring() substring-after() substring-before() translate()

34 Example translate() <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/"> <html> <body> <xsl:apply-templates select="//text()[normalize-space(.)]"/> </body> </html> </xsl:template> <xsl:template match="text()"> <xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/> <xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/> The input <span style="background-color: #c0c0c0; border: 1px solid #000000"> <xsl:value-of select="."/></span> was translated to <span style="background-color: #ffd700; border: 1px solid #000000"> <xsl:value-of select="translate(., $lower, $upper)"/> <br/> </span> </xsl:stylesheet>

35 translate() Result

36 Predicates //Section[@security="confidential"]

37 Structure of XPath Expressions
An axis A node test An optional predicate child::Paragraph[position()=2]

38 Looking Forward to XPath 2.0
The latest version of the XPath 2.0 specification is located at Functions for XPath 2.0 are specified in a separate document located at xpath-functions/. At the time of writing, further general information on XPath 2.0 is found at XML/Query. Currently, the XPath link from describes only XPath 1.0.

39 XPath 2.0 Revised XPath Data Model W3C XML Schema Data Types
Additional XPath 2.0 Functions Xpath 2.0 Features Better String Handling Better Date and Time Handling Creating New Sequences Conditional Logic Ability to Call User-Defined Functions More Node Tests


Download ppt "Beginning XML 4th Edition."

Similar presentations


Ads by Google