XPath query with default namespace in XML file

24 次查看(过去 30 天)
Matlab introduced a MATLAB API for XML Processing (I believe in 2021a).
I am using this API with XPath expressions to extract data from XML files. However, this fails when the XML file has a default namespace registered.
For example with this XML file test.xml:
<books xmlns="http://www.contoso.com/books" xmlns:i="http://www.contoso.com/currency">
<book>
<title>Title</title>
<author>Author Name</author>
<i:price>5.50</i:price>
</book>
</books>
Using the following snippet:
import matlab.io.xml.dom.*
import matlab.io.xml.xpath.*
xmlFilePath = "path\to\test.xml";
xmlDom = parseFile(Parser, xmlFilePath);
xpathPrice = "//i:price";
xpathAuthor = "//author";
res = evaluate(Evaluator, xpathPrice, xmlDom)
works for xpathPrice but fails for xpathAuthor (in the sense that it returns an empty result instead of the author node from the example).
Browsing through the different options, I could not come up with a solution. I am working on Matlab 2021b.
I know that I could use getElementsByTagName but I do require the functionality provided by XPath to identify nodes.
Are there any hints on how I could get that working?
Thanks!

采纳的回答

Sakshay
Sakshay 2022-11-29
Hello Chris,
As per my understanding, you are trying to access elements of an XML file, using XPath. But it seems that the elements with a default namespace having no prefix are not being captured.
This problem here seems to be with how XPath works, rather than in the MATLAB API.
Elements without a prefix, can be accessed using the "local-name()" attribute. As an example, for your case, the query would look like:
xPathAuthor = '//*[local-name()="book"]';
res = evaluate(Evaluator, xPathAuthor, xmlDom);
For information, on the XPath Evaluator Package, you can refer to the following documentation:
  1 个评论
chris
chris 2022-12-2
Hi,
thanks for your reply. After some more research, it looks like you are right and this is some weird behavior of XPath. Personally, I find the solution not very satisfying but that's how it is.
For interested people, another workaround would be to use a default prefix (as described here: Abstract base class for namespace prefix resolvers). Something along these lines
import matlab.io.xml.xpath.*
evalObj = Evaluator();
evalObj.PrefixResolver = myPrefixResolver;
xpathAuthor = "//def:author";
res = evaluate(evalObj, xpathAuthor, xmlDom);
with the custom prefix resolver:
classdef myPrefixResolver < matlab.io.xml.xpath.PrefixResolver
methods
function url = getNamespaceForPrefix(resolver,prefix)
if prefix == "i"
url = "http://www.contoso.com/currency";
else
url = "http://www.contoso.com/books";
end
end
end
end
The disadvantage is, that one has to know the namespace URLs beforehand.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structured Data and XML Documents 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by