Dieser Inhalt wurde automatisch aus dem Englischen übersetzt, und kann Fehler enthalten. Erfahre mehr über dieses Experiment.

View in English Always switch to English

Knoten: lookupNamespaceURI() Methode

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since Juli 2015.

Die lookupNamespaceURI() Methode des Node Interfaces nimmt ein Präfix als Parameter und gibt die Namespace-URI zurück, die damit auf dem gegebenen Knoten verknüpft ist (und null, wenn nicht gefunden). Das Vorhandensein dieser Methode ermöglicht es, Node-Objekte als Namespace-Resolver an XPathEvaluator.createExpression() und XPathEvaluator.evaluate() zu übergeben.

Syntax

js
lookupNamespaceURI(prefix)

Parameter

prefix

Das zu suchende Präfix. Der leere String ist gleichbedeutend mit null, was auf den Standard-Namespace verweist.

Hinweis: Dieser Parameter ist nicht optional, kann aber auf null gesetzt werden.

Rückgabewert

Ein String, der die Namespace-URI enthält, die dem Präfix entspricht.

  • Gibt immer null zurück, wenn der Knoten ein DocumentFragment, DocumentType, ein Document ohne documentElement oder ein Attr ohne zugehöriges Element ist.
  • Wenn prefix "xml" ist, ist der Rückgabewert immer "http://www.w3.org/XML/1998/namespace".
  • Wenn prefix "xmlns" ist, ist der Rückgabewert immer "http://www.w3.org/2000/xmlns/".
  • Wenn das prefix null ist, ist der Rückgabewert die Standard-Namespace-URI.
  • Wenn das Präfix nicht gefunden wird, ist der Rückgabewert null.

Beispiel

Hinweis: Dieses Beispiel läuft in einem HTML-Dokument, in dem xmlns:-Attribute ignoriert werden (außer xmlns:xlink). Firefox setzt alle Namespace-URIs der Elemente auf null, während Chrome und Safari die Standard-Namespace-URIs von HTML-, SVG- und MathML-Elementen korrekt setzen. Wenn Sie aussagekräftigere Tests durchführen möchten, können Sie ein eigenständiges SVG Dokument öffnen und Skripte in dessen Kontext ausführen.

html
<div class="hidden">
  <div>Test HTML element</div>
  <svg>
    <text>Test SVG element</text>
  </svg>
  <svg xmlns:xlink="http://www.w3.org/1999/xlink" id="with-xlink">
    <text>Test SVG element with xlink</text>
  </svg>
  <math>Test MathML element</math>
</div>

<table>
  <thead>
    <tr>
      <th><code>prefix</code></th>
      <th><code>&lt;div&gt;</code></th>
      <th><code>&lt;svg&gt;</code></th>
      <th><code>&lt;svg xmlns:xlink&gt;</code></th>
      <th><code>&lt;math&gt;</code></th>
    </tr>
  </thead>
  <tbody></tbody>
</table>
js
const htmlElt = document.querySelector("div");
const svgElt = document.querySelector("svg");
const svgEltXLink = document.querySelector("#with-xlink");
const mathElt = document.querySelector("math");

const tbody = document.querySelector("tbody");

for (const prefix of ["xmlns", "xml", "html", "svg", "xlink", "", null]) {
  const row = document.createElement("tr");
  tbody.appendChild(row);
  row.appendChild(document.createElement("td")).textContent =
    JSON.stringify(prefix);
  for (const el of [htmlElt, svgElt, svgEltXLink, mathElt]) {
    console.log(el, prefix, el.lookupNamespaceURI(prefix));
    row.appendChild(document.createElement("td")).textContent = String(
      el.lookupNamespaceURI(prefix),
    );
  }
}

Spezifikationen

Specification
DOM
# dom-node-lookupnamespaceuri

Browser-Kompatibilität

Siehe auch