Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - English


xmlXPath

Make a XPath query on a XML document

Syntax

result = xmlXPath(xmlObj, queryStr)
result = xmlXPath(xmlObj, queryStr, namespaces)

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".

For more information about XPath, you can read the W3C recommendation.

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);
for i=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) then
    xmlDump(xp(1))
end

// Retrieve the nodes with an attribute num equal to the number 5
xp = xmlXPath(doc, "//*[number(@num)=5]");
s = size(xp);
if (s(2) <> 0) then
    xmlDump(xp(1))
end

// Get the name and the content of all the attributes of nodes named 'emph'
xp = xmlXPath(doc, "//emph/@*");
xp.name
xp.content

xmlDelete(doc);

// Query using namespaces
t = "<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 a
xmlXPath(doc, "//a") // => nothing
xmlXPath(doc, "//scilab:a", ["scilab" "https://www.scilab.org"]) // => OK

// This code will fail:
// xmlXPath(doc, "//scilab:a") // => error

xmlDelete(doc);

// Query starting on an element
t = "<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 e
xp = xmlXPath(e, "@*");
xmlAsText(xp)

// Get the 'b' from e
xp = xmlXPath(e, "b");
xmlAsText(xp)

xmlDelete(doc);

Getting the XML comments:

doc = xmlRead(SCI+"/modules/ui_data/etc/newsfeed.xml");
c = xmlXPath(doc, "//comment()");   // do not forget the trailing ()
c.content'

xmlDelete(doc);
--> c.content'
 ans  =
! RSS Feed URL                                                        !
! automatic news change interval time (in ms), set to -1 to disable   !
! periodic feed update interval time (in ms), set to -1 to disable    !

History

VersionDescription
5.4.0 XML module introduced.
Report an issue
<< xmlWrite XML Management HDF5 files >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 24 11:13:09 CEST 2024