When using the iterateNext method with document.evaluate, a null value is

I have been experimenting with XPath to navigate through my XML document by following examples from W3Schools. However, I am facing an issue where the iterateNext() function returns null every time. Here is a snippet from my blog.xml file:

<blog
xmlns ="http://www.w3schools.com" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="blogschema.xsd">
<Title>My blog</Title>

<Entry>
    <Heading id="101">week1</Heading>   
    <body>
        <text>enter text right here</text>
        <pictures>pictures in the body</pictures>

    </body>
    <labels>Seperate labels with commas</labels>
    <date> 20121119</date>

</Entry>

</blog>

Despite referring to W3Schools for guidance, my HTML script seems to be encountering issues as the while statement is never triggered due to the result always being null. It appears that there might be something missing or incorrectly implemented on my end, even though I assumed the provided example should work flawlessly.

xmlDoc = loadXMLDoc("blog.xml"); // loads the XML file  
// loadXmlContent(xmlDoc); using XML DOM

path = "/blog/Title";
if (document.implementation && document.implementation.createDocument)
{
    var nodes = xmlDoc.evaluate(path, xmlDoc, null, 5, null);
    alert(nodes);
    var result = nodes.iterateNext();

    while (result)
    {
        document.write(result.childNodes[0].nodeValue);
    }
}
</script>

Answer №1

Remember to consider the default namespace declaration xmlns="http://www.example.com" in the given input when working with it, as shown below:

var elements = xmlDoc.evaluate("df:blog/df:Title", xmlDoc, function(prefix) { if (prefix === "df") return "http://www.example.com"; }, 5, null);

var output;

while ((output = elements.iterateNext()) != null)
{
  document.write(output.textContent);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

View the edited image preview instantly upon selecting the image

I have selected an image and previewed it before submitting the form. However, now I wish to be able to edit the file immediately after selecting it, preview the changes, and then submit the file. <input type ="file" accept="image/*" id="image" name="i ...

How can you achieve the effect of "hovering over an image to automatically start playing a muted video within the image itself"?

[![enter image description here][1]][1]I am working with WordPress and Elementor, and I want to create a hover effect where an image triggers a muted video to play within the image area on mouseover. The video should stop playing when the mouse is not hove ...

What is the reason that dates are not exported correctly in Laravel using jQuery data table when the month is two digits and the file format is EXCEL?

I am trying to export data as an EXCEL file from the view, everything is working fine except for the date format. Here is the code I have tried: $('#get_hourly_report').click(function () { var id = $('#teacher').val(); ...

Are there any React-UI libraries that come with a stylish green color scheme?

Currently working on a small React site and deciding on a UI library. While I do like Material UI and Bootstrap, I find that they have limited default themes available. I am looking for libraries with green-color-based themes or ones that make it easy to ...

Is there a way to initiate multiple AJAX requests once a single request has been completed successfully?

I have created two functions and I want to call the get_name_from_id function when my first ajax function is successful. However, it is not working as expected. When I add an alert in the get_name_from_id function after the jsonString variable like alert ...

The process of sending parameters to an API in ReactJS: a guide

My objective is to target .....the values originating from login (can be anything)-> for example email:'[email protected]' password:'12345678' I am required to extract the username until "@" and use it as a username i ...

Tips for effectively closing div elements

How can I modify this accordion to close when a user clicks on another link, and also change the image of the open "p" tag? Currently, clicking on a link toggles the content and changes the image. However, what I am struggling with is closing the previousl ...

Vue.js SyntaxError: Identifier came out of nowhere

An error was reported by VUE debug mode in this line of my code: :style="{transform : 'translate3d(' + translateX + 'px,0, 0)'}"\ The documentation does not provide instructions on how to include a variable within a style binding ...

Retrieving information from MongoDB for a specific ObjectID associated with the current authenticated user

Having two collections in my MongoDB structured as follows: Data User: id: ObjectId ("5fb39e3d11eaad3e30cfb1b0") userName: "Tobias" password: "yyy" id: ObjectId ("5fb3c83fb774ff3340482250") userName: "Thor&qu ...

Looking to switch classes based on a value exceeding 0?

I am aiming to use the "toggleClass" function when the numeric value within the span.points is greater than zero. This means that if the value is null, no class will be added, but if it changes to 1, a class gets added. I am still learning jQuery at a slow ...

Why am I unable to alter the variable value within the callback event?

Exploration of Request Handling POST request To /api/endpoint headers: standard body: data=test123 POST response From /api/endpoint headers: standard body: data=test123 expectation(NOT MET): Show the request body content in the console. reality(FRU ...

What is the most popular method for namespacing AngularJS modules?

I am new to AngularJS and currently exploring different ways to namespace modules in my application. One challenge I face is the need to integrate my Angular app into a designated placeholder div within a third-party website (which may also use Angular), ...

The functionality of the page becomes disrupted when multiple timers are used after the setInterval function is executed in

My webpage fetches multiple data for a table, with one of the columns displaying date and time values from which countdown timers run. Each row has its own timer and functions correctly. However, when I added JavaScript code to refresh the table every 10 ...

unexpected behavior with two-way data binding in Angular

After carefully studying the Angular documentation, I decided to test a basic data binding concept. Below is an example of the HTML file: <body ng-app> <h1>Hello, world!</h1> <div ng-controller="Ctrl"> <input type="text ...

Rendering Highcharts React Pie Chart Multiple Times

Here is the code snippet: import React, { useEffect, useRef, useState } from "react"; import * as Highcharts from "highcharts"; import HighchartsReact from "highcharts-react-official"; export const PieChart = (props: any) =&g ...

Running a Custom Tab Component in a React Application

I am currently facing an issue with my React app that has multiple tabs. When I click on a specific tab, I want only that tab to render, but currently all tabs are rendering when I click on one. I have used console.log to confirm that all tabs are indeed r ...

Editing an XML file on the browser and saving it in its original location on the local file system

Seeking assistance with editing an XML file directly from the browser on a local file system and saving it back to its original location. I have conducted extensive research using Google, but have not been able to find a suitable solution. Any help or gu ...

Error: Comparison of two objects cannot be performed in Node.js due to AssertionError

Utilizing the functions below to retrieve a value from the application and compare it with the expected value. However, encountering issues with the output displayed. Seeking assistance in resolving this matter. getEleAttribute = async function(ele, attr) ...

Tips for Resolving SyntaxError: Unexpected token = When Class Import is Done

After creating a class in a separate JavaScript file and importing it into another file, I encountered a syntax error within the exported class. All seemed fine when the class was not being exported. Here is the code for Property.js (the exported class): ...

Retrieving data from a subcollection in a cloud firestore database does not yield any results

In my Next.js application, I am utilizing Cloud Firestore database to store user documents. The structure of the collection path is as follows: collection "userDocs" └─ document "userEmail" └─ collection "docs" └─ document "document ...