aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/document/creatensresolver/index.html
blob: 001922bc5726a1b61306c30c59402da47dcd4a35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
---
title: Document.createNSResolver()
slug: Web/API/Document/createNSResolver
translation_of: Web/API/Document/createNSResolver
---
<p>{{ ApiRef("DOM") }}</p>

<p>创建一个 <code>XPathNSResolver</code> which resolves namespaces with respect to the definitions in scope for 指定节点</p>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="eval"><em>nsResolver</em> = document.createNSResolver(<em>node</em>);
</pre>

<h3 id="参数">参数</h3>

<ul>
 <li><code>node</code> is the node to be used as a context for namespace resolution.</li>
</ul>

<h3 id="返回值">返回值</h3>

<ul>
 <li><code>nsResolver</code> is an XPathNSResolver object.</li>
</ul>

<h2 id="Notes" name="Notes">注意</h2>

<p>Adapts any DOM node to resolve namespaces so that an <a href="/en/XPath" title="en/XPath">XPath</a> expression can be easily evaluated relative to the context of the node where it appeared within the document. This adapter works like the DOM Level 3 method <code>lookupNamespaceURI</code> on nodes in resolving the <code>namespaceURI</code> from a given prefix using the current information available in the node's hierarchy at the time <code>lookupNamespaceURI</code> is called. Also correctly resolves the implicit <code>xml</code> prefix.</p>

<p>Note, XPath defines QNames without prefix to match only elements in the null namespace. There is no way in XPath to pick up the default namespace as applied to a regular element reference (e.g., <code>p[@id='_myid'</code>] for <code><span class="nowiki">xmlns='http://www.w3.org/1999/xhtml'</span></code>). To match default elements in a non-null namespace, you either have to refer to a particular element using a form such as <code>*namespace-uri()=<span class="nowiki">http://www.w3.org/1999/xhtml</span> and name()=p[@id='_myid']</code> (<a href="/en/Introduction_to_using_XPath_in_JavaScript#Using_XPath_functions_to_reference_elements_with_its_default_namespace" title="en/Introduction_to_using_XPath_in_JavaScript#Using_XPath_functions_to_reference_elements_with_its_default_namespace">this approach</a> works well for dynamic XPath expressions where the namespaces might not be known) or use prefixed name tests, and create a namespace resolver mapping the prefix to the namespace. Read more on <a href="/en/Introduction_to_using_XPath_in_JavaScript#Implementing_a_User_Defined_Namespace_Resolver" title="en/Introduction_to_using_XPath_in_JavaScript#Implementing_a_User_Defined_Namespace_Resolver">how to create a user defined namespace resolver</a> if you wish to take the latter approach.</p>

<p><code>createNSResolver</code> was introduced in DOM Level 3.</p>

<h2 id="See_also" name="See_also">参见</h2>

<ul>
 <li><a href="/en/DOM/document.evaluate" title="en/DOM/document.evaluate">document.evaluate</a></li>
 <li><a href="/en/Introduction_to_using_XPath_in_JavaScript" title="en/Introduction_to_using_XPath_in_JavaScript">Introduction to using XPath in JavaScript</a></li>
</ul>

<h2 id="Specification" name="Specification">规范</h2>

<p><a class="external" href="http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator-createNSResolver">DOM Level 3 XPath Specification: createNSResolver</a></p>