zoukankan      html  css  js  c++  java
  • XPath 语法规则入门

    XPath 语法规则

    XPath使用路径表达式去确定XML文档中的节点。

    我们将利用下面的XML文档描述XPath语法

    <?xml version="1.0" encoding="GB2312"?>

    <order>
      <item catalog="parts">
        <itemNumber>C2688-67037</itemNumber>
        <description>LCD液晶显示器</description>
        <quantity>1</quantity>
        <price>358.00</price>
      </item>
        <item catalog="parts">
        <itemNumber>C2688-67061</itemNumber>
        <description>音箱</description>
        <quantity>1</quantity>
        <price>16.50</price>
      </item>
        <item catalog="parts">
        <itemNumber>C2688-67010</itemNumber>
        <description>鼠标</description>
        <quantity>1</quantity>
        <price>8.50</price>
      </item>
    </order>

    节点定位:

    XML文挡可以表示为树结构节点形式
    XPath使用模式表达式识别XML文档的节点。

    一个XPath的模式是使用反斜杠“/”分开子元素名称描述路径

    下面的XPath表达式选择元素order下元素item中的所有price元素
    /order/item/price

    注释:用“/”路径开始代表元素的绝对路径.

    不用“/”路径开始代表元素的相对路径

    item/price

    用“//”路径开始代表整个文档满足条件的所有元素

    下面的XPath表达式选择文档中所有的item元素
    //item

    选择未知元素

    通配符 “*”可用于选择未知XML元素

    下面的XPath表达式选择元素order中的所有item元素所属的子元素

    /order/item/*

    下面的XPath表达式选择元素order下所有孙子辈的price元素
    /order/*/price

    下面的XPath表达式选择所有具有两个祖先的price元素
    /*/*/price

    下面的XPath表达式选择文档所有元素
    //*

    选择分支

    使用方括号[]可以指定特定的元素

    下面的XPath表达式选择元素order中的第一个item的子元素

    /order/item[1]

    下面的XPath表达式选择元素order中的最后一个item的子元素
    /order/item[last()]

    下面的XPath表达式选择元素order中具有price元素的item元素
    /order/item[price]

    下面的XPath表达式,从元素order中选择具有price等于12.60元素的item元素
    /order/item[price=16.50]

    下面的XPath表达式,从隶属于元素order的item元素中选择具有price等于12.60元素的price元素
    /order/item[price=16.50]/price

    选择几个路径

    在XPath表达式中,使用 "|" 运算符可以选择几个路径 。实质上是逻辑“与”操作

    下面的XPath表达式,从隶属于order的item元素中选择所有itemNumber 和description 元素
    /order/item/itemNumber | /order/item/description


    下面的XPath表达式,从文档中选择所有itemNumber 和description 元素
    //itemNumber | //description

    下面的XPath表达式,从文档中选择所有itemNumber ,description和price 元素
    //itemNumber | //description | //price

    下面的XPath表达式,选取属于order中item下所有itemNumber元素和从文档中选择所有description元素
    /order/item/itemnumber | //description

    选择属性
    在XPath中,所有属性使用@前缀

    下面的XPath表达式,选取所有名为catalog的属性
    //@catalog


    下面的XPath表达式,选取所有具有catalog属性的item元素
    //item[@catalog]

    下面的XPath表达式,选取所有具有任何属性的item元素
    //item[@*]

    下面的XPath表达式,选取所有具有catalog等于"parts"属性的item元素
    //item[@catalog="parts"]


     

  • 相关阅读:
    SharePoint 2010 User Profile Sync Service自动停止
    如何区别多个svchost.exe?
    Log Parser分析IIS log的一个简单例子
    Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
    Windows中右键点击文件夹, 结果找不到共享选项卡, 怎么办?
    介绍SOS中的SaveModule命令
    SharePoint中Draft版本的文档不会收到document added的Alert Email
    和我一起学Windows Workflow Foundation(1)创建和调试一个WF实例
    门户网站
    C#基础—— check、lock、using语句归纳
  • 原文地址:https://www.cnblogs.com/eric812/p/352298.html
Copyright © 2011-2022 走看看