Using Xpath with PHP to parse HTML

My suggestion is to always use DOMDocument as opposed to SimpleXML, since it’s a much nicer interface to work with and makes tasks a lot more intuitive. The following example shows you how to load the HTML into the DOMDocument object and query the DOM using XPath. All you really need to do is find … Read more

Sending XPath a variable from Java

You can define a variable resolver and have the evaluation of the expression resolve variables such as $myvar, for example: XPathExpression expr = xpath.compile(“//doc[contains(., $myVar)]/*/text()”); There’s a fairly good explanation here. I haven’t actually done this before myself, so I might have a go and provide a more complete example. Update: Given this a go, … Read more

XPath and TXmlDocument

I can’t find anything in the TXMLDocument documentation about XPath. XML example, from the OmniXML XPath demo: <?xml version=”1.0″ encoding=”UTF-8″?> <bookstore> <book> <title lang=”eng”>Harry Potter</title> </book> <book> <title lang=”eng”>Learning XML</title> </book> <book> <title lang=”slo”>Z OmniXML v lepso prihodnost</title> <year>2006</year> </book> <book> <title>Kwe sona standwa sam</title> </book> </bookstore> Try something like this: uses XMLDoc, XMLDom, XMLIntf; … Read more

Why doesn’t xpath work when processing an XHTML document with lxml (in python)?

The problem is the namespaces. When parsed as XML, the img tag is in the http://www.w3.org/1999/xhtml namespace since that is the default namespace for the element. You are asking for the img tag in no namespace. Try this: >>> tree.getroot().xpath( … “//xhtml:img”, … namespaces={‘xhtml’:’http://www.w3.org/1999/xhtml’} … ) [<Element {http://www.w3.org/1999/xhtml}img at 11a29e0>]

How to update XML using XPath and Java

Use setNodeValue. First, get a NodeList, for example: myNodeList = (NodeList) xpath.compile(“//MyXPath/text()”) .evaluate(myXmlDoc, XPathConstants.NODESET); Then set the value of e.g. the first node: myNodeList.item(0).setNodeValue(“Hi mom!”); More examples e.g. here. As mentioned in two other answers here, as well as in your previous question: technically, XPath is not a way to “update” an XML document, but … Read more