zoukankan      html  css  js  c++  java
  • <转>MSDN上关于XPath的语法文章

    本主题回顾整个 XPath 参考中出现的语法示例。所有示例均基于 XPath 语法的示例 XML 文件 (inventory.xml)。有关在测试文件中使用 XPath 表达式的示例,请参见本主题最后的“联合 ( | ) 示例”。

     
    表达式引用
    ./author

    当前上下文中的所有 <author> 元素。注意,此表达式等效于下一行中的表达式。

     
    author

    当前上下文中的所有 <author> 元素。

     
    first.name

    当前上下文中的所有 <first.name> 元素。

     
    /bookstore

    本文档的文档元素 (<bookstore>)。

     
    //author

    文档中的所有 <author> 元素。

     
    book[/bookstore/@specialty = @style]

    style 属性值等于文档根目录的 <bookstore> 元素的 specialty 属性值的所有 <book> 元素。

    author/first-name

    属于 <author> 元素的子级的所有 <first-name> 元素。

     
    bookstore//title

    <bookstore> 元素中更深的一级或多级(任意子代)的所有 <title> 元素。注意,此表达式不同于下一行中的表达式。

     
    bookstore/*/title

    属于 <bookstore> 元素的孙级的所有 <title> 元素。

    bookstore//book/excerpt//emph

    <book> 元素的 <excerpt> 子级中的任意位置和 <bookstore> 元素中的任意位置的所有 <emph> 元素。

    .//title
    

    当前上下文中更深的一级或多级的所有 <title> 元素。注意,本质上只有这种情况需要句点表示法。

     
    author/*

    属于 <author> 元素的子级的所有元素。

    book/*/last-name

    属于 <book> 元素的孙级的所有 <last-name> 元素。

     
    */*

    当前上下文的所有孙级元素。

     
    *[@specialty]

    具有 specialty 属性的所有元素。

     
    @style

    当前上下文的 style 属性。

    当前上下文中 <price> 元素的 exchange 属性。

    返回空节点集,因为属性不包含元素子级。XML 路径语言 (XPath) 语法允许使用此表达式,但是严格意义上讲无效。

     
    book[@style]

    当前上下文的具有 style 属性的所有 <book> 元素。

    当前上下文的所有 <book> 元素的 style 属性。

    @*

    当前元素上下文的所有属性。

    ./first-name

    当前上下文节点中的所有 <first-name> 元素。注意,此表达式等效于下一行中的表达式。

     
    first-name

    当前上下文节点中的所有 <first-name> 元素。

    author[1]

    当前上下文节点中的第一个 <author> 元素。

    author[first-name][3]

    具有 <first-name> 子级的第三个 <author> 元素。

     
    my:book

    my 命名空间中的 <book> 元素。

    my:*

    my 命名空间中的所有元素。

     
    @my:*

    my 命名空间中的所有属性(不包括 my 命名空间中的元素的未限定属性)。

    注意,索引相对于父级。考虑以下数据:

    <x>
    <y/>
    <y/>
    </x>
    <x>
    <y/>
    <y/>
    </x>
     
    表达式引用
     
    x/y[1]

    每个 <x> 的第一个 <y> 子级。此表达式等效于下一行中的表达式。

     
    x/y[position() = 1]

    每个 <x> 的第一个 <y> 子级。

     
    (x/y)[1]

    <x> 元素的整个 <y> 子级元素集中的第一个 <y>

    x[1]/y[2]

    第一个 <x> 的第二个 <y> 子级。

    其他示例引用 XPath 的示例 XML 文件。

     
    表达式引用

    book[last()]

    当前上下文节点的最后一个 <book> 元素。

     
    book/author[last()]

    当前上下文节点的每个 <book> 元素的最后一个 <author> 子级。

     
    (book/author)[last()]

    当前上下文节点的 <book> 元素的整个 <author> 子级元素集中的最后一个 <author> 元素。

     
    book[excerpt]

    至少包含一个 <excerpt> 元素子级的所有 <book> 元素。

     
    book[excerpt]/title

    属于 <book> 元素(也至少包含一个 <excerpt> 元素子级)的子级的所有 <title> 元素。

    book[excerpt]/author[degree]
    

    至少包含一个 <degree> 元素子级并且属于 <book> 元素(也至少包含一个 <excerpt> 元素)子级的所有 <author> 元素。

    book[author/degree]

    所有包含 <author> 子级的 <book> 元素,该子级至少包含一个 <degree> 子级。

     
    author[degree][award]

    至少包含一个 <degree> 元素子级以及至少包含一个 <award> 元素子级的所有 <author> 元素。

    author[degree and award]
    

    至少包含一个 <degree> 元素子级以及至少包含一个 <award> 元素子级的所有 <author> 元素。

    author[(degree or award) and publication]
    

    至少包含一个 <degree><award> 以及至少包含一个 <publication> 子级的所有 <author> 元素。

    author[degree and not(publication)]
    

    至少包含一个 <degree> 元素子级并且不包含 <publication> 元素子级的所有 <author> 元素。

    author[not(degree or award) and publication]
    

    至少包含一个 <publication> 元素子级并且不包含 <degree><award> 元素子级的所有 <author> 元素。

    author[last-name = "Bob"]

    至少包含一个值为 Bob<last-name> 元素子级的所有 <author> 元素。

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

    第一个 <last-name> 子元素的值为 Bob 的所有 <author> 元素。注意,此表达式等效于下一行中的表达式。

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

    第一个 <last-name> 子元素的值为 Bob 的所有 <author> 元素。

     
    degree[@from != "Harvard"]

    from 属性不等于 "Harvard" 的所有 <degree> 元素。

     
    author[. = "Matthew Bob"]

    所有值为 Matthew Bob<author> 元素。

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

    包含值为 Bob<last-name> 子元素以及值大于 50 的 <price> 同辈元素的所有 <author> 元素。

     
    book[position() &lt;= 3]

    前三本书(1、2、3)。

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

    不包含值为 Bob<last-name> 子元素的所有 <author> 元素。

     
    author[first-name = "Bob"]

    至少包含一个值为 Bob<first-name> 元素的所有 <author> 元素。

    author[* = "Bob"]
    

    所有包含任何值为 Bob 的子元素的 author 元素。

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

    所有包含值为 Bob<last-name> 子元素和值为 Joe<first-name> 子元素的 <author> 元素。

     
    price[@intl = "Canada"]

    上下文节点中所有 intl 属性等于 "Canada"<price> 元素。

    degree[position() &lt; 3]

    属于上下文节点子级的前两个 <degree> 元素。

     
    p/text()[2]

    上下文节点中每个 <p> 元素的第二个文本节点。

    ancestor::book[1]

    与上下文节点最接近的 <book> 上级。

    ancestor::book[author][1]

    与上下文节点最接近的 <book> 上级,并且此 <book> 元素包含 <author> 元素子级。

     
    ancestor::author[parent::book][1]

    当前上下文中最接近的 <author> 上级,并且此 <author> 元素是 <book> 元素的子级。

    为了演示 union 运算,我们使用以下 XPath 表达式:

    x | y/x

    在以下 XML 文件中选择所有值为 green 或 blue 的 <x> 元素:

    XML 文件 (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 文件 (union.xsl)

    <?xml version='1.0'?>
    <xsl:stylesheet version="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>

    格式化输出

    green,blue,blue,green

    处理器输出

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

  • 相关阅读:
    C++ 实现B+树
    SSM项目--
    spring+mybatis使用MapperScannerConfigurer简化配置
    SpringMVC复习总结
    MyBatis复习总结
    ajax
    几种常用页面的跳转
    MyShop-不用框架的基础javaweb项目
    jsp
    Guava 工具类之joiner的使用
  • 原文地址:https://www.cnblogs.com/sunny0515/p/2350415.html
Copyright © 2011-2022 走看看