YouTip LogoYouTip

Met Document Getelementsbytagnamens

## XML DOM: getElementsByTagNameNS() Method The `getElementsByTagNameNS()` method returns a live `NodeList` of all elements with a specified local name and namespace URI, in the order in which they appear in the document. This method is essential when working with XML documents that utilize namespaces (such as XHTML, SVG, RSS, or custom XML schemas) to prevent naming collisions between elements. --- ## Syntax ```javascript var elements = document.getElementsByTagNameNS(namespaceURI, localName); ``` ### Parameters | Parameter | Type | Description | | :--- | :--- | :--- | | `namespaceURI` | String | The namespace URI of the elements to look for. Use `"*"` to match all namespaces. | | `localName` | String | The local name of the elements to look for (excluding the namespace prefix). Use `"*"` to match all local names. | ### Return Value * **Type:** `NodeList` * **Description:** A live, read-only collection of matching `Element` nodes. "Live" means that any changes made to the DOM tree will automatically update the collection. --- ## Code Example The following example demonstrates how to load an XML document (`books.xml`), dynamically append a namespaced element to each `` element, and then retrieve and display those elements using `getElementsByTagNameNS()`. ### Sample XML (`books.xml`) ```xml Everyday Italian Harry Potter ``` ### JavaScript Implementation ```javascript // Load the XML document var xmlDoc = loadXMLDoc("books.xml"); // Select all elements var books = xmlDoc.getElementsByTagName("book"); var newElement, newText; // Loop through each book and append a new element with a namespace for (var i = 0; i < books.length; i++) { // Create an element 'edition' with the namespace URI "http://www.example.com/publisher" (abbreviated as "p" in this context) newElement = xmlDoc.createElementNS("http://www.example.com/publisher", "p:edition"); newText = xmlDoc.createTextNode("First"); newElement.appendChild(newText); books.appendChild(newElement); } // Retrieve titles and the newly created namespaced edition elements var titles = xmlDoc.getElementsByTagName("title"); var editions = xmlDoc.getElementsByTagNameNS("http://www.example.com/publisher", "edition"); // Output the results for (var i = 0; i < titles.length; i++) { document.write(titles.childNodes.nodeValue); document.write(" - "); document.write(editions.childNodes.nodeValue); document.write(" edition."); document.write(" Namespace URI: "); document.write(editions.namespaceURI); document.write("
"); } ``` --- ## Key Considerations ### 1. Namespace URI vs. Prefix When using `getElementsByTagNameNS()`, you must query using the **absolute namespace URI** (e.g., `http://www.w3.org/2000/svg` or `http://www.example.com/publisher`), not the prefix (e.g., `svg:` or `p:`). The prefix is merely a shorthand alias defined in the document and can vary, whereas the URI remains constant. ### 2. Wildcard Matching You can pass `"*"` as a wildcard for either parameter: * `document.getElementsByTagNameNS("*", "paragraph")` matches all `` elements regardless of their namespace. * `document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "*")` matches all XHTML elements regardless of their local name. * `document.getElementsByTagNameNS("*", "*")` matches all elements in the document. ### 3. Live NodeList Performance Because the returned `NodeList` is live, it is automatically updated when the underlying DOM structure changes. Iterating over a live `NodeList` while modifying the DOM can lead to infinite loops or skipped elements if not handled carefully. If you need a static snapshot, consider converting the `NodeList` into a standard JavaScript array: ```javascript var elementsArray = Array.from(document.getElementsByTagNameNS(ns, name)); ```
← Dom Met Document RenamenodeDom Met Document Getelementsby β†’