在 Complex XML 中使用 Xpath 读取父节点的父节点
Read a parent's parent node with Xpath in Complex XML
我需要读取一个复杂的 XML 文件,我需要检索每个名为 "Disorder" 的节点的特定父节点...让我展示 xml 文件:
<ClassificationNode>
<Disorder id="14879">
<OrphaNumber>101943</OrphaNumber>
<ExpertLink lang="en">
http://www.orpha.net/consor/cgi-bin/OC_Exp.php?lng=en&Expert=101943
</ExpertLink>
<Name lang="en">Rare hepatic and biliary tract tumor</Name>
</Disorder>
<ClassificationNodeChildList count="3">
<ClassificationNode>
<Disorder id="21130">
<OrphaNumber>300557</OrphaNumber>
<ExpertLink lang="en">
http://www.orpha.net/consor/cgi-bin/OC_Exp.php?lng=en&Expert=300557
</ExpertLink>
<Name lang="en">Carcinoma of the ampulla of Vater</Name>
</Disorder>
<ClassificationNodeChildList count="0"></ClassificationNodeChildList>
</ClassificationNode>
每个标签 Disorder 都有一个 Disorder 父项,在这种情况下,Disorder 名称 "Carcinoma of the ampulla of Vater" 它是 Disorder "Rare hepatic and biliary tract tumor" 的子项。
我试图在 PHP 中使用 XPath 检索这些值,这是我的代码:
$parent = $simplexml->xpath("../../Disorder/Name");
但是数组对我来说达到了零....我用其他 xpath 语法尝试了很多次,但没有成功。我正在使用 SimpleXML 来读取 Disorder 节点,因为 XML 很小 (0.36MB) 并且 SimpleXML 比 XMLReader.That 的代码更简单我正在读取节点:
if ( $node->nodeType == XML_ELEMENT_NODE && $node->localName == "Disorder") {
$dom = new DomDocument();
$data = $dom->importNode($node,true);
$dom->appendChild($data);
$simplexml = simplexml_import_dom($data);
$disease['name'] = "$simplexml->Name";
$disease['orpha'] = "$simplexml->OrphaNumber";
$disease['link'] = "$simplexml->ExpertLink";
$disease['parent'] = ????? ;
在“??????”中是我需要插入实际 Disorder 父级 Disorder 名称的地方。
我努力了 2 天,什么也没有...:/
谁能帮帮我?
在一般的 XML/XPath 意义上,parent 将是 ..
。但是,在您的域的意义上,parent 混乱 与 XML/XPath parent。从名为"Carcinoma of the ampulla of Vater"的Disorder
点出发,要上三层(../../..
)才能到达包含Disorder
名字的祖传ClassificationNode
"Rare hepatic and biliary tract tumor".
具体来说,鉴于您的 XML(修复为 well-formed):
<ClassificationNode>
<Disorder id="14879">
<OrphaNumber>101943</OrphaNumber>
<ExpertLink lang="en">http://www.orpha.net/consor/cgi-bin/OC_Exp.php?lng=en&Expert=101943</ExpertLink>
<Name lang="en">Rare hepatic and biliary tract tumor</Name>
</Disorder>
<ClassificationNodeChildList count="3">
<ClassificationNode>
<Disorder id="21130">
<OrphaNumber>300557</OrphaNumber>
<ExpertLink lang="en">http://www.orpha.net/consor/cgi-bin/OC_Exp.php?lng=en&Expert=300557</ExpertLink>
<Name lang="en">Carcinoma of the ampulla of Vater</Name>
</Disorder>
<ClassificationNodeChildList count="0"/>
</ClassificationNode>
</ClassificationNodeChildList>
</ClassificationNode>
这个 XPath
//Disorder[@id='21130']/../../../Disorder/Name/text()
returns parent 请求的名称:
"Rare hepatic and biliary tract tumor"
所以,你的PHP语句可以调整如下:
$parent = $simplexml->xpath("../../../Disorder/Name/text()");
假设您想要 $parent
中 parent 疾病的名称,或者只是
$parent = $simplexml->xpath("../../../Disorder");
如果你想要 parent 无序元素本身 $parent
.
您遇到的问题是,您通过 XMLReader 的节点扩展变成 DOMElement 的文档片段不包含 "parent" 分别。 "child"(parent/child 甚至是错误的术语,您在这里寻找的是前面或后面的节点,而不是父节点或子节点):
<Disorder id="14879">
<OrphaNumber>101943</OrphaNumber>
<ExpertLink lang="en">
http://www.orpha.net/consor/cgi-bin/OC_Exp.php?lng=en&Expert=101943
</ExpertLink>
<Name lang="en">Rare hepatic and biliary tract tumor</Name>
</Disorder>
如该片段所示,它只是 "parent"。您需要将整个 ClassificationNode
元素作为您的 xpath 的基础。然后你应该能够像 .
这样的 xpath 查询
我需要读取一个复杂的 XML 文件,我需要检索每个名为 "Disorder" 的节点的特定父节点...让我展示 xml 文件:
<ClassificationNode>
<Disorder id="14879">
<OrphaNumber>101943</OrphaNumber>
<ExpertLink lang="en">
http://www.orpha.net/consor/cgi-bin/OC_Exp.php?lng=en&Expert=101943
</ExpertLink>
<Name lang="en">Rare hepatic and biliary tract tumor</Name>
</Disorder>
<ClassificationNodeChildList count="3">
<ClassificationNode>
<Disorder id="21130">
<OrphaNumber>300557</OrphaNumber>
<ExpertLink lang="en">
http://www.orpha.net/consor/cgi-bin/OC_Exp.php?lng=en&Expert=300557
</ExpertLink>
<Name lang="en">Carcinoma of the ampulla of Vater</Name>
</Disorder>
<ClassificationNodeChildList count="0"></ClassificationNodeChildList>
</ClassificationNode>
每个标签 Disorder 都有一个 Disorder 父项,在这种情况下,Disorder 名称 "Carcinoma of the ampulla of Vater" 它是 Disorder "Rare hepatic and biliary tract tumor" 的子项。 我试图在 PHP 中使用 XPath 检索这些值,这是我的代码:
$parent = $simplexml->xpath("../../Disorder/Name");
但是数组对我来说达到了零....我用其他 xpath 语法尝试了很多次,但没有成功。我正在使用 SimpleXML 来读取 Disorder 节点,因为 XML 很小 (0.36MB) 并且 SimpleXML 比 XMLReader.That 的代码更简单我正在读取节点:
if ( $node->nodeType == XML_ELEMENT_NODE && $node->localName == "Disorder") {
$dom = new DomDocument();
$data = $dom->importNode($node,true);
$dom->appendChild($data);
$simplexml = simplexml_import_dom($data);
$disease['name'] = "$simplexml->Name";
$disease['orpha'] = "$simplexml->OrphaNumber";
$disease['link'] = "$simplexml->ExpertLink";
$disease['parent'] = ????? ;
在“??????”中是我需要插入实际 Disorder 父级 Disorder 名称的地方。 我努力了 2 天,什么也没有...:/
谁能帮帮我?
在一般的 XML/XPath 意义上,parent 将是 ..
。但是,在您的域的意义上,parent 混乱 与 XML/XPath parent。从名为"Carcinoma of the ampulla of Vater"的Disorder
点出发,要上三层(../../..
)才能到达包含Disorder
名字的祖传ClassificationNode
"Rare hepatic and biliary tract tumor".
具体来说,鉴于您的 XML(修复为 well-formed):
<ClassificationNode>
<Disorder id="14879">
<OrphaNumber>101943</OrphaNumber>
<ExpertLink lang="en">http://www.orpha.net/consor/cgi-bin/OC_Exp.php?lng=en&Expert=101943</ExpertLink>
<Name lang="en">Rare hepatic and biliary tract tumor</Name>
</Disorder>
<ClassificationNodeChildList count="3">
<ClassificationNode>
<Disorder id="21130">
<OrphaNumber>300557</OrphaNumber>
<ExpertLink lang="en">http://www.orpha.net/consor/cgi-bin/OC_Exp.php?lng=en&Expert=300557</ExpertLink>
<Name lang="en">Carcinoma of the ampulla of Vater</Name>
</Disorder>
<ClassificationNodeChildList count="0"/>
</ClassificationNode>
</ClassificationNodeChildList>
</ClassificationNode>
这个 XPath
//Disorder[@id='21130']/../../../Disorder/Name/text()
returns parent 请求的名称:
"Rare hepatic and biliary tract tumor"
所以,你的PHP语句可以调整如下:
$parent = $simplexml->xpath("../../../Disorder/Name/text()");
假设您想要 $parent
中 parent 疾病的名称,或者只是
$parent = $simplexml->xpath("../../../Disorder");
如果你想要 parent 无序元素本身 $parent
.
您遇到的问题是,您通过 XMLReader 的节点扩展变成 DOMElement 的文档片段不包含 "parent" 分别。 "child"(parent/child 甚至是错误的术语,您在这里寻找的是前面或后面的节点,而不是父节点或子节点):
<Disorder id="14879">
<OrphaNumber>101943</OrphaNumber>
<ExpertLink lang="en">
http://www.orpha.net/consor/cgi-bin/OC_Exp.php?lng=en&Expert=101943
</ExpertLink>
<Name lang="en">Rare hepatic and biliary tract tumor</Name>
</Disorder>
如该片段所示,它只是 "parent"。您需要将整个 ClassificationNode
元素作为您的 xpath 的基础。然后你应该能够像