转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 电脑应用 >> 电脑技术 >> 正文
XPath Tutorial-from w3schools.com         ★★★

XPath Tutorial-from w3schools.com

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3419 更新时间:2006/7/27
picture

XPath is a set of syntax rules for defining parts of an XML document.

XPath is a major element in the W3C XSLT standard. Without XPath knowledge you will not be able to create XSLT documents.

Start learning XPath!

XPath Introduction

Previous Next

XPath is a set of syntax rules for defining parts of an XML document.


What is XPath?

  • XPath is a syntax for defining parts of an XML document
  • XPath uses paths to define XML elements
  • XPath defines a library of standard functions
  • XPath is a major element in XSLT
  • XPath is not written in XML
  • XPath is a W3C Standard

Like Traditional File Paths

XPath uses path expressions to identify nodes in an XML document. These path expressions look very much like the expressions you see when you work with a computer file system:

w3schools/xpath/default.asp


XPath Example

Look at this simple XML document:

<?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>
  <cd country="UK">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>9.90</price>
  </cd>
  <cd country="USA">
    <title>Greatest Hits</title> 
    <artist>Dolly Parton</artist> 
    <price>9.90</price> 
  </cd>
</catalog>

The XPath expression below selects the ROOT element catalog:

/catalog

The XPath expression below selects all the cd elements of the catalog element:

/catalog/cd

The XPath expression below selects all the price elements of all the cd elements of the catalog element:

/catalog/cd/price

Note: If the path starts with a slash ( / ) it represents an absolute path to an element!


XPath Defines a Library of Standard Functions

XPath defines a library of standard functions for working with strings, numbers and Boolean expressions.

The XPath expression below selects all the cd elements that have a price element with a value larger than 10.80:

/catalog/cd[price>10.80]



XPath is Used in XSLT

XPath is a major element of the XSLT standard. Without XPath knowledge you will not be able to create XSLT documents.

You can read more about XSLT in our XSLT tutorial.


XPath is a W3C Standard

XPath was released as a W3C Recommendation 16. November 1999 as a language for addressing parts of an XML document.

XPath was designed to be used by XSLT, XPointer and other XML parsing software.

You can read more about XML and XSL standards in our W3C tutorial.


XPath Syntax

Previous Next

XPath uses path expressions to locate nodes within XML documents.


XML Example Document

We will use this simple XML document to describe the XPath syntax:

<?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>
  <cd country="UK">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>9.90</price>
  </cd>
  <cd country="USA">
    <title>Greatest Hits</title> 
    <artist>Dolly Parton</artist> 
    <price>9.90</price> 
  </cd>
</catalog>


Locating Nodes

XML documents can be represented as a tree view of nodes (very similar to the tree view of folders you can see on your computer).

XPath uses a pattern expression to identify nodes in an XML document. An XPath pattern is a slash-separated list of child element names that describe a path through the XML document. The pattern "selects" elements that match the path.

The following XPath expression selects all the price elements of all the cd elements of the catalog element:

/catalog/cd/price

Note: If the path starts with a slash ( / ) it represents an absolute path to an element!

Note: If the path starts with two slashes ( // ) then all elements in the document that fulfill the criteria will be selected (even if they are at different levels in the XML tree)!

The following XPath expression selects all the cd elements in the document:

//cd



Selecting Unknown Elements

Wildcards ( * ) can be used to select unknown XML elements.

The following XPath expression selects all the child elements of all the cd elements of the catalog element:

/catalog/cd/*

The following XPath expression selects all the price elements that are grandchild elements of the catalog element:

/catalog/*/price

The following XPath expression selects all price elements which have 2 ancestors:

/*/*/price

The following XPath expression selects all elements in the document:

//*



Selecting Branches

By using square brackets in an XPath expression you can specify an element further.

The following XPath expression selects the first cd child element of the catalog element:

/catalog/cd[1]

The following XPath expression selects the last cd child element of the catalog element (Note: There is no function named first()):

/catalog/cd[last()]

The following XPath expression selects all the cd elements of the catalog element that have a price element:

/catalog/cd[price]

The following XPath expression selects all the cd elements of the catalog element that have a price element with a value of 10.90:

/catalog/cd[price=10.90]

The following XPath expression selects all the price elements of all the cd elements of the catalog element that have a price element with a value of 10.90:

/catalog/cd[price=10.90]/price



Selecting Several Paths

By using the | operator in an XPath expression you can select several paths.

The following XPath expression selects all the title and artist elements of the cd element of the catalog element:

/catalog/cd/title | /catalog/cd/artist

The following XPath expression selects all the title and artist elements in the document:

//title | //artist

The following XPath expression selects all the title, artist and price elements in the document:

//title | //artist | //price

The following XPath expression selects all the title elements of the cd element of the catalog element, and all the artist elements in the document:

/catalog/cd/title | //artist



Selecting Attributes

In XPath all attributes are speci

[1] [2] [3] [4]  下一页


[系统软件]给IE增加dom3xpath支持  [VB.NET程序]VBCOM TUTORIAL(3)
[VB.NET程序]VBCOM TUTORIAL(2)  [VB.NET程序]VBCOM TUTORIAL(1)
[VB.NET程序]Microsoft Agent Tutorial Chapter 2  [VB.NET程序]Microsoft Agent Tutorial Chapter 1
[Web开发]XSLT轻松入门第四章:XPath的语法  [Web开发]XPath 2.0 的新特性
[Web开发]JDOM/XPATH编程指南  [Web开发].NET中的xpath
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · 电脑技术  · 操作系统
    · 磁盘工具  · 视音频技术
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台