xmlXPath

Calling Sequence

Arguments

xmlObj

a XML mlist typed XMLDoc or XMLElem

queryStr

a Xpath query

namespaces

An optional matrix nx2 of strings

result

result can be a set of XMLElements or a number or a string or a boolean

Description

Make a XPath query on a document or in starting on an element. If you need to use namespaces, then you must define them in using the optional argument. XML namespaces are defined in the first tag with the keyword "xmlns".

Examples

doc=xmlRead("http://www.w3.org/TR/2009/REC-xml-names-20091208/xml-names-10-3e.xml");// Retrieve the nodes with name equal to "note"xp=xmlXPath(doc,"//note");s=size(xp);fori=1:s(2)xmlDump(xp(i))end// Count the nodes with name equal to "note"xp=xmlXPath(doc,"count(//note)")// Retrieve the node with id="Philosophy"xp=xmlXPath(doc,"//*[@id=""Philosophy""]");s=size(xp);if(s(2)<>0)thenxmlDump(xp(1))end// Retrieve the nodes with an attribute num equal to the number 5xp=xmlXPath(doc,"//*[number(@num)=5]");s=size(xp);if(s(2)<>0)thenxmlDump(xp(1))end// Get the name and the content of all the attributes of nodes named 'emph'xp=xmlXPath(doc,"//emph/@*");xp.namexp.contentxmlDelete(doc);// Query using namespacest="<root xmlns:scilab=""http://www.scilab.org"">"+.."<scilab:a att=""foo"" rib=""bar"">"+.."<b>Hello</b></scilab:a></root>"doc=xmlReadStr(t);// We search for element named axmlXPath(doc,"//a")// => nothingxmlXPath(doc,"//scilab:a",["scilab""http://www.scilab.org"])// => OK// This code will fail:// xmlXPath(doc, "//scilab:a") // => errorxmlDelete(doc);// Query starting on an elementt="<root att=""attribute""><a a1=""A1"" a2=""A2"" a3=""A3"">"+.."<b>Hello</b><c>Scilab</c><b>World</b></a><b>Nothing</b></root>"doc=xmlReadStr(t);e=doc.root.children(1);// Get the attributes of exp=xmlXPath(e,"@*");xmlAsText(xp)// Get the 'b' from exp=xmlXPath(e,"b");xmlAsText(xp)xmlDelete(doc);