In Firefox, xPath is failing to evaluate

I am encountering an issue with the document.evaluate function when attempting to validate xPath in Firefox. The document.createNSResolver function seems to be malfunctioning as I only receive xmlDoc and nothing else. Even when I try leaving it with a null value in evaluate, it still does not work. Strangely, Edge, Opera, and Chrome do not have this problem as everything works smoothly on those browsers. I have been searching for a solution but have been unsuccessful so far...

My main function is structured like this:

const checkXPathVisiblity = (xPathValid?: string) => {
if (!xPathValid) return false;

try {
  const parsedXPathValid = _.unescape(xPathValid);
  const parser = new DOMParser();
  const xmlDoc = parser.parseFromString(xml, 'text/xml');
  const resolver = document.createNSResolver(xmlDoc);
  const result = document.evaluate(parsedXPathValid, xmlDoc, resolver);

  return !!result.booleanValue;
} catch (error) {
  return false;
}
};

Where:

xPathValid:

 number(Dokument/F0002x2) = 1

xml:

<?xml version="1.0" encoding="utf-8"?>
<Dokument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<F0001x1 id="F0001x1"></F0001x1>
<F0002x2 id="F0002x2">1</F0002x2>
</Dokument>

Answer №1

Using plain JavaScript, I have encountered no issues with Firefox. Check out the code snippet below:

const xmlSource = `<?xml version="1.0" encoding="utf-8"?>
<Dokument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<F0001x1 id="F0001x1"></F0001x1>
<F0002x2 id="F0002x2">1</F0002x2>
</Dokument>`;

const xmlDoc = new DOMParser().parseFromString(xmlSource, 'application/xml');

const xpathResult = xmlDoc.evaluate('number(Dokument/F0002x2) = 1', xmlDoc);

console.log(xpathResult.booleanValue);

You can also check this alternative approach by creating a resolver:

const xmlSource = `<?xml version="1.0" encoding="utf-8"?>
<Dokument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<F0001x1 id="F0001x1"></F0001x1>
<F0002x2 id="F0002x2">1</F0002x2>
</Dokument>`;

const xmlDoc = new DOMParser().parseFromString(xmlSource, 'application/xml');

const xpathResult = xmlDoc.evaluate('number(Dokument/F0002x2) = 1', xmlDoc, xmlDoc.createNSResolver(xmlDoc));

console.log(xpathResult.booleanValue);

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

Importing Typescript modules by specifying their namespace instead of using a function

I have been working on a project where I needed to generate typings from graphql using the gql2ts library. In the gql-2-ts file, I initially used a namespace import for glob, which resulted in TypeScript showing me an error as intended. I then switched the ...

Angular application that features a material table created using *ngFor directive and displayedColumns defined as an array

I am trying to create a table that displays columns with the format {key: string, display: string} where 'display' is the header and 'key' is used to display the value. <ng-container *ngFor="let col of displayedColumns"> ...

Type children are not permitted in the TypeScript container

Container is a component that extends from @material-ui/core and does not accept children of type React.ReactNode. Layout.tsx: import { Container } from "@material-ui/core"; type LayoutProps = { children: React.ReactNode; }; function Layout( ...

Connecting nested xpaths in Selenium webdriver assert can be achieved by specifying the unique xpaths for each element, such as the label and separate button

I am currently working on validating the disabled state of a button on our webpage. Below is the HTML code I am dealing with: <div data-persona-noeditable="" style="display: block;"> <div class="c-potential-tooltip persona-name pull-left" dat ...

Is there a way to identify when a user is returning to a previous page in Angular2

How can I detect if a user has pressed the back button in their browser to navigate back while using Angular? Currently, I am subscribing to router events to achieve this. constructor(private router: Router, private activatedRoute: ActivatedRoute) { ...

Is there a way to incorporate my getter into a computed property?

My Vuex Store is built using Vuex module decorators and I am facing an issue with using a getter for a computed property. Here is my code: @Module export default class WorkoutModule extends VuexModule { _workout: Workout; @Mutation startWork ...

The button fails to log any text to the developer console

Attempting to verify the functionality of my button by logging a message on the developer console. However, upon clicking the button, the text does not appear in the console. import { Component, EventEmitter, Input, Output } from '@angular/core'; ...

I'm encountering an issue trying to apply array filtering with checkboxes using React hooks and TypeScript

Help needed! I'm facing an issue while trying to filter an array based on gender using checkboxes. The functionality works fine for the male checkbox but seems to fail when clicking on the female checkbox. Below is the code snippet from my App.tsx fil ...

Updating Select Options Disabled/Enabled in Angular 2

In my Angular2 project, I have 2 select elements: <div ng-controller="ExampleController"> <form name="myForm"> <label for="companySelect"> Company: </label> <select name="companySelect" id= ...

Here is an example showcasing how to use Angular 2 to make an

How can I correctly retrieve json data from an http get request in Angular 2? Currently, I am working on testing some local data with a mocked endpoint. Although I am able to see the result in the http.get() method, I am facing issues when trying to assign ...

Importing BrowserAnimationsModule in the core module may lead to dysfunctional behavior

When restructuring a larger app, I divided it into modules such as feature modules, core module, and shared module. Utilizing Angular Material required me to import BrowserAnimationsModule, which I initially placed in the Shared Module. Everything function ...

Tips for resolving these dependency issues

After updating my Angular and my Angular project version to Angular 7, I encountered an issue when trying to run it: The package "@angular/compiler-cli" has an incompatible peer dependency with "typescript" (requires ">=3.1.1 <3.2", but would instal ...

Enhance VSCode Intellisense feature to unwrap TypeScript types

Consider the following scenario: type X = Blob | File; function printX(x: X) { console.log(x); } When using VSCode intellisense and calling the function, I am prompted with: printX(x: X): void The issue arises when I'm unsure of what type X rep ...

Exploring NuxtJS Vuex Module namespaces and mutation enumerations

My NuxtJS website has a Vuex store with a root state and a module located at store/shop/cart/state.ts. Within the module, there is a file called store/shop/cart/mutations.ts. import { MutationTree } from 'vuex'; import { CartState } from './ ...

Yes, it's not able to retrieve the value from headlessui combobox

I have encountered an issue while using the Headlessui combobox component in conjunction with Yup. Despite successfully storing the selected value in the selectedMemory state variable, Yup consistently generates a required error message. I seem to be overl ...

What is the best approach for conducting unit tests on model interfaces with TypeScript?

export interface Person { fullName: string; } What is the best way to create unit tests for the above interface and ensure that Karma includes it in the code coverage report? I attempted to assert properties by creating an object, but it seems that K ...

The base class is invoking a function from its child class

There are two classes, a base class and a derived one, each with an init function. When constructing the derived class, it should: Call its base constructor which: 1.1. Calls its init function Call its own (derived) init function. The issue is that ...

Mastering the implementation of type refinements for io-ts in processing input data

I have implemented io-ts for defining my typescript types. This allows me to perform runtime type validation and generate type declarations from a single source. In this particular scenario, I aim to create an interface with a string member that needs to ...

Initially, when an iframe is loaded in Angular 10, it may display a 404 error page

Hey there! I'm currently using the HTML code below to incorporate an iframe and display an external page hosted on the same domain so no need to worry about cross domain issues: <iframe frameborder="0" [src]="url"></iframe ...

Encountering a Login Issue with Firebase Google Authentication in Angular 16

I am currently working on implementing a Google sign-in/login feature in Angular 16 using Firebase. However, when I try to click the "LogIn" button, I encounter the following error: "ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of ...