diff --git a/files/zh-cn/web/xpath/axes/index.html b/files/zh-cn/web/xpath/axes/index.html deleted file mode 100644 index ad31f4e051db98..00000000000000 --- a/files/zh-cn/web/xpath/axes/index.html +++ /dev/null @@ -1,68 +0,0 @@ ---- -title: Axes -slug: Web/XPath/Axes -tags: - - XPath - - XPath_Reference -translation_of: Web/XPath/Axes ---- -

{{ XsltRef() }} There are thirteen different axes in the XPath specification. An axis represents a relationship to the context node, and is used to locate nodes relative to that node on the tree. The following is an extremely brief description of the thirteen available axes and the degree of support available in Gecko.

-

For further information on using XPath expressions, please see the For Further Reading section at the end of Transforming XML with XSLT document.

-

 

-
-
- ancestor
-
- Indicates all the ancestors of the context node beginning with the parent node and traveling through to the root node.
-
- ancestor-or-self
-
- Indicates the context node and all of its ancestors, including the root node.
-
- attribute
-
- Indicates the attributes of the context node. Only elements have attributes. This axis can be abbreviated with the at sign (@).
-
- child
-
- Indicates the children of the context node. If an XPath expression does not specify an axis, is understood by default. Since only the root node or element nodes have children, any other use will select nothing.
-
- descendant
-
- Indicates all of the children of the context node, and all of their children, and so forth. Attribute and namespace nodes are not included - the parent of an attribute node is an element node, but attribute nodes are not the children of their parents.
-
- descendant-or-self
-
- Indicates the context node and all of its descendants. Attribute and namespace nodes are not included - the parent of an attribute node is an element node, but attribute nodes are not the children of their parents.
-
- following
-
- Indicates all the nodes that appear after the context node, except any descendant, attribute, and namespace nodes.
-
- following-sibling
-
- Indicates all the nodes that have the same parent as the context node and appear after the context node in the source document.
-
- namespace - - (not supported) -
-
- Indicates all the nodes that are in scope for the context node. In this case, the context node must be an element node.
-
- parent
-
- Indicates the single node that is the parent of the context node. It can be abbreviated as two periods (..).
-
- preceding
-
- Indicates all the nodes that precede the context node in the document except any ancestor, attribute and namespace nodes.
-
- preceding-sibling
-
- Indicates all the nodes that have the same parent as the context node and appear before the context node in the source document.
-
- self
-
- Indicates the context node itself. It can be abbreviated as a single period (.).
-
diff --git a/files/zh-cn/web/xpath/axes/index.md b/files/zh-cn/web/xpath/axes/index.md new file mode 100644 index 00000000000000..a401cd509468c8 --- /dev/null +++ b/files/zh-cn/web/xpath/axes/index.md @@ -0,0 +1,39 @@ +--- +title: Axes +slug: Web/XPath/Axes +tags: + - XPath + - XPath_Reference +translation_of: Web/XPath/Axes +--- +{{ XsltRef() }} There are thirteen different axes in the [XPath](cn/XPath) specification. An axis represents a relationship to the context node, and is used to locate nodes relative to that node on the tree. The following is an extremely brief description of the thirteen available axes and the degree of support available in [Gecko](cn/Gecko). + +For further information on using XPath expressions, please see the [For Further Reading](cn/Transforming_XML_with_XSLT/For_Further_Reading) section at the end of [Transforming XML with XSLT](cn/Transforming_XML_with_XSLT) document. + +- [ancestor](cn/XPath/Axes/ancestor) + - : Indicates all the ancestors of the context node beginning with the parent node and traveling through to the root node. +- [ancestor-or-self](cn/XPath/Axes/ancestor-or-self) + - : Indicates the context node and all of its ancestors, including the root node. +- [attribute](cn/XPath/Axes/attribute) + - : Indicates the attributes of the context node. Only elements have attributes. This axis can be abbreviated with the at sign (`@`). +- [child](cn/XPath/Axes/child) + - : Indicates the children of the context node. If an XPath expression does not specify an axis, is understood by default. Since only the root node or element nodes have children, any other use will select nothing. +- [descendant](cn/XPath/Axes/descendant) + - : Indicates all of the children of the context node, and all of their children, and so forth. Attribute and namespace nodes are **not** included - the `parent` of an `attribute` node is an element node, but `attribute` nodes are not the children of their parents. +- [descendant-or-self](cn/XPath/Axes/descendant-or-self) + - : Indicates the context node and all of its descendants. Attribute and namespace nodes are **not** included - the `parent` of an `attribute` node is an element node, but `attribute` nodes are not the children of their parents. +- [following](cn/XPath/Axes/following) + - : Indicates all the nodes that appear after the context node, except any `descendant`, `attribute`, and `namespace` nodes. +- [following-sibling](cn/XPath/Axes/following-sibling) + - : Indicates all the nodes that have the same parent as the context node and appear after the context node in the source document. +- [namespace](cn/XPath/Axes/namespace) + _(not supported)_ + - : Indicates all the nodes that are in scope for the context node. In this case, the context node must be an element node. +- [parent](cn/XPath/Axes/parent) + - : Indicates the single node that is the parent of the context node. It can be abbreviated as two periods (`..`). +- [preceding](cn/XPath/Axes/preceding) + - : Indicates all the nodes that precede the context node in the document except any `ancestor`, `attribute` and `namespace` nodes. +- [preceding-sibling](cn/XPath/Axes/preceding-sibling) + - : Indicates all the nodes that have the same parent as the context node and appear before the context node in the source document. +- [self](cn/XPath/Axes/self) + - : Indicates the context node itself. It can be abbreviated as a single period (`.`). diff --git a/files/zh-cn/web/xpath/comparison_with_css_selectors/index.html b/files/zh-cn/web/xpath/comparison_with_css_selectors/index.html deleted file mode 100644 index dcdb90c4d52f33..00000000000000 --- a/files/zh-cn/web/xpath/comparison_with_css_selectors/index.html +++ /dev/null @@ -1,44 +0,0 @@ ---- -title: Comparison of CSS Selectors and XPath -slug: Web/XPath/Comparison_with_CSS_selectors -translation_of: Web/XPath/Comparison_with_CSS_selectors -original_slug: Web/CSS/CSS_Selectors/Comparison_with_XPath ---- -
{{CSSRef("Selectors")}}{{QuickLinksWithSubpages("/en-US/docs/Web/XPath")}}
- -

本文旨在记录 CSS 选择器和 XPath 之间的区别,以便 Web 开发人员能够更好地为正确的工作选择合适的工具。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
XPath featureCSS equivalent
ancestor, parent or preceding-sibling axis{{CSSxRef(":has",":has()")}} selector {{experimental_inline}}
attribute axisAttribute selectors
child axisChild combinator
descendant axisDescendant combinator
following-sibling axisGeneral sibling combinator or adjacent sibling combinator
self axis{{CSSxRef(":scope")}} or {{CSSxRef(":host")}} selector
diff --git a/files/zh-cn/web/xpath/comparison_with_css_selectors/index.md b/files/zh-cn/web/xpath/comparison_with_css_selectors/index.md new file mode 100644 index 00000000000000..e06b4e77c8b5e4 --- /dev/null +++ b/files/zh-cn/web/xpath/comparison_with_css_selectors/index.md @@ -0,0 +1,18 @@ +--- +title: Comparison of CSS Selectors and XPath +slug: Web/XPath/Comparison_with_CSS_selectors +translation_of: Web/XPath/Comparison_with_CSS_selectors +original_slug: Web/CSS/CSS_Selectors/Comparison_with_XPath +--- +{{CSSRef("Selectors")}}{{QuickLinksWithSubpages("/en-US/docs/Web/XPath")}} + +本文旨在记录 CSS 选择器和 XPath 之间的区别,以便 Web 开发人员能够更好地为正确的工作选择合适的工具。 + +| [XPath feature](/en-US/docs/Web/XPath) | [CSS equivalent](/en-US/docs/Web/CSS/CSS_Selectors) | +| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| [`ancestor`](/en-US/docs/Web/XPath/Axes/ancestor), [`parent`](/en-US/docs/Web/XPath/Axes/parent) or [`preceding-sibling`](/en-US/docs/Web/XPath/Axes/preceding-sibling) axis | {{CSSxRef(":has",":has()")}} selector {{experimental_inline}} | +| [`attribute`](/en-US/docs/Web/XPath/Axes/ancestor) axis | [Attribute selectors](/en-US/docs/Web/CSS/Attribute_selectors) | +| [`child`](/en-US/docs/Web/XPath/Axes/child) axis | [Child combinator](/en-US/docs/Web/CSS/Child_selectors) | +| [`descendant`](/en-US/docs/Web/XPath/Axes/descendant) axis | [Descendant combinator](/en-US/docs/Web/CSS/Descendant_selectors) | +| [`following-sibling`](/en-US/docs/Web/XPath/Axes/following-sibling) axis | [General sibling combinator](/en-US/docs/Web/CSS/General_sibling_selectors) or [adjacent sibling combinator](/en-US/docs/Web/CSS/Adjacent_sibling_selectors) | +| [`self`](/en-US/docs/Web/XPath/Axes/self) axis | {{CSSxRef(":scope")}} or {{CSSxRef(":host")}} selector | diff --git a/files/zh-cn/web/xpath/index.html b/files/zh-cn/web/xpath/index.html deleted file mode 100644 index e42eb102253918..00000000000000 --- a/files/zh-cn/web/xpath/index.html +++ /dev/null @@ -1,54 +0,0 @@ ---- -title: XPath -slug: Web/XPath -tags: - - XPath -translation_of: Web/XPath ---- -

XPath 的意思是 XML 路径语言。它使用的一个非 XML 语法提供一种灵活地定位 XML 文档的不同部分的方法。它同时也可以用于检测文档中某个节点是否与某个模式(pattern)匹配。

- -

XPath 主要被用于 XSLT,也可用于定位文档元素,像类 XML 语言文档(如 HTML 和 XUL ) 通过 DOM 定位元素一样。替代 {{Domxref("document.getElementById")}} 方法、 {{Domxref("element.childNodes")}} 属性和其他 DOM 核心特性。

- -

XPath 使用路径标识符通过层级结构来导航 XML 文档。它使用非 XML 语法,以致于它可以被用在 URIs 和 XML 属性值上。

- -
-

注意:XPath 的浏览器支持差别很大;Firefox 支持地相当不错 (尽管没有计划进一步提高支持),而其他浏览器则较小程度地实现了它。如果你需要一个兼容主流浏览器的 XPath,你可以考虑试试 js-xpathwicked-good-xpath.

-
- -

文档

- -
-
在 JavaScript 中使用 XPath 的介绍
-
描述了 XPath 的一个非 XSLT 使用。
-
XPath:Axes
-
List and definition of the axes defined in the XPath specification. Axes are used to describe the relationships between nodes.
-
XPath:Functions
-
List and description of the core XPath functions and XSLT-specific additions to XPath.
-
Transforming XML with XSLT
-
XSLT uses XPath to address code segments in a XML document that it wishes to transform.
-
XPath snippets
-
JavaScript utility functions to use in your own code, based on DOM Level 3 XPath APIs.
-
What is XSLT?
-
This extensive introduction to XSLT and XPath assumes no prior knowledge of the technologies, and guides the reader through background, context, structure, concepts and introductory terminology.
-
JXON
-
JXON (lossless JavaScript XML Object Notation) is a generic name by which is defined the representation of JavaScript Objects using XML. There are some cases in which the whole content of an XML document must be read from the JavaScript interpreter (like for web-apps languages or settings XML documents, for example). In these cases JXON could represent the most practical way and a valid alternative to XPath.
-
- -

工具

- -
-
XPather
-
Feature rich XPath generator, editor, inspector, and simple extraction tool (FireFox Add-On).
-
FireXPath
-
XPath panel that integrates tightly into FireBug, providing an editor and inspector (FireFox Add-On).
-
XMLQuire (formerly known as SketchPath)
-
A Graphical XPath Builder/Debugger(.NET).
-
- - - - - -
{{QuickLinksWithSubpages("/zh-CN/docs/Web/XPath")}}
diff --git a/files/zh-cn/web/xpath/index.md b/files/zh-cn/web/xpath/index.md new file mode 100644 index 00000000000000..f75fb493898270 --- /dev/null +++ b/files/zh-cn/web/xpath/index.md @@ -0,0 +1,46 @@ +--- +title: XPath +slug: Web/XPath +tags: + - XPath +translation_of: Web/XPath +--- +XPath 的意思是 XML 路径语言。它使用的一个非 XML 语法提供一种灵活地定位 [XML](/zh-CN/docs/XML) 文档的不同部分的方法。它同时也可以用于检测文档中某个节点是否与某个模式(pattern)匹配。 + +XPath 主要被用于 [XSLT](/zh-CN/docs/XSLT),也可用于定位文档元素,像类 XML 语言文档(如 HTML 和 [XUL](/zh-CN/docs/XUL) ) 通过 [DOM](/zh-CN/docs/DOM) 定位元素一样。替代 {{Domxref("document.getElementById")}} 方法、 {{Domxref("element.childNodes")}} 属性和其他 DOM 核心特性。 + +XPath 使用路径标识符通过层级结构来导航 XML 文档。它使用非 XML 语法,以致于它可以被用在 URIs 和 XML 属性值上。 + +> **备注:** XPath 的浏览器支持差别很大;Firefox 支持地相当不错 (尽管没有计划进一步提高支持),而其他浏览器则较小程度地实现了它。如果你需要一个兼容主流浏览器的 XPath,你可以考虑试试 [js-xpath](http://nchc.dl.sourceforge.net/project/js-xpath/js-xpath/1.0.0/xpath.js) 或 [wicked-good-xpath](https://github.com/google/wicked-good-xpath). + +## 文档 + +- [在 JavaScript 中使用 XPath 的介绍](/zh-CN/docs/Introduction_to_using_XPath_in_JavaScript) + - : 描述了 XPath 的一个非 XSLT 使用。 +- [XPath:Axes](/en-US/docs/XPath/Axes) + - : List and definition of the axes defined in the XPath specification. Axes are used to describe the relationships between nodes. +- [XPath:Functions](/en-US/docs/XPath/Functions) + - : List and description of the core XPath functions and XSLT-specific additions to XPath. +- [Transforming XML with XSLT](/en-US/docs/Transforming_XML_with_XSLT) + - : XSLT uses XPath to address code segments in a XML document that it wishes to transform. +- [XPath snippets](/en-US/docs/Web/XPath/Snippets) + - : JavaScript utility functions to use in your own code, based on [DOM Level 3 XPath ](http://www.w3.org/TR/DOM-Level-3-XPath/)APIs. +- [What is XSLT?](http://www.xml.com/pub/a/2000/08/holman/) + - : This extensive introduction to XSLT and XPath assumes no prior knowledge of the technologies, and guides the reader through background, context, structure, concepts and introductory terminology. +- [JXON](/en-US/docs/JXON) + - : **JXON** (lossless **J**avaScript **X**ML **O**bject **N**otation) is a generic name by which is defined the representation of JavaScript Objects using [XML](/en/XML). There are some cases in which the whole content of an XML document must be read from the JavaScript interpreter (like for web-apps languages or settings XML documents, for example). In these cases JXON could represent the most practical way and a valid alternative to XPath. + +## 工具 + +- [XPather](https://addons.mozilla.org/zh-CN/firefox/addon/1192) + - : Feature rich XPath generator, editor, inspector, and simple extraction tool (FireFox Add-On). +- [FireXPath](https://addons.mozilla.org/zh-CN/firefox/addon/11900) + - : XPath panel that integrates tightly into FireBug, providing an editor and inspector (FireFox Add-On). +- [XMLQuire (formerly known as SketchPath)](http://qutoric.com/xmlquire/) + - : A Graphical XPath Builder/Debugger(.NET). + +## 相关主题 + +- [XSLT](/zh-CN/docs/XSLT), [XQuery](/zh-CN/docs/XQuery), [XML](/zh-CN/docs/XML), [DOM](/zh-CN/docs/DOM), [JXON](/zh-CN/docs/JXON), [JSONPath](/zh-CN/docs/JSON/JSONPath) + +{{QuickLinksWithSubpages("/zh-CN/docs/Web/XPath")}} diff --git a/files/zh-cn/web/xpath/introduction_to_using_xpath_in_javascript/index.html b/files/zh-cn/web/xpath/introduction_to_using_xpath_in_javascript/index.html deleted file mode 100644 index 80eb1e9b673ea6..00000000000000 --- a/files/zh-cn/web/xpath/introduction_to_using_xpath_in_javascript/index.html +++ /dev/null @@ -1,437 +0,0 @@ ---- -title: Introduction to using XPath in JavaScript -slug: Web/XPath/Introduction_to_using_XPath_in_JavaScript -tags: - - DOM - - Extensions - - Transforming_XML_with_XSLT - - Web Development - - XPath -translation_of: Web/XPath/Introduction_to_using_XPath_in_JavaScript -original_slug: Web/JavaScript/Introduction_to_using_XPath_in_JavaScript ---- -

该篇文档描述了如何在扩展和网站内部通过 JavaScript 调用 XPath 接口。 Mozilla 实现了相当多的 DOM 3 XPath,意味着 Xpath 表达式已经可以在 HTML 和 XML 文档中使用。

- -

使用 XPath 的主要接口是 document 对象的 evaluate 方法。

- -

document.evaluate

- -

此方法针对基于 XML 的文档(包括 HTML 文档)评估 XPath 表达式,并返回 XPathResult 对象,该对象可以是单个节点或一组节点。这个方法的现有文档位于 document.evaluate,但是对于我们现在的需求来说它相当稀疏;下面将给出更全面的研究。

- -
var xpathResult = document.evaluate( xpathExpression, contextNode, namespaceResolver, resultType, result );
-
- -

参数

- -

evaluate 函数共有五个参数:

- - - -

返回值

- -

返回 xpathResult,它是 resultType 参数中指定的类型的 XPathResult 对象。XPathResult 在这里定义。

- -

实现默认的命名空间解析器

- -

我们使用 document 对象的 createNSResolver 方法创建一个命名空间解析器。

- -
var nsResolver = document.createNSResolver( contextNode.ownerDocument == null ? contextNode.documentElement : contextNode.ownerDocument.documentElement );
-
- -

或者,也可以使用 XPathEvaluator 对象的 createNSResolver 方法。

 var xpEvaluator = new XPathEvaluator(); var nsResolver = xpEvaluator.createNSResolver( contextNode.ownerDocument == null ? contextNode.documentElement : contextNode.ownerDocument.documentElement ); 

- -

然后传递 document.evaluate,将 nsResolver 变量作为 namespaceResolver 参数。

- -

注意:XPath 定义不带前缀的 QNames,以仅匹配 null 命名空间中的元素。XPath 没有办法选择应用于常规元素引用的默认命名空间(例如,p[@id='_myid'] 对应于 xmlns='http://www.w3.org/1999/xhtml')。要匹配非命名空间中的默认元素,您必须使用如 [namespace-uri()='http://www.w3.org/1999/xhtml' and name()='p' and @id='_id']这种方法适用于命名空间未知的动态 XPath),或者使用前缀名测试,并创建一个命名空间解析器将前缀映射到命名空间。如果你想采取后一种方法,阅读更多关于如何创建一个用户定义的命名空间解析器

- -

注意

- -

适应任何 DOM 节点以解析命名空间,以便可以相对于文档中出现的节点的上下文轻松地评估 XPath 表达式。此适配器的工作方式类似于 DOM 级别 3 方法 lookupNamespaceURI 在解析 namespaceuRI 时节点的层次结构中的可用的当前信息的节点。也正确解析了隐式 xml 前缀。

- -

指定返回类型

- -

document.evaluate 返回的变量 xpathResult 可以由单个节点(简单类型)或节点集合(节点集类型)组成。

- -

简单类型

- -

resultType 中的所需结果类型指定为:

- - - -

我们通过分别访问 XPathResult 对象的以下属性来获取表达式的返回值。

- - - -
示例
- -

以下使用 XPath 表达式 count(//p) 来获取 HTML 文档中的 <p> 元素数:

- -
var paragraphCount = document.evaluate( 'count(//p)', document, null, XPathResult.ANY_TYPE, null );
-
-alert( 'This document contains ' + paragraphCount.numberValue + ' paragraph elements' );
-
- -

虽然 JavaScript 允许我们将数字转换为一个字符串进行显示,但 XPath 接口不会自动转换数字结果,如果 stringValue 属性被请求,所以下面的代码将工作:

- -
var paragraphCount = document.evaluate('count(//p)', document, null, XPathResult.ANY_TYPE, null );
-
-alert( 'This document contains ' + paragraphCount.stringValue + ' paragraph elements' );
-
- -

相反,它将返回一个带有 NS_DOM_TYPE_ERROR 的异常。

- -

节点集类型

- -

XPathResult 对象允许以 3 种主要不同类型返回节点集:

- - - -
Iterators
- -

resultType 参数中的指定结果类型为:

- - - -

返回的 XPathResult 对象是一个匹配节点的节点集,它将作为迭代器,允许我们使用 XPathResultiterateNext() 方法访问包含的各个节点。

- -

一旦迭代完成所有的匹配节点,iterateNext() 将返回 null

- -

但请注意,如果在迭代过程中,文档发生突变(文档树被修改),将使迭代无效,并且 XPathResultinvalidIteratorState 属性设置为 true,抛出 NS_ERROR_DOM_INVALID_STATE_ERR 异常。

- -
Iterator Example
- -
var iterator = document.evaluate('//phoneNumber', documentNode, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null );
-
-try {
-  var thisNode = iterator.iterateNext();
-
-  while (thisNode) {
-    alert( thisNode.textContent );
-    thisNode = iterator.iterateNext();
-  }
-}
-catch (e) {
-  dump( 'Error: Document tree modified during iteration ' + e );
-}
-
- -
Snapshots
- -

resultType 参数中的指定结果类型为:

- - - -

返回的 XPathResult 对象是一个匹配节点的静态节点集,这允许我们通过 XPathResult 对象的 snapshotItem(itemNumber) 方法访问每个节点,其中 itemNumber 是要检索的节点的索引。包含的节点总数可以通过 snapshotLength 属性访问。

- -

快照不随文档突变而改变,因此与迭代器不同,快照不会变得无效,但是它可能不对应于当前文档,例如节点可能已被移动,它可能包含不再存在的节点,或新节点可能已添加。

- -
Snapshot Example
- -
var nodesSnapshot = document.evaluate('//phoneNumber', documentNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
-
-for ( var i=0 ; i < nodesSnapshot.snapshotLength; i++ )
-{
-  dump( nodesSnapshot.snapshotItem(i).textContent );
-}
-
- -
First Node
- -

resultType 参数中的指定结果类型为:

- - - -

返回的 XPathResult 对象只是匹配 XPath 表达式的第一个找到的节点。这可以通过 XPathResult 对象的 singleNodeValue 属性访问。如果节点集为空,这将为 null

- -

请注意,对于无序子类型,返回的单个节点可能不是文档顺序中的第一个,但是对于有序子类型,保证以文档顺序获取第一个匹配的节点。

- -
First Node Example
- -
var firstPhoneNumber = document.evaluate('//phoneNumber', documentNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null );
-
-dump( 'The first phone number found is ' + firstPhoneNumber.singleNodeValue.textContent );
-
- -

ANY_TYPE 常量

- -

resultType 参数中的结果类型指定为 ANY_TYPE 时,返回的 XPathResult 对象将是由表达式求值自然产生的任何类型。

- -

它可以是任何简单类型(NUMBER_TYPESTRING_TYPEBOOLEAN_TYPE ),如果返回的结果类型是节点集,那么它将是一个 UNORDERED_NODE_ITERATOR_TYPE

- -

要在评估后确定类型,我们使用 XPathResult 对象的 resultType 属性。此属性的常量值在附录中定义。 None Yet =====Any_Type Example===== <pre> </pre>

- -

示例

- -

在 HTML 文档中

- -

以下代码旨在放置在要针对其评估 XPath 表达式的 HTML 文档中内嵌或外链的任何 JavaScript 片段中。

- -

要使用 XPath 提取 HTML 文档中的所有 <h2> 标题元素,xpathExpression 只是 //h2。其中,// 是递归下降运算符,在文档树中的任何位置将元素与 nodeName h2 相匹配。这个的完整代码是: link to introductory xpath doc

- -
var headings = document.evaluate('//h2', document, null, XPathResult.ANY_TYPE, null );
-
- -

请注意,由于 HTML 没有命名空间,因此我们为 namespaceResolver 参数传递了 null

- -

因为希望在整个文档中搜索标题,所以我们使用 document 对象本身作为 contextNode

- -

此表达式的结果是 XPathResult 对象。如果想知道返回的结果的类型,我们可以评估返回的对象的 resultType 属性。在这种情况下,这将评估为 4,即 UNORDERED_NODE_ITERATOR_TYPE。这是 XPath 表达式的结果是节点集时的默认返回类型。它一次提供对单个节点的访问,并且可能不以特定顺序返回节点。要访问返回的节点,我们使用返回对象的 iterateNext() 方法:

- -
var thisHeading = headings.iterateNext();
-
-var alertText = 'Level 2 headings in this document are:\n'
-
-while (thisHeading) {
-  alertText += thisHeading.textContent + '\n';
-  thisHeading = headings.iterateNext();
-}
-
- -

一旦迭代到一个节点,我们就可以访问该节点上的所有标准 DOM 接口。在遍历从表达式返回的所有 h2 元素之后,对 iterateNext() 的任何进一步调用都将返回 null 。

- -

针对扩展中的 XML 文档进行评估

- -

以下使用位于 chrome://yourextension/content/peopleDB.xml 的 XML 文档作为示例。

- -
<?xml version="1.0"?>
-<people xmlns:xul = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" >
-  <person>
-  <name first="george" last="bush" />
-  <address street="1600 pennsylvania avenue" city="washington" country="usa"/>
-  <phoneNumber>202-456-1111</phoneNumber>
-  </person>
-  <person>
-  <name first="tony" last="blair" />
-  <address street="10 downing street" city="london" country="uk"/>
-  <phoneNumber>020 7925 0918</phoneNumber>
-  </person>
-</people>
-
- -

为了使 XML 文档的内容在扩展中可用,我们创建一个 XMLHttpRequest 对象以同步加载文档,变量 xmlDoc 将包含该文档作为 XMLDocument 对象,我们可以使用 evaluate 方法。

- -

JavaScript 用于扩展 xul/js 文档。

- -
var req = new XMLHttpRequest();
-
-req.open("GET", "chrome://yourextension/content/peopleDB.xml", false);
-req.send(null);
-
-var xmlDoc = req.responseXML;
-
-var nsResolver = xmlDoc.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
-
-var personIterator = xmlDoc.evaluate('//person', xmlDoc, nsResolver, XPathResult.ANY_TYPE, null );
-
- -

注意

- -

当未定义 XPathResult 对象时,可以使用 Components.interfaces.nsIDOMXPathResult.ANY_TYPE (CI.nsIDOMXPathResult) 在特权代码中检索常量。类似地,可以使用以下创建 XPathEvaluator:

- -
Components.classes["@mozilla.org/dom/xpath-evaluator;1"].createInstance(Components.interfaces.nsIDOMXPathEvaluator)
- -

附录

- -

实现用户定义的命名空间解析器

- -

这只是一个例子。此函数将需要从 xpathExpression 获取命名空间前缀,并返回与该前缀对应的 URI。例如,表达式:

- -
'//xhtml:td/mathml:math'
-
- -

将选择作为 (X)HTML 表数据单元元素的子项的所有 MathML 表达式。

- -

为了将使用命名空间 URI http://www.w3.org/1998/Math/MathMLmathml: 前缀和使用 URI http://www.w3.org/1999/xhtmlxhtml: 关联,我们提供了一个函数:

- -
function nsResolver(prefix) {
-  var ns = {
-    'xhtml' : 'http://www.w3.org/1999/xhtml',
-    'mathml': 'http://www.w3.org/1998/Math/MathML'
-  };
-  return ns[prefix] || null;
-}
-
- -

我们对 document.evaluate 的调用将如下所示:

- -
document.evaluate( '//xhtml:td/mathml:math', document, nsResolver, XPathResult.ANY_TYPE, null );
-
- -

为 XML 文档实现默认命名空间

- -

如前面实现默认命名空间解析器中所述,默认解析器不处理 XML 文档的默认命名空间。 例如使用本文档:

- -
<?xml version="1.0" encoding="UTF-8"?>
-<feed xmlns="http://www.w3.org/2005/Atom">
-    <entry />
-    <entry />
-    <entry />
-</feed>
-
- -

doc.evaluate('//entry', doc, nsResolver, XPathResult.ANY_TYPE, null) 将返回一个空集,其中 nsResolver 是 createNSResolver 返回的解析器。传递一个 null 解析器再好不过了。

- -

一种可能的解决方法是创建一个自定义解析器,返回正确的默认命名空间(本例中为 Atom 命名空间)。请注意,您仍然必须在 XPath 表达式中使用一些命名空间前缀,以便解析器函数能够将其更改为所需的命名空间。例如:

- -
function resolver() {
-    return 'http://www.w3.org/2005/Atom';
-}
-doc.evaluate('//myns:entry', doc, resolver, XPathResult.ANY_TYPE, null)
-
- -

请注意,如果文档使用多个命名空间,则需要更复杂的解析器。

- -

下一节将介绍一种可能更好的方法(并允许不提前知道命名空间)。

- -

使用 XPath 函数引用具有默认命名空间的元素

- -

另一种匹配非空命名空间中的默认的元素的方法(以及对于动态 XPath 表达式很有效,其中命名空间可能未知),涉及使用如 [namespace-uri()='http://www.w3.org/1999/xhtml' and name()='p' and @id='_myid']。这避免了 XPath 查询无法检测到定期标记的元素上的默认命名空间的问题。

- -

获取特定的命名空间元素和属性,而不考虑前缀

- -

如果希望在命名空间(像预期的那样)中提供灵活性,当发现命名空间元素或属性时不一定需要使用特定的前缀,必须使用特殊技术。

- -

虽然可以修改上述部分中的方法来测试命名空间元素,而不管选择的前缀(使用 local-name() 结合 namespace-uri() 而不是 name()),但是会发生更具挑战性的情况,如果希望在谓词中获取具有特定命名空间属性的元素(假设在 XPath 1.0 中没有与实现无关的变量)。

- -

例如,可能尝试(不正确地)使用 namespaced 属性获取元素,如下所示: var xpathlink = someElements[local-name(@*)="href" and namespace-uri(@*)='http://www.w3.org/1999/xlink'];

- -

这可能会无意中抓取一些元素,如果它的一个属性存在,本地名称为 href,但它是一个不同的属性,有目标(XLink)命名空间(而不是 @href)。

- -

为了使用 XLink @href 属性(而不仅限于命名空间解析器中的预定义前缀)精确地抓取元素,可以按如下方式获取它们:

- -
var xpathEls = 'someElements[@*[local-name() = "href" and namespace-uri() = "http://www.w3.org/1999/xlink"]]'; // Grabs elements with any single attribute that has both the local name 'href' and the XLink namespace
-var thislevel = xml.evaluate(xpathEls, xml, null, XPathResult.ANY_TYPE, null);
-var thisitemEl = thislevel.iterateNext();
-
- -

XPathResult 定义的常量

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
结果类型定义的常数描述
ANY_TYPE0包含任何类型的结果集,从表达式的评估中自然地产生。注意,如果结果是节点集,则 UNORDERED_NODE_ITERATOR_TYPE 始终是结果类型。
NUMBER_TYPE1包含单个数字的结果。这非常有用,例如,在 XPath 表达式中使用 count() 函数。
STRING_TYPE2包含单个字符串的结果。
BOOLEAN_TYPE3包含单个布尔值的结果。这非常有用,例如,在 XPath 表达式中使用 not() 函数。
UNORDERED_NODE_ITERATOR_TYPE4包含与表达式匹配的所有节点的结果节点集。节点可能不一定与它们在文档中出现的顺序相同。
ORDERED_NODE_ITERATOR_TYPE5包含与表达式匹配的所有节点的结果节点集。结果集中的节点与文档中显示的节点顺序相同。
UNORDERED_NODE_SNAPSHOT_TYPE6包含与表达式匹配的所有节点的快照的结果节点集。节点可能不一定与它们在文档中出现的顺序相同。
ORDERED_NODE_SNAPSHOT_TYPE7包含与表达式匹配的所有节点的快照的结果节点集。结果集中的节点与文档中显示的节点顺序相同。
ANY_UNORDERED_NODE_TYPE8包含与表达式匹配的任何单个节点的结果节点集。该节点不一定是文档中与表达式匹配的第一个节点。
FIRST_ORDERED_NODE_TYPE9包含文档中与表达式匹配的第一个节点的结果节点集。
- -

参见

- - - -
-

Original Document Information

- - -
- -

 

diff --git a/files/zh-cn/web/xpath/introduction_to_using_xpath_in_javascript/index.md b/files/zh-cn/web/xpath/introduction_to_using_xpath_in_javascript/index.md new file mode 100644 index 00000000000000..d65728349d1287 --- /dev/null +++ b/files/zh-cn/web/xpath/introduction_to_using_xpath_in_javascript/index.md @@ -0,0 +1,430 @@ +--- +title: Introduction to using XPath in JavaScript +slug: Web/XPath/Introduction_to_using_XPath_in_JavaScript +tags: + - DOM + - Extensions + - Transforming_XML_with_XSLT + - Web Development + - XPath +translation_of: Web/XPath/Introduction_to_using_XPath_in_JavaScript +original_slug: Web/JavaScript/Introduction_to_using_XPath_in_JavaScript +--- +该篇文档描述了如何在扩展和网站内部通过 JavaScript 调用 [XPath](/zh-CN/XPath) 接口。 Mozilla 实现了相当多的 [DOM 3 XPath](http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html),意味着 Xpath 表达式已经可以在 HTML 和 XML 文档中使用。 + +使用 XPath 的主要接口是 [document](/zh-CN/DOM/document) 对象的 [evaluate](/zh-CN/DOM/document.evaluate) 方法。 + +## document.evaluate + +此方法针对基于 [XML](/en-US/docs/Glossary/XML) 的文档(包括 HTML 文档)评估 XPath 表达式,并返回 [XPathResult](/en-US/docs/XPathResult) 对象,该对象可以是单个节点或一组节点。这个方法的现有文档位于 [document.evaluate](/en-US/docs/Web/API/Document.evaluate),但是对于我们现在的需求来说它相当稀疏;下面将给出更全面的研究。 + +```js +var xpathResult = document.evaluate( xpathExpression, contextNode, namespaceResolver, resultType, result ); +``` + +### 参数 + +[evaluate](/en-US/docs/Web/API/Document.evaluate) 函数共有五个参数: + +- `xpathExpression`:包含要评估的 XPath 表达式的字符串。 +- `contextNode`:应评估 `xpathExpression` 的文档中的节点,包括其任何和所有子节点。document 节点是最常用的。 +- `namespaceResolver`:将传递包含在 `xpathExpression` 中的任何命名空间前缀的函数,它返回一个表示与该前缀关联的命名空间 URI 的字符串。这使得能够在 XPath 表达式中使用的前缀和文档中使用的可能不同的前缀之间进行转换。该转换函数可以是: + + - 使用 [`XPathEvaluator`](/en-US/docs/Using_XPath#Node-specific_evaluator_function) 对象的 [`createNSResolver`](/en-US/docs/Web/API/Document.createNSResolver) 方法[创建](/en-US/docs/Introduction_to_using_XPath_in_JavaScript#Implementing_a_Default_Namespace_Resolver)。 + - `null`。其可以用于 HTML 文档或者当不使用命名空间前缀时。注意,如果 `xpathExpression` 包含命名空间前缀,这将导致一个带有 `NAMESPACE_ERR` 的 `DOMException` 抛出。 + - 用户定义的函数。有关详细信息,请参阅附录中的 [使用一个用户定义的命名空间解析器](/en-US/docs/Introduction_to_using_XPath_in_JavaScript#Implementing_a_User_Defined_Namespace_Resolver) 部分。 + +- `resultType`:指定作为评估结果返回的所需结果类型的[常数](/en-US/docs/Introduction_to_using_XPath_in_JavaScript#XPathResult_Defined_Constants)。最常传递的常量是 `XPathResult.ANY_TYPE`,它将返回 XPath 表达式的结果作为最自然的类型。附录中有一个部分,其中包含[可用常数](/en-US/docs/Introduction_to_using_XPath_in_JavaScript#XPathResult_Defined_Constants)的完整列表。它们在下面“[指定返回类型](/en-US/docs/Introduction_to_using_XPath_in_JavaScript#Specifying_the_Return_Type)”部分中进行解释。 +- `result`:如果指定了现有的 `XPathResult` 对象,它将被重用以返回结果。指定 `null` 将创建一个新的 `XPathResult` 对象。 + +### 返回值 + +返回 `xpathResult`,它是 `resultType` 参数中[指定的](/en-US/docs/Introduction_to_using_XPath_in_JavaScript#Specifying_the_Return_Type)类型的 `XPathResult` 对象。`XPathResult` 在[这里](http://mxr.mozilla.org/mozilla-central/source/dom/interfaces/xpath/nsIDOMXPathResult.idl)定义。 + +### 实现默认的命名空间解析器 + +我们使用 document 对象的 `createNSResolver` 方法创建一个命名空间解析器。 + +```js +var nsResolver = document.createNSResolver( contextNode.ownerDocument == null ? contextNode.documentElement : contextNode.ownerDocument.documentElement ); +``` + +或者,也可以使用 `XPathEvaluator` 对象的 `createNSResolver` 方法。 + + var xpEvaluator = new XPathEvaluator(); var nsResolver = xpEvaluator.createNSResolver( contextNode.ownerDocument == null ? contextNode.documentElement : contextNode.ownerDocument.documentElement ); + +然后传递 `document.evaluate`,将 `nsResolver` 变量作为 `namespaceResolver` 参数。 + +注意:XPath 定义不带前缀的 QNames,以仅匹配 null 命名空间中的元素。XPath 没有办法选择应用于常规元素引用的默认命名空间(例如,`p[@id='_myid']` 对应于 `xmlns='http://www.w3.org/1999/xhtml'`)。要匹配非命名空间中的默认元素,您必须使用如 `[namespace-uri()='http://www.w3.org/1999/xhtml' and name()='p' and @id='_id']`([这种方法](/en-US/docs/Introduction_to_using_XPath_in_JavaScript#Using_XPath_functions_to_reference_elements_with_a_default_namespace)适用于命名空间未知的动态 XPath),或者使用前缀名测试,并创建一个命名空间解析器将前缀映射到命名空间。如果你想采取后一种方法,阅读更多关于[如何创建一个用户定义的命名空间解析器](/en-US/docs/Introduction_to_using_XPath_in_JavaScript#Implementing_a_User_Defined_Namespace_Resolver)。 + +### 注意 + +适应任何 DOM 节点以解析命名空间,以便可以相对于文档中出现的节点的上下文轻松地评估 XPath 表达式。此适配器的工作方式类似于 DOM 级别 3 方法 `lookupNamespaceURI` 在解析 `namespaceuRI` 时节点的层次结构中的可用的当前信息的节点。也正确解析了隐式 `xml` 前缀。 + +### 指定返回类型 + +`document.evaluate` 返回的变量 `xpathResult` 可以由单个节点([简单类型](/en-US/docs/Introduction_to_using_XPath_in_JavaScript#Simple_Types))或节点集合([节点集类型](/en-US/docs/Introduction_to_using_XPath_in_JavaScript#Node-Set_Types))组成。 + +#### 简单类型 + +当 `resultType` 中的所需结果类型指定为: + +- `NUMBER_TYPE` - a double +- `STRING_TYPE` - a string +- `BOOLEAN_TYPE` - a boolean + +我们通过分别访问 `XPathResult` 对象的以下属性来获取表达式的返回值。 + +- `numberValue` +- `stringValue` +- `booleanValue` + +##### 示例 + +以下使用 XPath 表达式 [`count(//p)`](/en-US/docs/XPath/Functions/count) 来获取 HTML 文档中的 `

` 元素数: + +```js +var paragraphCount = document.evaluate( 'count(//p)', document, null, XPathResult.ANY_TYPE, null ); + +alert( 'This document contains ' + paragraphCount.numberValue + ' paragraph elements' ); +``` + +虽然 JavaScript 允许我们将数字转换为一个字符串进行显示,但 XPath 接口不会自动转换数字结果,如果 `stringValue` 属性被请求,所以下面的代码将**不**工作: + +```js +var paragraphCount = document.evaluate('count(//p)', document, null, XPathResult.ANY_TYPE, null ); + +alert( 'This document contains ' + paragraphCount.stringValue + ' paragraph elements' ); +``` + +相反,它将返回一个带有 `NS_DOM_TYPE_ERROR` 的异常。 + +#### 节点集类型 + +`XPathResult` 对象允许以 3 种主要不同类型返回节点集: + +- [Iterators](#Iterators) +- [Snapshots](#Snapshots) +- [First Nodes](#First_Node) + +##### Iterators + +当 `resultType` 参数中的指定结果类型为: + +- `UNORDERED_NODE_ITERATOR_TYPE` +- `ORDERED_NODE_ITERATOR_TYPE` + +返回的 `XPathResult` 对象是一个匹配节点的节点集,它将作为迭代器,允许我们使用 `XPathResult` 的 `iterateNext()` 方法访问包含的各个节点。 + +一旦迭代完成所有的匹配节点,`iterateNext()` 将返回 `null`。 + +但请注意,如果在迭代过程中,文档发生突变(文档树被修改),将使迭代无效,并且 `XPathResult` 的 `invalidIteratorState` 属性设置为 `true`,抛出 `NS_ERROR_DOM_INVALID_STATE_ERR` 异常。 + +```js +var iterator = document.evaluate('//phoneNumber', documentNode, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null ); + +try { + var thisNode = iterator.iterateNext(); + + while (thisNode) { + alert( thisNode.textContent ); + thisNode = iterator.iterateNext(); + } +} +catch (e) { + dump( 'Error: Document tree modified during iteration ' + e ); +} +``` + +##### Snapshots + +当 `resultType` 参数中的指定结果类型为: + +- `UNORDERED_NODE_SNAPSHOT_TYPE` +- `ORDERED_NODE_SNAPSHOT_TYPE` + +返回的 `XPathResult` 对象是一个匹配节点的静态节点集,这允许我们通过 `XPathResult` 对象的 `snapshotItem(itemNumber)` 方法访问每个节点,其中 `itemNumber` 是要检索的节点的索引。包含的节点总数可以通过 `snapshotLength` 属性访问。 + +快照不随文档突变而改变,因此与迭代器不同,快照不会变得无效,但是它可能不对应于当前文档,例如节点可能已被移动,它可能包含不再存在的节点,或新节点可能已添加。 + +```js +var nodesSnapshot = document.evaluate('//phoneNumber', documentNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null ); + +for ( var i=0 ; i < nodesSnapshot.snapshotLength; i++ ) +{ + dump( nodesSnapshot.snapshotItem(i).textContent ); +} +``` + +##### First Node + +当 `resultType` 参数中的指定结果类型为: + +- `ANY_UNORDERED_NODE_TYPE` +- `FIRST_ORDERED_NODE_TYPE` + +返回的 `XPathResult` 对象只是匹配 XPath 表达式的第一个找到的节点。这可以通过 `XPathResult` 对象的 `singleNodeValue` 属性访问。如果节点集为空,这将为 `null`。 + +请注意,对于无序子类型,返回的单个节点可能不是文档顺序中的第一个,但是对于有序子类型,保证以文档顺序获取第一个匹配的节点。 + +```js +var firstPhoneNumber = document.evaluate('//phoneNumber', documentNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ); + +dump( 'The first phone number found is ' + firstPhoneNumber.singleNodeValue.textContent ); +``` + +#### ANY_TYPE 常量 + +当 `resultType` 参数中的结果类型指定为 `ANY_TYPE` 时,返回的 `XPathResult` 对象将是由表达式求值自然产生的任何类型。 + +它可以是任何简单类型(`NUMBER_TYPE`,`STRING_TYPE`,`BOOLEAN_TYPE` ),**但**如果返回的结果类型是节点集,那么它将**只**是一个 `UNORDERED_NODE_ITERATOR_TYPE`。 + +要在评估后确定类型,我们使用 `XPathResult` 对象的 `resultType` 属性。此属性的[常量](/en-US/docs/Introduction_to_using_XPath_in_JavaScript#XPathResult_Defined_Constants)值在附录中定义。 None Yet =====Any_Type Example===== \

 \
+ +## 示例 + +### 在 HTML 文档中 + +以下代码旨在放置在要针对其评估 XPath 表达式的 HTML 文档中内嵌或外链的任何 JavaScript 片段中。 + +要使用 XPath 提取 HTML 文档中的所有 `

` 标题元素,`xpathExpression` 只是 `//h2`。其中,`//` 是递归下降运算符,在文档树中的任何位置将元素与 nodeName `h2` 相匹配。这个的完整代码是: link to introductory xpath doc + +```js +var headings = document.evaluate('//h2', document, null, XPathResult.ANY_TYPE, null ); +``` + +请注意,由于 HTML 没有命名空间,因此我们为 `namespaceResolver` 参数传递了 `null`。 + +因为希望在整个文档中搜索标题,所以我们使用 document 对象本身作为 `contextNode`。 + +此表达式的结果是 `XPathResult` 对象。如果想知道返回的结果的类型,我们可以评估返回的对象的 `resultType` 属性。在这种情况下,这将评估为 `4`,即 `UNORDERED_NODE_ITERATOR_TYPE`。这是 XPath 表达式的结果是节点集时的默认返回类型。它一次提供对单个节点的访问,并且可能不以特定顺序返回节点。要访问返回的节点,我们使用返回对象的 `iterateNext()` 方法: + +```js +var thisHeading = headings.iterateNext(); + +var alertText = 'Level 2 headings in this document are:\n' + +while (thisHeading) { + alertText += thisHeading.textContent + '\n'; + thisHeading = headings.iterateNext(); +} +``` + +一旦迭代到一个节点,我们就可以访问该节点上的所有标准 DOM 接口。在遍历从表达式返回的所有 `h2` 元素之后,对 `iterateNext()` 的任何进一步调用都将返回 `null` 。 + +### 针对扩展中的 XML 文档进行评估 + +以下使用位于 [chrome://yourextension/content/peopleDB.xml]() 的 XML 文档作为示例。 + +```xml + + + + +
+ 202-456-1111 + + + +
+ 020 7925 0918 + + +``` + +为了使 XML 文档的内容在扩展中可用,我们创建一个 [`XMLHttpRequest`](/en-US/docs/Web/API/XMLHttpRequest) 对象以同步加载文档,变量 `xmlDoc` 将包含该文档作为 [`XMLDocument`](/en-US/docs/Web/API/XMLDocument) 对象,我们可以使用 `evaluate` 方法。 + +JavaScript 用于扩展 xul/js 文档。 + +```js +var req = new XMLHttpRequest(); + +req.open("GET", "chrome://yourextension/content/peopleDB.xml", false); +req.send(null); + +var xmlDoc = req.responseXML; + +var nsResolver = xmlDoc.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement); + +var personIterator = xmlDoc.evaluate('//person', xmlDoc, nsResolver, XPathResult.ANY_TYPE, null ); +``` + +### 注意 + +当未定义 XPathResult 对象时,可以使用 `Components.interfaces.nsIDOMXPathResult.ANY_TYPE` (`CI.nsIDOMXPathResult`) 在特权代码中检索常量。类似地,可以使用以下创建 XPathEvaluator: + +```js +Components.classes["@mozilla.org/dom/xpath-evaluator;1"].createInstance(Components.interfaces.nsIDOMXPathEvaluator) +``` + +## 附录 + +#### 实现用户定义的命名空间解析器 + +这只是一个例子。此函数将需要从 `xpathExpression` 获取命名空间前缀,并返回与该前缀对应的 URI。例如,表达式: + + '//xhtml:td/mathml:math' + +将选择作为 (X)HTML 表数据单元元素的子项的所有 [MathML](/en-US/docs/Web/API/MathML) 表达式。 + +为了将使用命名空间 URI `http://www.w3.org/1998/Math/MathML` 的 `mathml:` 前缀和使用 URI `http://www.w3.org/1999/xhtml` 的 `xhtml:` 关联,我们提供了一个函数: + +```js +function nsResolver(prefix) { + var ns = { + 'xhtml' : 'http://www.w3.org/1999/xhtml', + 'mathml': 'http://www.w3.org/1998/Math/MathML' + }; + return ns[prefix] || null; +} +``` + +我们对 `document.evaluate` 的调用将如下所示: + +```js +document.evaluate( '//xhtml:td/mathml:math', document, nsResolver, XPathResult.ANY_TYPE, null ); +``` + +#### 为 XML 文档实现默认命名空间 + +如前面[实现默认命名空间解析器](/en-US/docs/Introduction_to_using_XPath_in_JavaScript#Implementing_a_Default_Namespace_Resolver)中所述,默认解析器不处理 XML 文档的默认命名空间。 例如使用本文档: + +```xml + + + + + + +``` + +`doc.evaluate('//entry', doc, nsResolver, XPathResult.ANY_TYPE, null)` 将返回一个空集,其中 `nsResolver` 是 `createNSResolver` 返回的解析器。传递一个 `null` 解析器再好不过了。 + +一种可能的解决方法是创建一个自定义解析器,返回正确的默认命名空间(本例中为 Atom 命名空间)。请注意,您仍然必须在 XPath 表达式中使用一些命名空间前缀,以便解析器函数能够将其更改为所需的命名空间。例如: + +```js +function resolver() { + return 'http://www.w3.org/2005/Atom'; +} +doc.evaluate('//myns:entry', doc, resolver, XPathResult.ANY_TYPE, null) +``` + +请注意,如果文档使用多个命名空间,则需要更复杂的解析器。 + +下一节将介绍一种可能更好的方法(并允许不提前知道命名空间)。 + +#### 使用 XPath 函数引用具有默认命名空间的元素 + +另一种匹配非空命名空间中的默认的元素的方法(以及对于动态 XPath 表达式很有效,其中命名空间可能未知),涉及使用如 `[namespace-uri()='http://www.w3.org/1999/xhtml' and name()='p' and @id='_myid']`。这避免了 XPath 查询无法检测到定期标记的元素上的默认命名空间的问题。 + +#### 获取特定的命名空间元素和属性,而不考虑前缀 + +如果希望在命名空间(像预期的那样)中提供灵活性,当发现命名空间元素或属性时不一定需要使用特定的前缀,必须使用特殊技术。 + +虽然可以修改上述部分中的方法来测试命名空间元素,而不管选择的前缀(使用 [`local-name()`](/en-US/docs/XPath/Functions/local-name) 结合 [`namespace-uri()`](/en-US/docs/XPath/Functions/namespace-uri) 而不是 [`name()`](/en-US/docs/XPath/Functions/name)),但是会发生更具挑战性的情况,如果希望在谓词中获取具有特定命名空间属性的元素(假设在 XPath 1.0 中没有与实现无关的变量)。 + +例如,可能尝试(不正确地)使用 namespaced 属性获取元素,如下所示: `var xpathlink = someElements[local-name(@*)="href" and namespace-uri(@*)='http://www.w3.org/1999/xlink'];` + +这可能会无意中抓取一些元素,如果它的一个属性存在,本地名称为 `href`,但它是一个不同的属性,有目标(XLink)命名空间(而不是 `@href`)。 + +为了使用 XLink `@href` 属性(而不仅限于命名空间解析器中的预定义前缀)精确地抓取元素,可以按如下方式获取它们: + +```js +var xpathEls = 'someElements[@*[local-name() = "href" and namespace-uri() = "http://www.w3.org/1999/xlink"]]'; // Grabs elements with any single attribute that has both the local name 'href' and the XLink namespace +var thislevel = xml.evaluate(xpathEls, xml, null, XPathResult.ANY_TYPE, null); +var thisitemEl = thislevel.iterateNext(); +``` + +#### XPathResult 定义的常量 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
结果类型定义的常数描述
ANY_TYPE0 + 包含任何类型的结果集,从表达式的评估中自然地产生。注意,如果结果是节点集,则 + UNORDERED_NODE_ITERATOR_TYPE 始终是结果类型。 +
NUMBER_TYPE1 + 包含单个数字的结果。这非常有用,例如,在 XPath 表达式中使用 + count() 函数。 +
STRING_TYPE2包含单个字符串的结果。
BOOLEAN_TYPE3 + 包含单个布尔值的结果。这非常有用,例如,在 XPath 表达式中使用 + not() 函数。 +
UNORDERED_NODE_ITERATOR_TYPE4 + 包含与表达式匹配的所有节点的结果节点集。节点可能不一定与它们在文档中出现的顺序相同。 +
ORDERED_NODE_ITERATOR_TYPE5 + 包含与表达式匹配的所有节点的结果节点集。结果集中的节点与文档中显示的节点顺序相同。 +
UNORDERED_NODE_SNAPSHOT_TYPE6 + 包含与表达式匹配的所有节点的快照的结果节点集。节点可能不一定与它们在文档中出现的顺序相同。 +
ORDERED_NODE_SNAPSHOT_TYPE7 + 包含与表达式匹配的所有节点的快照的结果节点集。结果集中的节点与文档中显示的节点顺序相同。 +
ANY_UNORDERED_NODE_TYPE8 + 包含与表达式匹配的任何单个节点的结果节点集。该节点不一定是文档中与表达式匹配的第一个节点。 +
FIRST_ORDERED_NODE_TYPE9包含文档中与表达式匹配的第一个节点的结果节点集。
+ +## 参见 + +- [Using XPath](/zh-CN/Using_XPath) + +## Original Document Information + +- Based Upon Original Document [Mozilla XPath Tutorial](http://www-xray.ast.cam.ac.uk/~jgraham/mozilla/xpath-tutorial.html) +- Original Source Author: James Graham. +- Other Contributors: James Thompson. +- Last Updated Date: 2006-3-25. diff --git a/files/zh-cn/web/xslt/element/index.html b/files/zh-cn/web/xslt/element/index.html deleted file mode 100644 index b847db18f5e520..00000000000000 --- a/files/zh-cn/web/xslt/element/index.html +++ /dev/null @@ -1,53 +0,0 @@ ---- -title: Elements -slug: Web/XSLT/Element -tags: - - XSLT_Reference -translation_of: Web/XSLT/Element -original_slug: Web/XSLT/Elements ---- -

{{ XsltRef() }} There are two types of elements discussed here: top-level elements and instructions. A top-level element must appear as the child of either <xsl:stylesheet> or <xsl:transform>. An instruction, on the other hand, is associated with a template. A stylesheet may include several templates. A third type of element, not discussed here, is the literal result element (LRE). An LRE also appears in a template. It consists of any non-instruction element that should be copied as-is to the result document, for example, an <hr> element in an HTML conversion stylesheet.

-

On a related note, any attribute in an LRE and some attributes of a limited number of XSLT elements can also include what is known as an attribute value template. An attribute value template is simply a string that includes an embedded XPath expression which is used to specify the value of an attribute. At run-time the expression is evaluated and the result of the evaluation is substituted for the XPath expression. For example, assume that a variable "image-dir" is defined as follows:

-
<xsl:variable name="image-dir">/images</xsl:variable>
-

The expression to be evaluated is placed inside curly brackets:

-
<img src="{$image-dir}/mygraphic.jpg"/>
-

This would result in the following:

-
<img src="/images/mygraphic.jpg"/>
-

The element annotations that follow include a description, a syntax listing, a list of required and optional attributes, a description of type and position, its source in the W3C Recommendation and an explanation of the degree of present Gecko support.

- diff --git a/files/zh-cn/web/xslt/element/index.md b/files/zh-cn/web/xslt/element/index.md new file mode 100644 index 00000000000000..08cc3d0a7188d1 --- /dev/null +++ b/files/zh-cn/web/xslt/element/index.md @@ -0,0 +1,65 @@ +--- +title: Elements +slug: Web/XSLT/Element +tags: + - XSLT_Reference +translation_of: Web/XSLT/Element +original_slug: Web/XSLT/Elements +--- +{{ XsltRef() }} There are two types of elements discussed here: top-level elements and instructions. A top-level element must appear as the child of either `` or ``. An instruction, on the other hand, is associated with a template. A stylesheet may include several templates. A third type of element, not discussed here, is the literal result element (LRE). An LRE also appears in a template. It consists of any non-instruction element that should be copied as-is to the result document, for example, an `
` element in an HTML conversion stylesheet. + +On a related note, any attribute in an LRE and some attributes of a limited number of XSLT elements can also include what is known as an attribute value template. An attribute value template is simply a string that includes an embedded XPath expression which is used to specify the value of an attribute. At run-time the expression is evaluated and the result of the evaluation is substituted for the XPath expression. For example, assume that a variable "`image-dir`" is defined as follows: + +```xml +/images +``` + +The expression to be evaluated is placed inside curly brackets: + +```xml + +``` + +This would result in the following: + +```xml + +``` + +The element annotations that follow include a description, a syntax listing, a list of required and optional attributes, a description of type and position, its source in the W3C Recommendation and an explanation of the degree of present Gecko support. + +- [xsl:apply-imports](/cn/XSLT/apply-imports) +- [xsl:apply-templates](/cn/XSLT/apply-templates) +- [xsl:attribute](/cn/XSLT/attribute) +- [xsl:attribute-set](/cn/XSLT/attribute-set) +- [xsl:call-template](/cn/XSLT/call-template) +- [xsl:choose](/cn/XSLT/choose) +- [xsl:comment](/cn/XSLT/comment) +- [xsl:copy](/cn/XSLT/copy) +- [xsl:copy-of](/cn/XSLT/copy-of) +- [xsl:decimal-format](/cn/XSLT/decimal-format) +- [xsl:element](/cn/XSLT/element) +- [xsl:fallback](/cn/XSLT/fallback) _(not supported)_ +- [xsl:for-each](/cn/XSLT/for-each) +- [xsl:if](/cn/XSLT/if) +- [xsl:import](/cn/XSLT/import) _(mostly supported)_ +- [xsl:include](/cn/XSLT/include) +- [xsl:key](/cn/XSLT/key) +- [xsl:message](/cn/XSLT/message) +- [xsl:namespace-alias](/cn/XSLT/namespace-alias) _(not supported)_ +- [xsl:number](/cn/XSLT/number) _(partially supported)_ +- [xsl:otherwise](/cn/XSLT/otherwise) +- [xsl:output](/cn/XSLT/output) _(partially supported)_ +- [xsl:param](/cn/XSLT/param) +- [xsl:preserve-space](/cn/XSLT/preserve-space) +- [xsl:processing-instruction](/cn/XSLT/processing-instruction) +- [xsl:sort](/cn/XSLT/sort) +- [xsl:strip-space](/cn/XSLT/strip-space) +- [xsl:stylesheet](/cn/XSLT/stylesheet) _(partially supported)_ +- [xsl:template](/cn/XSLT/template) +- [xsl:text](/cn/XSLT/text) _(partially supported)_ +- [xsl:transform](/cn/XSLT/transform) +- [xsl:value-of](/cn/XSLT/value-of) _(partially supported)_ +- [xsl:variable](/cn/XSLT/variable) +- [xsl:when](/cn/XSLT/when) +- [xsl:with-param](/cn/XSLT/with-param) diff --git a/files/zh-cn/web/xslt/index.html b/files/zh-cn/web/xslt/index.html deleted file mode 100644 index 522ad92020cea8..00000000000000 --- a/files/zh-cn/web/xslt/index.html +++ /dev/null @@ -1,69 +0,0 @@ ---- -title: XSLT -slug: Web/XSLT -tags: - - XSLT -translation_of: Web/XSLT ---- -
可扩展样式表语言转换 (XSLT)是一种基于XML的语言,和专门的处理软件一起使用,用于 XML 文档转换。虽然这个处理过程被称作“转换”,但并没有改变原始文档,而是在原文档内容的基础上创建了一个新的 XML 文档。然后,这个新文档会被处理器序列化(输出)为标准的 XML 语法或其他格式,如HTML或纯文本。XSLT 最常用于不同 XML 模式间的数据转换,或用于将 XML 数据转换为网页或 PDF 文档。
- - - - - - - - -
-

Documentation

- -
-
XSLT Element Reference
-
- -
-
Transforming XML with XSLT
-
XSLT allows a stylesheet author to transform a primary XML document in two significant ways: manipulating and sorting the content, including a wholesale reordering of it if so desired, and transforming the content into a different format.
-
- -
-
Using the Mozilla JavaScript interface to XSL Transformations
-
This document describes the JavaScript interface to the XSLT processing engine in Mozilla 1.2 and up.
-
- -
-
Specifying parameters using processing instructions
-
Firefox allows stylesheet parameters to be specified when using the <?xml-stylesheet?> processing instruction. This is done using the <?xslt-param?> PI described in this document.
-
- -
-
XSLT Tutorial
-
This W3Schools tutorial teaches the reader how to use XSLT to transform XML documents into other formats, like XHTML.
-
- -
-
What is XSLT?
-
This extensive introduction to XSLT and XPath assumes no prior knowledge of the technologies, and guides the reader through background, context, structure, concepts and introductory terminology.
-
- -
-
XSLT 2.0 (new)
-
- -
- - -
-
XML, XPath, XQuery
-
-
- -
- -
-

備註:This description of XSLT is obtained from the Wikipedia XSLT article.

-
- -

Categories

- -

Interwiki Language Links

diff --git a/files/zh-cn/web/xslt/index.md b/files/zh-cn/web/xslt/index.md new file mode 100644 index 00000000000000..24dca321c8cef3 --- /dev/null +++ b/files/zh-cn/web/xslt/index.md @@ -0,0 +1,35 @@ +--- +title: XSLT +slug: Web/XSLT +tags: + - XSLT +translation_of: Web/XSLT +--- +**可扩展样式表语言转换 (XSLT)**是一种基于[XML](/zh-CN/docs/Web/XML/XML_introduction)的语言,和专门的处理软件一起使用,用于 XML 文档转换。虽然这个处理过程被称作“转换”,但并没有改变原始文档,而是在原文档内容的基础上创建了一个新的 XML 文档。然后,这个新文档会被处理器序列化(输出)为标准的 XML 语法或其他格式,如[HTML](/zh-CN/docs/Web/HTML)或纯文本。XSLT 最常用于不同 XML 模式间的数据转换,或用于将 XML 数据转换为网页或 PDF 文档。 + +#### Documentation + +- [XSLT Element Reference](/zh-CN/docs/Web/XSLT/Elements) + - : Reference. +- [Transforming XML with XSLT](/zh-CN/docs/Web/XSLT/Transforming_XML_with_XSLT) + - : XSLT allows a stylesheet author to transform a primary XML document in two significant ways: manipulating and sorting the content, including a wholesale reordering of it if so desired, and transforming the content into a different format. +- [Using the Mozilla JavaScript interface to XSL Transformations](/zh-CN/docs/Web/XSLT/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations) + - : This document describes the JavaScript interface to the XSLT processing engine in Mozilla 1.2 and up. +- [Specifying parameters using processing instructions](/zh-CN/docs/Web/XSLT/PI_Parameters) + - : Firefox allows stylesheet parameters to be specified when using the `` processing instruction. This is done using the `` PI described in this document. +- [XSLT Tutorial](http://www.w3schools.com/xml/xsl_intro.asp) + - : This [W3Schools](http://www.w3schools.com) tutorial teaches the reader how to use XSLT to transform XML documents into other formats, like XHTML. +- [What is XSLT?](http://www.xml.com/pub/a/2000/08/holman/) + - : This extensive introduction to XSLT and XPath assumes no prior knowledge of the technologies, and guides the reader through background, context, structure, concepts and introductory terminology. +- [Common XSLT Errors](/zh-CN/docs/Web/XSLT/Common_errors) + - : This article lists some common problems using XSLT in Firefox. + +#### Related Topics + +- [XML](/zh-CN/docs/Web/XML/XML_introduction), [XPath](/zh-CN/docs/Web/XPath) + +> **备注:** This description of XSLT is obtained from the [Wikipedia XSLT article](http://en.wikipedia.org/wiki/Xslt). + +Categories + +Interwiki Language Links diff --git a/files/zh-cn/web/xslt/transforming_xml_with_xslt/index.html b/files/zh-cn/web/xslt/transforming_xml_with_xslt/index.html deleted file mode 100644 index f2b2037a4ca46e..00000000000000 --- a/files/zh-cn/web/xslt/transforming_xml_with_xslt/index.html +++ /dev/null @@ -1,345 +0,0 @@ ---- -title: Transforming XML with XSLT -slug: Web/XSLT/Transforming_XML_with_XSLT -tags: - - NeedsMigration - - Transforming_XML_with_XSLT - - XSLT -translation_of: Web/XSLT/Transforming_XML_with_XSLT ---- -

 

-

 

-

An Overview

-

The separation of content and presentation is a key design feature of XML. The structure of an XML document is designed to reflect and clarify important relationships among the individual aspects of the content itself, unhindered by a need to provide any indication about how this data should eventually be presented. This intelligent structuring is particularly important as more and more data transfers are automated and take place between highly heterogeneous machines linked by a network.

-

Yet eventually much of the content stored in XML documents will need to be presented to human readers. Because a browser provides a familiar and highly flexible interface, it is an ideal mechanism for delivering such presentation versions of XML content. Built from the ground up utilizing a wide variety of XML technologies, Mozilla incorporates within itself all of the mechanisms needed to process both original XML documents and the specialized stylesheets used to style and lay them out for HTML display, reducing server load with client-side processing.

-

At present, Gecko (the layout engine behind Mozilla and Firefox) supports two forms of XML stylesheets. For basic control of appearance -- fonts, colors, position, and so forth -- Gecko uses CSS, familiar from DHTML. All of CSS1 and most of CSS2 are supported. Support for the emerging CSS3 standard is under development. For further information about CSS, see Eric Meyer's CSS pages.

-

Our focus here is on the second type of stylesheet that Gecko supports: the XSLT stylesheet. XSLT stands for eXtensible Stylesheet Language/Transform and the name is apt. XSLT allows a stylesheet author to transform a primary XML document in two significant ways: manipulating and sorting the content, including a wholesale reordering of it if so desired, and transforming the content into a different format (and in the case of Mozilla, the focus is on converting it on the fly into HTML which can then be displayed by the browser).

-

XSLT/XPath reference

-

Elements

- -

Axes

- -

Functions

- -

For Further Reading

- -

Resources

-

Index

-
-

Original Document Information

-
    -
  • Copyright Information: Copyright © 2001-2003 Netscape. All rights reserved.
  • -
  • Note: This reprinted article was originally part of the DevEdge site.
  • -
-
diff --git a/files/zh-cn/web/xslt/transforming_xml_with_xslt/index.md b/files/zh-cn/web/xslt/transforming_xml_with_xslt/index.md new file mode 100644 index 00000000000000..16a6b22dcf219a --- /dev/null +++ b/files/zh-cn/web/xslt/transforming_xml_with_xslt/index.md @@ -0,0 +1,204 @@ +--- +title: Transforming XML with XSLT +slug: Web/XSLT/Transforming_XML_with_XSLT +tags: + - NeedsMigration + - Transforming_XML_with_XSLT + - XSLT +translation_of: Web/XSLT/Transforming_XML_with_XSLT +--- +### [An Overview](cn/Transforming_XML_with_XSLT/An_Overview) + +The separation of content and presentation is a key design feature of [XML](cn/XML). The structure of an XML document is designed to reflect and clarify important relationships among the individual aspects of the content itself, unhindered by a need to provide any indication about how this data should eventually be presented. This intelligent structuring is particularly important as more and more data transfers are automated and take place between highly heterogeneous machines linked by a network. + +Yet eventually much of the content stored in XML documents will need to be presented to human readers. Because a browser provides a familiar and highly flexible interface, it is an ideal mechanism for delivering such presentation versions of XML content. Built from the ground up utilizing a wide variety of XML technologies, Mozilla incorporates within itself all of the mechanisms needed to process both original XML documents and the specialized stylesheets used to style and lay them out for HTML display, reducing server load with client-side processing. + +At present, Gecko (the layout engine behind Mozilla and Firefox) supports two forms of XML stylesheets. For basic control of appearance -- fonts, colors, position, and so forth -- Gecko uses [CSS](cn/CSS), familiar from [DHTML](cn/DHTML). All of CSS1 and most of CSS2 are supported. Support for the emerging CSS3 standard is under development. For further information about CSS, see [Eric Meyer's CSS pages](http://www.meyerweb.com/eric/css/). + +Our focus here is on the second type of stylesheet that Gecko supports: the XSLT stylesheet. XSLT stands for eXtensible Stylesheet Language/Transform and the name is apt. XSLT allows a stylesheet author to transform a primary XML document in two significant ways: manipulating and sorting the content, including a wholesale reordering of it if so desired, and transforming the content into a different format (and in the case of Mozilla, the focus is on converting it on the fly into HTML which can then be displayed by the browser). + +### XSLT/XPath reference + +#### [Elements](cn/XSLT/Elements) + +- [xsl:apply-imports](cn/XSLT/apply-imports) + _(supported)_ +- [xsl:apply-templates](cn/XSLT/apply-templates) + _(supported)_ +- [xsl:attribute](cn/XSLT/attribute) + _(supported)_ +- [xsl:attribute-set](cn/XSLT/attribute-set) + _(supported)_ +- [xsl:call-template](cn/XSLT/call-template) + _(supported)_ +- [xsl:choose](cn/XSLT/choose) + _(supported)_ +- [xsl:comment](cn/XSLT/comment) + _(supported)_ +- [xsl:copy](cn/XSLT/copy) + _(supported)_ +- [xsl:copy-of](cn/XSLT/copy-of) + _(supported)_ +- [xsl:decimal-format](cn/XSLT/decimal-format) + _(supported)_ +- [xsl:element](cn/XSLT/element) + _(supported)_ +- [xsl:fallback](cn/XSLT/fallback) + _(not supported)_ +- [xsl:for-each](cn/XSLT/for-each) + _(supported)_ +- [xsl:if](cn/XSLT/if) + _(supported)_ +- [xsl:import](cn/XSLT/import) + _(mostly supported)_ +- [xsl:include](cn/XSLT/include) + _(supported)_ +- [xsl:key](cn/XSLT/key) + _(supported)_ +- [xsl:message](cn/XSLT/message) + _(supported)_ +- [xsl:namespace-alias](cn/XSLT/namespace-alias) + _(not supported)_ +- [xsl:number](cn/XSLT/number) + _(partially supported)_ +- [xsl:otherwise](cn/XSLT/otherwise) + _(supported)_ +- [xsl:output](cn/XSLT/output) + _(partially supported)_ +- [xsl:param](cn/XSLT/param) + _(supported)_ +- [xsl:preserve-space](cn/XSLT/preserve-space) + _(supported)_ +- [xsl:processing-instruction](cn/XSLT/processing-instruction) +- [xsl:sort](cn/XSLT/sort) + _(supported)_ +- [xsl:strip-space](cn/XSLT/strip-space) + _(supported)_ +- [xsl:stylesheet](cn/XSLT/stylesheet) + _(partially supported)_ +- [xsl:template](cn/XSLT/template) + _(supported)_ +- [xsl:text](cn/XSLT/text) + _(partially supported)_ +- [xsl:transform](cn/XSLT/transform) + _(supported)_ +- [xsl:value-of](cn/XSLT/value-of) + _(partially supported)_ +- [xsl:variable](cn/XSLT/variable) + _(supported)_ +- [xsl:when](cn/XSLT/when) + _(supported)_ +- [xsl:with-param](cn/XSLT/with-param) + _(supported)_ + +#### [Axes](cn/XPath/Axes) + +- [ancestor](cn/XPath/Axes/ancestor) +- [ancestor-or-self](cn/XPath/Axes/ancestor-or-self) +- [attribute](cn/XPath/Axes/attribute) +- [child](cn/XPath/Axes/child) +- [descendant](cn/XPath/Axes/descendant) +- [descendant-or-self](cn/XPath/Axes/descendant-or-self) +- [following](cn/XPath/Axes/following) +- [following-sibling](cn/XPath/Axes/following-sibling) +- [namespace](cn/XPath/Axes/namespace) + _(not supported)_ +- [parent](cn/XPath/Axes/parent) +- [preceding](cn/XPath/Axes/preceding) +- [preceding-sibling](cn/XPath/Axes/preceding-sibling) +- [self](cn/XPath/Axes/self) + +#### [Functions](cn/XPath/Functions) + +- [boolean()](cn/XPath/Functions/boolean) + _(supported)_ +- [ceiling()](cn/XPath/Functions/ceiling) + _(supported)_ +- [concat()](cn/XPath/Functions/concat) + _(supported)_ +- [contains()](cn/XPath/Functions/contains) + _(supported)_ +- [count()](cn/XPath/Functions/count) + _(supported)_ +- [current()](cn/XPath/Functions/current) + _(supported)_ +- [document()](cn/XPath/Functions/document) + _(supported)_ +- [element-available()](cn/XPath/Functions/element-available) + _(supported)_ +- [false()](cn/XPath/Functions/false) + _(supported)_ +- [floor()](cn/XPath/Functions/floor) + _(supported)_ +- [format-number()](cn/XPath/Functions/format-number) + _(supported)_ +- [function-available()](cn/XPath/Functions/function-available) + _(supported)_ +- [generate-id()](cn/XPath/Functions/generate-id) + _(supported)_ +- [id()](cn/XPath/Functions/id) + _(partially supported)_ +- [key()](cn/XPath/Functions/key) + _(supported)_ +- [lang()](cn/XPath/Functions/lang) + _(supported)_ +- [last()](cn/XPath/Functions/last) + _(supported)_ +- [local-name()](cn/XPath/Functions/local-name) + _(supported)_ +- [name()](cn/XPath/Functions/name) + _(supported)_ +- [namespace-uri()](cn/XPath/Functions/namespace-uri) + _(supported)_ +- [normalize-space()](cn/XPath/Functions/normalize-space) + _(supported)_ +- [not()](cn/XPath/Functions/not) + _(supported)_ +- [number()](cn/XPath/Functions/number) + _(supported)_ +- [position()](cn/XPath/Functions/position) + _(supported)_ +- [round()](cn/XPath/Functions/round) + _(supported)_ +- [starts-with()](cn/XPath/Functions/starts-with) + _(supported)_ +- [string()](cn/XPath/Functions/string) + _(supported)_ +- [string-length()](cn/XPath/Functions/string-length) + _(supported)_ +- [substring()](cn/XPath/Functions/substring) + _(supported)_ +- [substring-after()](cn/XPath/Functions/substring-after) + _(supported)_ +- [substring-before()](cn/XPath/Functions/substring-before) + _(supported)_ +- [sum()](cn/XPath/Functions/sum) + _(supported)_ +- [system-property()](cn/XPath/Functions/system-property) + _(supported)_ +- [translate()](cn/XPath/Functions/translate) + _(supported)_ +- [true()](cn/XPath/Functions/true) + _(supported)_ +- [unparsed-entity-url()](cn/XPath/Functions/unparsed-entity-url) + _(not supported)_ + +### [For Further Reading](cn/Transforming_XML_with_XSLT/For_Further_Reading) + +- [Books](cn/Transforming_XML_with_XSLT/For_Further_Reading#Books) +- [Online](cn/Transforming_XML_with_XSLT/For_Further_Reading#Online) + + - [The World Wide Web Consortium](cn/Transforming_XML_with_XSLT/For_Further_Reading#The_World_Wide_Web_Consortium) + - [Portals](cn/Transforming_XML_with_XSLT/For_Further_Reading#Portals) + - [Articles](cn/Transforming_XML_with_XSLT/For_Further_Reading#Articles) + - [Tutorials/Examples](cn/Transforming_XML_with_XSLT/For_Further_Reading#Tutorials.2FExamples) + - [Mailing Lists/Newsgroups](cn/Transforming_XML_with_XSLT/For_Further_Reading#Mailing_Lists.2FNewsgroups) + +### [Resources](cn/Transforming_XML_with_XSLT/Resources) + +### [Index](cn/Transforming_XML_with_XSLT/Index) + +### Original Document Information + +- Copyright Information: Copyright © 2001-2003 Netscape. All rights reserved. +- Note: This reprinted article was originally part of the DevEdge site.