XPath syntax on MSDN

Source: Internet
Author: User
Tags intl xsl

This topic reviews the syntax examples in the entire XPath reference. All examples are example XML files (inventory. xml) based on the XPath syntax ). For an example of using an XPath expression in the test file, see the "Union (|) Example" at the end of this topic ".

Expression Reference
./Author

All<Author>Element. Note: This expression is equivalent to the expression in the next row.

Author

All<Author>Element.

First. name

All<First. name>Element.

/Bookstore

The document elements of this document (<Bookstore>).

// Author

All<Author>Element.

Book [/bookstore/@ specialty = @ style]

StyleThe property value is equal to<Bookstore>ElementSpecialtyAll attribute values<Book>Element.

Author/first-name

Belong<Author>All child-level elements<First-name>Element.

Bookstore // title

<Bookstore>All of the more advanced or multi-level (any child) Elements<Title>Element. Note that this expression is different from the expression in the next row.

Bookstore/*/title

Belong<Bookstore>All of the grandchildren of an element<Title>Element.

Bookstore // book/excerpt // emph

<Book>Element<Excerpt>Any position and<Bookstore>All<Emph>Element.

.//title

In the current context, the deeper level or multi-level all<Title>Element. Note: in essence, only this case requires a period representation.

Author /*

Belong<Author>All the child-level elements of an element.

Book/*/last-name

Belong<Book>All of the grandchildren of an element<Last-name>Element.

*/*

All grandchildren in the current context.

* [@ Specialty]

WithSpecialtyAll elements of the attribute.

@ Style

Current contextStyleAttribute.

Price/@ exchange

Current context<Price>ElementExchangeAttribute.

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]

The current context hasStyleAll attributes<Book>Element.

Book/@ style

All of the current context<Book>ElementStyleAttribute.

@*

All attributes of the current element context.

./First-name

All<First-name>Element. Note: This expression is equivalent to the expression in the next row.

First-name

All<First-name>Element.

Author [1]

The first in the current context node<Author>Element.

Author [first-name] [3]

With<First-name>The third Sub-Level<Author>Element.

My: book

MyIn the namespace<Book>Element.

My :*

MyAll elements in the namespace.

@ My :*

MyAll attributes in the namespace (excludingMyThe undefined attributes of the elements in the namespace ).

Note that the index is relative to the parent level. Consider the following data:

<X>
<Y/>
<Y/>
</X>
<X>
<Y/>
<Y/>
</X>
Expression Reference

X/y [1]

Each<X>The first<Y>Sub-level. This expression is equivalent to the expression in the next row.

X/y [position () = 1]

Each<X>The first<Y>Sub-level.

(X/y) [1]

<X>Entire Element<Y>The first sub-level Element Set<Y>.

X [1]/y [2]

First<X>The second<Y>Sub-level.

Other examples reference the example XML file of XPath.

Expression Reference

Book [last ()]

Last of the current context node<Book>Element.

Book/author [last ()]

Each of the current context nodes<Book>Last Element<Author>Sub-level.

(Book/author) [last ()]

Current context node<Book>Entire Element<Author>The last element in the sub-level Element Set<Author>Element.

Book [excerpt]

Contains at least one<Excerpt>All child-level elements<Book>Element.

Book [excerpt]/title

Belong<Book>Element (also contains at least one<Excerpt>All<Title>Element.

book[excerpt]/author[degree]

Contains at least one<Degree>Element child level and belongs<Book>Element (also contains at least one<Excerpt>Element) all child-level<Author>Element.

Book [author/degree]

All include<Author>Sub-Level<Book>Element, which contains at least one<Degree>Sub-level.

Author [degree] [award]

Contains at least one<Degree>Element sublevel and at least one<Award>All child-level elements<Author>Element.

author[degree and award]

Contains at least one<Degree>Element sublevel and at least one<Award>All child-level elements<Author>Element.

author[(degree or award) and publication]

Contains at least one<Degree>Or<Award>And contain at least one<Publication>Sub-level all<Author>Element.

author[degree and not(publication)]

Contains at least one<Degree>The element is child-level and does not contain<Publication>All child-level elements<Author>Element.

author[not(degree or award) and publication]

Contains at least one<Publication>The element is child-level and does not contain<Degree>And<Award>All child-level elements<Author>Element.

Author [last-name = "Bob"]

Contains at least one valueBobOf<Last-name>All child-level elements<Author>Element.

Author [last-name [1] = "Bob"]

First<Last-name>The sub-element value isBobAll<Author>Element. Note: This expression is equivalent to the expression in the next row.

Author [last-name [position () = 1] = "Bob"]

First<Last-name>The sub-element value isBobAll<Author>Element.

Degree [@ from! = "Harvard"]

FromAttribute not equal"Harvard"All<Degree>Element.

Author [. = "Matthew Bob"]

All values areMatthew BobOf<Author>Element.

author[last-name = "Bob" and ../price &gt; 50]

Include value:BobOf<Last-name>Child element and value greater than 50<Price>All peer Elements<Author>Element.

Book [position () & lt; = 3]

The first three books (1, 2, 3 ).

Author [not (last-name = "Bob")]

The value isBobOf<Last-name>All sub-elements<Author>Element.

Author [first-name = "Bob"]

Contains at least one valueBobOf<First-name>All elements<Author>Element.

author[* = "Bob"]

All include any valueBobThe author element of the child element.

Author [last-name = "Bob" and first-name = "Joe"]

All include values areBobOf<Last-name>The child element and value areJoeOf<First-name>Sub-Element<Author>Element.

Price [@ intl = "Canada"]

AllIntlAttribute equals"Canada"Of<Price>Element.

Degree [position () & lt; 3]

The first two sublevels of the context node<Degree>Element.

P/text () [2]

Each<P>The second text node of the element.

Ancestor: book [1]

Closest to the context node<Book>Superior.

Ancestor: book [author] [1]

Closest to the context node<Book>And<Book>Element inclusion<Author>Child level of the element.

Ancestor: author [parent: book] [1]

Closest to the current context<Author>And<Author>Element is<Book>Child level of the element.

Union (|) Example

To demonstrate the union operation, we use the following XPath expression:

X | y/x

Select all <x> elements with green or blue values in the following XML file:

XML file (data1.xml)

<? Xml version = '1. 0'?>
<? Xml-stylesheet type = "text/xsl" href = "union. xsl"?>
<Root>
<X> green </x>
<Y>
<X> blue </x>
<X> blue </x>
</Y>
<Z>
<X> red </x>
<X> red </x>
</Z>
<X> green </x>
</Root> XSLT file (union. xsl) <? Xml version = '1. 0'?>
<Xsl: stylesheetversion = "1.0"
Xmlns: xsl = "http://www.w3.org/1999/XSL/Transform">

<Xsl: template match = "root">
<Xsl: for-each select = "x | y/x">
<Xsl: value-of select = "."/>,
<Xsl: if test = "not (position () = last ()" >,</xsl: if>
</Xsl: for-each>
</Xsl: template>

</Xsl: stylesheet> Format the output.

Green, blue, blue, green

Processor output

<? Xml version = "1.0" encoding = "UTF-16"?> Green, blue, blue, green

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.