Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of XPath Author: Dan McCreary Date: October, 2008 Version: 0.2 with TEI Examples M D.

Similar presentations


Presentation on theme: "Overview of XPath Author: Dan McCreary Date: October, 2008 Version: 0.2 with TEI Examples M D."— Presentation transcript:

1 Overview of XPath Author: Dan McCreary Date: October, 2008 Version: 0.2 with TEI Examples M D

2 M D Copyright 2008 Dan McCreary & Associates2 Objectives Provide a short overview of XPath Describe why knowledge of XPath is important for: –Business analysts –Project managers –Report developers –Programmers Demonstrate XPath using TEI document

3 M D Copyright 2008 Dan McCreary & Associates3 Definition XPath XPath: "A language for addressing parts of an XML document" Similar to a DOS or UNIX "file system path" but with powerful expressions XPath is to XML what the SQL "select" statement is to SQL XPath is not a full programming language or a query language.

4 M D Copyright 2008 Dan McCreary & Associates4 XPath Related Standards XSLT – XPath is used to tell XSLT how to match tags XLink – similar to HTML links but more powerful XPointer - a standard manner for identifying document fragments XQuery – a newer, more comprehensive standard that includes XPath 2.0 and allows more complex searches and data types include relational database searches. Includes clauses for "for, let, where, order-by, return"

5 M D Copyright 2008 Dan McCreary & Associates5 Versions Version 1.0 –W3C Recommendation November, 16 1999 –http://www.w3.org/TR/xpath Version 2.0 –W3C Working Draft October, 29 2004 –http://www.w3.org/TR/xpath20/

6 M D Copyright 2008 Dan McCreary & Associates6 Other Familiar Path Names DOS: –C:\Program Files\Altova\XMLSPY2004\Examples\Tutorial Web –http://www.google.com/search?hl=en&lr=lang_en&&q=XPath Unix –/usr/local/lib/mylib/myprogram.jar Similarities –Absolute path starts with "/" –Relative paths express do not start with "/"

7 M D Copyright 2008 Dan McCreary & Associates7 XPath Has Relative Expressions self child parent ancestor sibling descendant sibling precedingfollowing ancestor following-sibling child descendant preceding-sibling

8

9 M D Copyright 2008 Dan McCreary & Associates9 Location Steps An XPath expression contains one or more "location steps", separated by slashes. Each location step has the following unabbreviated form: –axis-name :: node-test [predicate]* The abbreviated form is more commonly used, so that is what this example uses. It refers to the child axis: –node-test [predicate]*

10 M D Copyright 2008 Dan McCreary & Associates10 XPath Also Provides XPath has functions for manipulation of: –Nodes and sets of nodes (elements-NameTest, KindTest) –Strings (substring, contains etc.) –Numbers (position etc.) –Booleans (and/or/not etc.)

11 M D Copyright 2008 Dan McCreary & Associates11 Disclaimer Example was chosen for teaching purposes only Not necessarily indicative of a full actual TEI structure

12 M D Copyright 2008 Dan McCreary & Associates12 oXygen XPath Builder Perspective > Show View > XPath Builder

13 M D Copyright 2008 Dan McCreary & Associates13 Sample XML Document Foreign Relations of the United States, 1969-1976, Volume E-13: Documents on China, 1969-1972 Foreign Relations of the United States 1969-1976 Volume E-13 Documents on China, 1969- 1972 Steven E. Phillips Edward C. Keefer

14 M D Copyright 2008 Dan McCreary & Associates14 TEI Tree text TEI namespace teiHeader front body div type="compilation" headpb div type=“document" pb div type=“document"

15 M D Copyright 2008 Dan McCreary & Associates15 90/10 rule for XPath 90% of the time your XPath expressions will be very simple. They will be just getting subsets of all the data in the document They will use the default “child” selector

16 M D Copyright 2008 Dan McCreary & Associates16 Sample XPath Child Expressions Find the root element of any document –/ Find the root element –/TEI Find the TEI Header –/TEI/teiHeader Find the text body –/TEI/text/body Find the text body headers –/TEI/text/body/div/head Find the text body sub headers –/TEI/text/body/div/head/div/head

17 M D Copyright 2008 Dan McCreary & Associates17 Selecting Attributes To select an attribute you must add the @ symbol to the expression Example: select only the divs that have an attribute type="document" –//div[@type="document"]

18 M D Copyright 2008 Dan McCreary & Associates18 Getting Data with // Get all the person names in the document –//persName Get all the dates or notes in the document –//date Get all the notes of type summary //note[@type='summary']

19 M D Copyright 2008 Dan McCreary & Associates19 Sample Counts Count of the number of paragraphs in the document –count(//p) Count of the number of divs –count(//div) Count of the number of person names –count(//persName)

20 M D Copyright 2008 Dan McCreary & Associates20 What’s the Context? XPath evaluates expressions relative to a context. –Usually specified by one of the technologies that extend XPath, such as XSLT and XPointer. –Includes context node, context size, context position, etc. In XSLT, the context node is the current node being evaluated.

21 M D Copyright 2008 Dan McCreary & Associates21 Sample XPath Syntax Test for equality –node=‘value’ [ ] – predicate, e.g. First in sequence: [1] Last: last() * - wildcards @ - attributes // - all children of the context node. – context node itself.. – parent of the context node

22 M D Copyright 2008 Dan McCreary & Associates22 XPath String Operations concat starts-with ends-with contains substring string-length substring-before substring-after normalize-space normalize-unicode upper-case lower-case translate string-pad matches replace tokenize escape-uri

23 M D Copyright 2008 Dan McCreary & Associates23 Boolean Operators boolean(1 and 1) returns true (1 and 0) returns false (1 or 0) returns true (1 and 1 and (1 or 0)) returns true

24 M D Copyright 2008 Dan McCreary & Associates24 Contains() Returns true if the first string passed contains the second string passed Example: –//p[contains(.,'President')]

25 M D Copyright 2008 Dan McCreary & Associates25 Benefits of XPath Business analysts –Precise specification of constraints, support for graphical representation of rules, easy to change. Project managers –Shorten development cycle, easier to change business logic. Report developers –Pattern matching language for routing of documents based on rules, platform and language neutral. Programmers –Functions available for manipulating nodes, sets of nodes, strings, numbers, or boolean. Based on regular expressions and mathematical model. Good for mapping and transforming documents from one type to another.

26 M D Copyright 2008 Dan McCreary & Associates26 To Find Out More XPath 2.0 Programmer's Reference –by Michael Kay Definitive XSLT and XPath –by G. Ken Holman XSLT and XPath –John Robert Gardner and Zarella L. Rendon

27 M D Copyright 2008 Dan McCreary & Associates27 Thank You! Please contact me for more information: XML Development Metadata Management Services Web Services Service Oriented Architectures Business Intelligence and Data Warehouse Metadata Registries Semantic Web Dan McCreary, President Dan McCreary & Associates Metadata Strategy Development dan@danmccreary.com (952) 931-9198


Download ppt "Overview of XPath Author: Dan McCreary Date: October, 2008 Version: 0.2 with TEI Examples M D."

Similar presentations


Ads by Google