Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

106
Vistas
Create XML attribute using XPath

I want to create an xml attribute but in order to find the element I want to add the query on, I need to use xpath. How can I do this?

Example =

const xmlText = `<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>
<book>
  <title id="somethingeng">Learning XML</title>
  <price>39.95</price>
</book>
</bookstore>`;

var doc = new DOMParser().parseFromString(xmlText,'text/xml');

var r = doc.evaluate("//*[@lang[contains(.,'eng')]]", doc, null, XPathResult.ANY_TYPE, null);

I want to create an attribute for this r;

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

First of all the xmlText string should be a template literals now that is has new lines.

evaluate() seamed fine, but the result of that is a XPathResult that needs to be iterated with XPathResult.iterateNext().

const xmlText = `<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>
<book>
  <title id="somethingeng">Learning XML</title>
  <price>39.95</price>
</book>
</bookstore>`;

var doc = new DOMParser().parseFromString(xmlText,'text/xml');

var r = doc.evaluate("//*[@lang[contains(.,'eng')]]", doc, null, XPathResult.ANY_TYPE, null);

var next = r.iterateNext();
while (next) {
  console.log(next.textContent);
  next = r.iterateNext();
}

Update

Based on the iterator you need to collect the nodes that you are interested in and then alter them afterwards. In the following I created functions that can create child elements and attributes based on a XPath expression and an object representing the new data.

const xmlText = `<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>
<book>
  <title id="somethingeng">Learning XML</title>
  <price>39.95</price>
</book>
</bookstore>`;

var doc = new DOMParser().parseFromString(xmlText, 'text/xml');

function addAttribute(doc, xpath, obj) {
  let r = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null);

  let nodes = [];
  let next = r.iterateNext();
  while (next) {
    nodes.push(next);
    next = r.iterateNext();
  }

  nodes.forEach(node => {
    Object.keys(obj).forEach(key => {
      let newattr = doc.createAttribute(key);
      newattr.value = obj[key];
      node.setAttributeNode(newattr);
    });
  });
}

function addChildNode(doc, xpath, obj) {
  let r = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null);

  let nodes = [];
  let next = r.iterateNext();
  while (next) {
    console.log(next.textContent);
    nodes.push(next);
    next = r.iterateNext();
  }

  nodes.forEach(node => {
    Object.keys(obj).forEach(key => {
      let newnode = doc.createElement(key);
      newnode.textContent = obj[key];
      node.appendChild(newnode);
    });
  });
}



addAttribute(doc, "//title[@lang[contains(.,'eng')]]", {"data-lang":"eng", index: 2});
addChildNode(doc, "//book[number(price) < 30]", {sale: true});

console.log(doc.documentElement.outerHTML);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Found the solution by using XPathResult.snapshotItem().

var xmlText = `<?xml version="1.0" encoding="ISO-8859-1"?>
  <bookstore>
    <book>
      <title lang="eng">Harry Potter</title>
      <price>29.99</price>
    </book>
    
    <book>
      <title id=\"somethingeng\">Learning XML</title>
      <price>39.95</price>
    </book>
    
  </bookstore>`;

var doc = new DOMParser().parseFromString(xmlText, 'text/xml');
var r = doc.evaluate("//*[@id[contains(.,'eng')]]", doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

var index = 0;
while (index < r.snapshotLength) {
  var next = r.snapshotItem(index);
  next.setAttribute("value", "val");
  index++;
}

var xmlSerializer = new XMLSerializer();
const updatedDoc = xmlSerializer.serializeToString(doc);

console.log(updatedDoc);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda