Common XPath syntax

Source: Internet
Author: User
Tags intl xsl

XPath example

This topic reviews the syntax examples in the entire XPath reference. All examples are based on the example XML file of xpath syntax appended to this section.

Expression reference
./Author
All <author> elements in the current context. Note: This expression is equivalent to the expression in the next row.
Author
All <author> elements in the current context.
First. Name
All <first. Name> elements in the current context.
/Bookstore
The document elements of this document (<bookstore> ).
// Author
All <author> elements in the document.
Book [/bookstore/@ specialty = @ style]
The style attribute value is equal to all <book> elements in the <bookstore> element's specialty attribute value in the root directory of the document.
Author/first-name
All <first-name> elements belonging to the child level of the <author> element.
Bookstore // Title
<Bookstore> all <title> elements of a deeper level or multi-level (any child) element. Note that this expression is different from the expression in the next row.
Bookstore/*/Title
All <title> elements of the <bookstore> element.
Bookstore // book/excerpt // emph
<Book> any position in the <excerpt> sub-level of the element and all <emph> elements in any position in the <bookstore> element.
. // Title
All <title> elements in the current context with a deeper level or level. Note: in essence, only this case requires a period representation.
Author /*
All child-level elements of the <author> element.
Book/*/last-name
All <last-name> elements belonging to the grandchildren of the <book> element.
*/*
All grandchildren in the current context.
* [@ Specialty]
All elements with the specialty attribute.
@ Style
The style attribute of the current context.
Price/@ exchange
The exchange attribute of the <price> element in the current context.
Price/@ exchange/Total
An empty node set is returned because the attribute does not contain the element sublevel. The XML Path Language (XPath) syntax allows this expression, but it is strictly invalid.
Book [@ style]
All <book> elements in the current context with the style attribute.
Book/@ Style
The style attribute of all <book> elements in the current context.
@*
All attributes of the current element context.
./First-name
All <first-name> elements in the current context node. Note: This expression is equivalent to the expression in the next row.
First-name
All <first-name> elements in the current context node.
Author [1]
The first <author> element in the current context node.
Author [first-name] [3]
Has the <first-name> child level third <author> element.
My: Book
The <book> element in my namespace.
My :*
All elements in my namespace.
@ My :*
All attributes in my namespace (excluding the undefined attributes of elements in my namespace ).
 
Note that the index is relative to the parent level. Consider the following data:
XML Code
<X>
<Y/>
<Y/>
</X>
<X>
<Y/>
<Y/>
</X>
 
Expression reference
X/y [1]
The first <Y> sub-level of each <x>. This expression is equivalent to the expression in the next row.
X/y [position () = 1]
The first <Y> sub-level of each <x>.
(X/y) [1]
<X> the entire <Y> first <Y> in the sub-level element set.
X [1]/Y [2]
The second <Y> sub-level of the first <x>.
 
For other examples, refer to "example XML file of xpath Syntax".
Expression reference
Book [last ()]
The last <book> element of the current context node.
Book/author [last ()]
The last <author> sub-level of each <book> element of the current context node.
(Book/author) [last ()]
The last <author> element in the <book> element of the current context node's <author> sub-level element set.
Book [excerpt]
Contains at least one <excerpt> sub-level <book> element.
Book [excerpt]/Title
All <title> elements belonging to the <book> element (or at least one <excerpt> element sub-level.
Book [excerpt]/author [degree]
Contains at least one <degree> element sub-level and all <author> elements belonging to the <book> element (or at least one <excerpt> element) sub-level.
Book [author/degree]
All <book> elements that contain <author> sublevels. This sublevel contains at least one <degree> sublevel.
Author [degree] [award]
Contains at least one <degree> element sub-level and at least one <award> element sub-level <author> element.
Author [degree and award]
Contains at least one <degree> element sub-level and at least one <award> element sub-level <author> element.
Author [(degree or award) and publication]
Contains at least one <degree> or <award> and at least one <publication> sub-level <author> element.
Author [degree and not (publication)]
Contains at least one <degree> element sub-level and does not contain all <author> elements of <publication> element sub-level.
Author [not (degree or award) and publication]
It must contain at least one <publication> element sub-level and not all <author> elements of <degree> and <award> element sub-level.
Author [last-name = "Bob"]
Contains at least one <last-name> sub-level <author> element whose value is Bob.
Author [last-name [1] = "Bob"]
The value of the first <last-name> sub-element is all <author> elements of Bob. Note: This expression is equivalent to the expression in the next row.
Author [last-name [position () = 1] = "Bob"]
The value of the first <last-name> sub-element is all <author> elements of Bob.
Degree [@ from! = "Harvard"]
The from attribute is not equal to all <degree> elements of "Harvard.
Author [. = "Matthew Bob"]
All <author> elements whose values are Matthew Bob.
Author [last-name = "Bob" and.../price & gt; 50]
Contains <last-name> sub-elements with a value of Bob and all <author> elements with a value greater than 50 <price> peer elements.
Book [position () & lt; = 3]
The first three books (1, 2, 3 ).
Author [not (last-name = "Bob")]
It does not contain all <author> elements of the <last-name> sub-element with a value of Bob.
Author [first-name = "Bob"]
Contains at least one <first-name> <author> element with the value of Bob.
Author [* = "Bob"]
All author elements that contain any child element whose value is Bob.
Author [last-name = "Bob" and first-name = "Joe"]
All <author> elements that contain <last-name> child elements and <first-name> child elements whose values are Bob and Joe.
Price [@ Intl = "Canada"]
All Intl attributes in the context node are equal to the <price> element of "Canada.
Degree [position () <3]
The first two <degree> elements belonging to the sublevel of the context node.
P/text () [2]
The second text node of each <p> element in the context node.
Ancestor: Book [1]
The <book> parent closest to the context node.
Ancestor: Book [author] [1]
The <book> parent closest to the context node, and the <book> element contains the <author> element sub-level.
Ancestor: Author [Parent: Book] [1]
The closest <author> parent in the current context, and the <author> element is the child level of the <book> element.
 
 
Appendix: XPath syntax example XML file
XML Code
<? XML version = "1.0"?>
<? XML-stylesheet type = "text/XSL" href = "myfile. XSL"?>
<Bookstore specialty = "novel">
<Book style = "autobiography">
<Author>
<First-name> Joe </first-name>
<Last-name> Bob </last-name>
<Award> Trenton literary review honorable mention </award>
</Author>
<Price> 12 </price>
</Book>
<Book style = "textbook">
<Author>
<First-name> Mary </first-name>
<Last-name> Bob </last-name>
<Publication>
Selected Short Stories
<First-name> Mary </first-name>
<Last-name> Bob </last-name>
</Publication>
</Author>
<Editor>
<First-name> Britney </first-name>
<Last-name> Bob </last-name>
</Editor>
<Price> 55 </price>
</Book>
<Magazine style = "glossy" frequency = "Monthly">
<Price> 2.50 </price>
<Subscribe price = "24" Per = "year"/>
</Magazine>
<Book style = "novel" id = "myfave">
<Author>
<First-name> Toni </first-name>
<Last-name> Bob </last-name>
<Degree from = "Trenton U"> B. A. </degree>
<Degree from = "Harvard"> PH. D. </degree>
<Award> Pulitzer </award>
<Publication> still in Trenton </publication>
<Publication> Trenton forever </publication>
</Author>
<Price Intl = "Canada" exchange = "0.7"> 6.50 </price>
<Excerpt>
<P> It was a dark and stormy night. </P>
<P>
But then all nights in Trenton seem dark and stormy
Someone who has gone through what
<Emph> I </emph>
Have.
</P>
<Definition-List>
<Term> Trenton </term>
<Definition> misery </definition>
</Definition-List>
</Excerpt>
</Book>
<My: Book xmlns: My = "URI: mynamespace" style = "leather" Price = "29.50">
<My: title> who's who in Trenton </My: title>
<My: Author> Robert Bob </My: Author>
</My: Book>
</Bookstore>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.