Is xpath capable to replace values of a xml doc?
The following gets all name elements, but how could I replace them?
<accounts>
<account>
<name>Johndoe1<name>
<account>
<account>
<name>Johndoe2<name>
<account>
</account>
XPathExpression fax = xpath.compile("/accounts/account/name")
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
No, XPath is a query language for XML documents. As such it only extracts node and data from XML documents, but cannot alter (add, replace, delete, a node) the XML document.
When the processing of an XML document results in the creation of another XML document that is different from the original, this is called XML transformation. XSLT is a language especially designed for XML transformations and is the most natural choice for such processing.