I'm trying to find the children of a specific node (concept) of an XMl document in matlab using Xpath. I used the following code I get 5 children which is true.
expression = xpath.compile('//concept\[@name="con1"\]/\*');
Childs = expression.evaluate(xDoc, XPathConstants.NODESET);
But for my project I have to use the string values of the attributes "name" of each concept in dynamic manner, so I stored them in vector in order to cal them one by one.
For example, ConceptName(1)="con1"
, however, when I execute the following code, I get zero children:
expression = xpath.compile('//concept\[@name="ConceptName(1)"\]/\*');
Childs = expression.evaluate(xDoc, XPathConstants.NODESET);
If there is someone who can help me to call the sting variables to the path expression I would be very grateful. Thank you in advance.
Here is how my XML doc look like, My desired outpout whould be a list of four concepts (the first children of the concept which has the name="con1"), but I must extract the name of the parent concept dynamicly because the structure whould be unkowen.
<?xml version="1.0" encoding="UTF-8"?>
<taxonomy>
<concept name="con1">
<concept name="con11">
<concept name="con1033990258">
<concept name="con271874239">
<concept name="con1657241849">
<concept name="con1448945150">
<instance name="inst686829093"/>
<instance name="inst1379512917"/>
<instance name="inst2072196703"/>
</concept>
</concept>
</concept>
</concept>
<concept name="con12"> </concept>
<concept name="con13"></concept>
<concept name="con14"></concept>
</concept>
</taxonomy>
This is my code
% get the xpath mechanism into the workspace
import javax.xml.xpath.*
factory = XPathFactory.newInstance;
xpath = factory.newXPath;
% read the XML file
filedir = 'C:\Users\Asus\Documents\Asma\MatlabCode\Contribution2\WSC2009_XML'; %location of the file
files = dir(fullfile(filedir, '*.xml'));
xDoc = xmlread(fullfile(filedir, files(1).name)); % read the XML doc but return "[#document: null]". The xmlread function returns a Java object that represents the file's Document Object Model, or DOM. The "null" is simply what the org.apache.xerces.dom.DeferredDocumentImpl's implementation of toString() dumps to the MATLAB Command Window
XDocInMatlab = xmlwrite(xDoc); % show the XML file
taxonomy = xDoc.getElementsByTagName('taxonomy'); %% get the root elment
concepts = xDoc.getElementsByTagName('concept'); %% get the concept elemnt node
concept_Matrix = strings(concepts.getLength,1);
for i = 0 : concepts.getLength-1
conceptName = string(concepts.item(i).getAttribute('name'));
concept_Matrix(i 1,1) = conceptName;
if concepts.item(i).hasChildNodes
expression = xpath.compile('//concept[@name=conceptName]/*');
Childs = expression.evaluate(xDoc, XPathConstants.NODESET);
% Iterate through the nodes that are returned.
for j = 0:Childs.getLength-1
ChildsName(j 1) = char(Childs.item(j).getAttribute('name'));
end
end
end
CodePudding user response:
The expression @name="ConceptName(1)"
doesn't select anything because you don't have any elements whose name attribute has the value "ConceptName(1)".
It's hard to know how to correct your code because you don't really tell us what you thought it might mean. You say you stored the attribute names "in a vector" but there's no such thing as a vector in XPath, so I really don't know what you did or what you are trying to achieve.
CodePudding user response:
My guess is that you want to replace the following line
expression = xpath.compile('//concept\[@name="ConceptName(1)"\]/\*')
with something like this:
expression = xpath.compile('//concept\[@name="' ConceptName(1) '"\]/\*')
Note that this works only if ConceptName
is a string array type (with double quotes), not a char vector (single quotes).
Note also that it is not necessary to escape square brackets and asterisks in strings:
expression = xpath.compile('//concept[@name="' ConceptName(1) '"]/*')