如何在 matlab 中的 XPath 表达式中使用动态字符串变量

发布于 2025-01-16 13:24:26 字数 3103 浏览 0 评论 0原文

我正在尝试使用 Xpath 在 matlab 中查找 XMl 文档的特定节点(概念)的子节点。 我使用下面的代码我得到了 5 个孩子,这是真的。

expression  = xpath.compile('//concept\[@name="con1"\]/\*');
Childs      = expression.evaluate(xDoc, XPathConstants.NODESET);

但对于我的项目,我必须以动态方式使用每个概念的属性“名称”的字符串值,因此我将它们存储在向量中以便一一计算它们。 例如,ConceptName(1)="con1",但是,当我执行以下代码时,我得到零个子项:

expression = xpath.compile('//concept\[@name="ConceptName(1)"\]/\*');
Childs     = expression.evaluate(xDoc, XPathConstants.NODESET);

如果有人可以帮助我将字符串变量调用到路径表达式我将非常感激。 先感谢您。

这是我的 XML 文档的样子,我想要的输出应该是四个概念的列表(该概念的第一个子概念的名称为“con1”),但我必须动态提取父概念的名称,因为结构应该是未知的。

<?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>

这是我的代码

% 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
                                     

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
                                     

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任性一次 2025-01-23 13:24:26

表达式 @name="ConceptName(1)" 不会选择任何内容,因为您没有任何 name 属性值为“ConceptName(1)”的元素。

很难知道如何更正您的代码,因为您并没有真正告诉我们您认为它可能意味着什么。你说你将属性名称存储在“向量中”,但 XPath 中没有向量这样的东西,所以我真的不知道你做了什么或你想要实现什么。

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.

红衣飘飘貌似仙 2025-01-23 13:24:26

我的猜测是您希望将以下行替换

expression = xpath.compile('//concept\[@name="ConceptName(1)"\]/\*')

为类似以下内容:

expression = xpath.compile('//concept\[@name="' + ConceptName(1) + '"\]/\*')

请注意,仅当 ConceptName 是字符串数组类型(带双引号)而不是 char 向量(单引号)时,这才有效。

另请注意,无需转义字符串中的方括号和星号:

expression = xpath.compile('//concept[@name="' + ConceptName(1) + '"]/*')

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) + '"]/*')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文