`
icz
  • 浏览: 7767 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论

XML fundenmental 1

    博客分类:
  • XML
阅读更多
Up until this point, we've been using what are called abbreviated location paths . These are easy to type, not usually verbose, and very familiar to most people. They're also the kind of XPath expression that works best for XSLT match patterns. However, XPath also offers an unabbreviated syntax for location paths, which is more verbose but perhaps less cryptic and definitely more flexible than abbreviated location paths.

Every location step in a location path has two required parts, an axis and a node test, and one optional part, the predicates. The axis tells you which direction to travel from the context node to look for the next nodes. The node test tells you which nodes to include along that axis, and the predicates further reduce the nodes according to some expression.

In an abbreviated location path, the axis and the node test are combined, while in an unabbreviated location path, they're separated by a double colon (::). For example, the abbreviated location path people/person/@id is composed of three location steps. The first step selects people element nodes along the child axis. The second step selects person element nodes along the child axis. The third step selects id attribute nodes along the attribute axis. When rewritten using the unabbreviated syntax, the same location path is child::people/child::person/attribute::id.

These full, unabbreviated location paths may be absolute if they start from the root node, just as abbreviated paths can be. The full form /child::people/child::person, for example, is equivalent to the abbreviated form /people/person.

Unabbreviated location paths may be used in predicates as well. For example, the abbreviated path /people/person[@born<1950]/name[first_name="Alan"] becomes /child::people/child::person[ attribute::born < 1950 ] /child::name[ child::first_name = "Alan" ] in the full form.

Overall, the unabbreviated form is quite verbose and not used much in practice. However, it does offer one crucial ability that makes it essential to know: it is the only way to access most of the axes from which XPath expressions can choose nodes. The abbreviated syntax lets you walk along the child, parent, self, attribute, and descendant-or-self axes. The unabbreviated syntax adds eight more axes:




ancestor

All element nodes that contain the context node, that is, the parent node, the parent's parent, the parent's parent's parent, and so on up through the root node in reverse document order.




following-sibling

All nodes that follow the context node and are children of the same parent node in document order. Attribute and namespace nodes do not have any siblings.




preceding-sibling

All nodes that precede the context node and are children of the same parent node in reverse document order.




following

All nodes that follow the end of the context node in document order except for descendant, attribute, and namespace nodes.




preceding

All nodes that precede the start of the context node in reverse document order except for ancestor, attribute, and namespace nodes.




namespace

If the context node is an element, all namespaces in scope on the context node, whether declared on the context node or one of its ancestors. If the context node is not an element, then the empty set.




descendant

All descendants of the context node but not the context node itself.




ancestor-or-self

All ancestors of the context node and the context node itself.

Example 9-4 demonstrates several of these axes using the full unabbreviated syntax. The goal is to produce a list of person elements that look more or less like this (after accounting for whitespace):

<dt>Richard P Feynman</dt>

<dd>

  <ul>

    <li>physicist</li>

    <li>Playing the bongoes</li>

  </ul>

</dd>


Example 9-4. An XSLT stylesheet that uses unabbreviated XPath syntax

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"

                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

     

  <xsl:template match="/">

    <dl>

      <xsl:apply-templates select="descendant::person"/>

    </dl>

  </xsl:template>

     

  <xsl:template match="person">

    <dt><xsl:value-of select="child::name"/></dt>

    <dd>

      <ul>

        <xsl:apply-templates select="child::name/following-sibling::*"/>

      </ul>

    </dd>

  </xsl:template>

     

  <xsl:template match="*">

    <li><xsl:value-of select="self::*"/></li>

  </xsl:template>

     

  <xsl:template match="homepage"

               xmlns:xlink="http://www.w3.org/1999/xlink">

    <li><xsl:value-of select="attribute::xlink:href"/></li>

  </xsl:template>

     

</xsl:stylesheet>



The first template rule matches the root node. It applies templates to all descendants of the root node that happen to be person elements. That is, it moves from the root node along the descendant axis with a node test of person. This XPath expression could have been rewritten in the abbreviated syntax as //person.

The second template rule matches person elements. It places the value of the name child of each person element in a dt element. The location path used here, child::name, could have been rewritten in the abbreviated syntax as the single word name. Then it applies templates to all elements that follow the name element at the same level of the hierarchy. It begins at the context node person element, then moves along the child axis to find the name element. From there it moves along the following-sibling axis looking for elements with any name (*) after the name element that are also children of the same person element. There is no abbreviated equivalent for the following-sibling axis, so this really is the simplest way to make this statement.

The third template rule matches any element not matched by another template rule. It simply wraps that element in an li element. The XPath expression self::* selects the value of the currently matched element, that is, the context node. This expression could have been abbreviated as a single period.

The fourth and final template rule matches homepage elements. In this case we need to select the value of xlink:href attributes, so we move from the context homepage node along the attribute axis. The node test is looking for the xlink:href attributes. (More properly, it's looking for an attribute with the local name href whose prefix is mapped to the http://www.w3.org/1999/xlink namespace URI.)
分享到:
评论

相关推荐

    xml2axml反编译AndroidManafest文件

    1、获取到apk 2、解压获取里面的AndroidManifest.xml文件 3、在xml2axml.jar文件目录下,打开cmd窗口,输入命令 java -jar xml2axml.jar d AndroidManifest.xml AndroidManifest-out.xml 4、在xml2axml.jar文件目录...

    tinyxml与tinyxml2

    1. **解析XML文档**:TinyXML可以将XML文件解析成内存中的DOM(Document Object Model)结构,使开发者可以遍历和修改XML元素。 2. **操作XML元素**:通过`TiXmlElement`、`TiXmlAttribute`等类,可以访问和修改元素...

    pugixml读写XML示例

    1. **初始化pugixml库**:在使用pugixml之前,需要包含头文件`#include &lt;pugixml.hpp&gt;`。 2. **加载XML文档**:使用`pugi::xml_document`的`load_file()`或`load()`函数,传入XML文件路径。例如: ```cpp pugi::...

    pugixml 一个很好用的XML类

    1. **加载XML文档**:使用`xml_document::load_file()`或`xml_document::parse()`函数加载XML文件或XML字符串。 2. **遍历XML结构**:通过`xml_node::first_child()`、`xml_node::last_child()`、`xml_node::next_...

    xml转javaBean,javaBean转xml,xml标签大小写问题,以及对xml特殊符号的处理

    1. XML转JavaBean: 在Java编程中,XML数据经常需要转换为Java对象以便于操作。这一过程通常通过XML解析器来完成,如JAXB(Java Architecture for XML Binding)或DOM4J等。JAXB提供了一种自动将XML文档映射到Java...

    andxml xml解密工具

    andxml汉化版是一个xml文件反...1、一键机器翻译会造成某些代码出现翻译错误现象,请人工识别。例如AndXml对"&lt;b&gt;..&lt;/b&gt;" 2、AndXml汉化arrays.xml会出现在汉化的文本之前加入“item|”请手动删除。另一款不会出现!

    XML解析工具- TinyXML2 -源代码 - C++

    1. 轻量级且易于集成:它体积小巧,代码简洁,易于集成到各种 C++ 项目中。 2. API简单易用:它提供了简单易用的 API,使得解析和操作 XML 数据变得简单快捷。开发者可以轻松地加载、访问和修改 XML 文档的内容。 3....

    tinyxml2解析XML文件读取数据

    1. **tinyxml2库介绍** - tinyxml2提供了DOM(Document Object Model)模型来处理XML文件,允许开发者以树形结构访问和修改XML文档。 - 库的主要类包括`XMLDocument`(XML文档对象)、`XMLElement`(XML元素)、`...

    maven的本地仓库配置文件settings.xml和项目中文件pom.xml.zip

    D:\developsoft\javaweb\commonPlugins\maven\apache-maven-3.8.1_first\conf\settings_Myeclipse1.xml 三、3个可单独使用的,maven项目文件pom.xml自定义配置 pom-maven-springboot-CusConfigV1.xml pom-maven-...

    DBMS_XMLDOM DBMS_XMLPARSER DBMS_XMLQUERY 文档

    Oracle数据库系统提供了强大的XML处理能力,这主要体现在其内置的几个PL/SQL包上,如DBMS_XMLDOM、DBMS_XMLPARSER和DBMS_XMLQUERY。这些包为开发者提供了处理XML文档的一整套工具,使得在数据库环境中进行XML数据的...

    PB解析XML字符串 把XML 变为数据窗口

    1. 加载XML字符串:首先,你需要使用PB的XML解析函数如`XMLTextToDOM`或`XMLTextToDOMEx`,将XML文本转换成DOM(Document Object Model)对象。DOM是XML文档的一种内存表示,可以方便地通过节点遍历和操作。 2. ...

    Xml完全操作 XML增删改查

    XML(eXtensible Markup Language)是一种用于标记数据的语言,其设计目的是传输和存储数据,而非显示数据。在IT行业中,XML因其结构清晰、可扩展性强的特点,被广泛应用于数据交换、配置文件、Web服务等领域。本文...

    MFC保存控件数据到xml及读取xml数据到控件

    1. **保存控件数据到XML**: - 首先,需要引入TinyXML2库,这是一套轻量级的XML解析器,能够方便地读写XML文档。 - 创建一个函数,遍历MFC应用程序中的所有控件。每个控件都有一个GetWindowText()函数来获取其当前...

    matlab读取XML,XML转换为matlab

    ### 1. XML Toolbox概述 XML Toolbox是Matlab中的一个附加组件,它提供了一系列函数和方法,帮助用户解析XML文档并将其数据转换为Matlab结构,同时也可以将Matlab的数据结构转换为XML格式。这样,Matlab用户就能利用...

    XMLParser(XML解析代码 C++版)

    1. **加载XML文件**:TinyXML能够从文件或字符串中读取XML文档,将其转化为内存中的DOM树。 2. **遍历DOM树**:解析后的XML文档被表示为一系列的元素(Element)、属性(Attribute)、文本(Text)等节点,可以使用...

    STM32解析XML

    1. **集成minixml库**:首先,将minixml库的源代码下载到项目中,根据项目需求进行编译和链接。 2. **配置内存**:由于STM32F107的内存有限,可能需要调整minixml库的内存分配策略,例如预分配一定大小的内存池来...

    Qt读写Xml文件,QTreeWidget显示Xml和导出Xml文件

    在Qt框架中,XML文件是一种常见的数据存储格式,它提供了结构化的数据表示方式,便于程序读取和写入。QTreeWidget是Qt提供的一种用于显示树状结构数据的控件,它可以很好地展示XML文件的层次结构。QXml是Qt中的XML...

    XML校验工具,可以用来进行XML合法性检验

    1. **创建DTD文件**:定义XML文档的基本结构。 2. **编写XML文档**:根据DTD文件中定义的结构编写具体的XML文档。 3. **在线校验**:将XML文档内容粘贴到W3C提供的校验工具中,选择DTD校验方式,并上传相应的DTD文件...

    Delphi下最好用的XML/XSLT组件DIXML最新版本无需密码效率超高

    1. **XML解析**:DIXML提供了对XML文档的深度解析能力。它支持XML的完整规范,包括命名空间、属性、注释、处理指令等。开发者可以方便地通过API读取和修改XML节点,如元素、属性、文本内容等。 2. **XSLT转换**:...

    c#操作XML 读取、生成,WEBSERVICE接口

    1. **C#操作XML:读取** 在C#中,我们可以使用`System.Xml`命名空间中的类来处理XML文档。其中,`XmlDocument`类是核心,用于加载和操作XML文档。例如,以下代码展示了如何读取一个XML文件: ```csharp using ...

Global site tag (gtag.js) - Google Analytics