Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create XPATH for a HTML DOM element?

Tags:

html

dom

xpath

How to create XPATH for a HTML DOM element? for example, "/HTML/BODY/DIV[1]/TABLE[1]/TR[2]/TD[1]/INPUT".

Given an DOM element how to get this XPATH string? Any ideas?

Thanks,

Dattebayo.

like image 936
dattebayo Avatar asked Aug 26 '09 08:08

dattebayo


People also ask

Does XPath use DOM?

The XPath DOM extends the document order of the DOM Core to include the XPathNamespace nodes. Element nodes occur before their children. The attribute nodes and namespace nodes of an element occur before the children of the element. The namespace nodes are defined to occur before the attribute nodes.

How do I get XPath in HTML?

When you click an element in a web page, it's related code will be highlighted in the Firebug panel at the bottom of the window. Right-click this highlighted code. Select “Copy XPath” from the menu. This will copy the element's XPath information to your clipboard.

What is XPath in DOM?

XPath stands for XML Path Language. It uses a non-XML syntax to provide a flexible way of addressing (pointing to) different parts of an XML document. It can also be used to test addressed nodes within a document to determine whether they match a pattern or not.


1 Answers

You can create a new domdocument and then import the node element

$DD= new DOMDocument('1.0', 'utf-8');
$DD->loadXML( "<html></html>" );
$DD->documentElement->appendChild($DD->importNode($DE,true));

then you can use xpath insithe the domelement:

$xpathe=new DOMXPath($DD);
like image 188
Sergi Mayordomo Avatar answered Oct 02 '22 15:10

Sergi Mayordomo