Php 读一本家长书';在复杂XML中使用Xpath的父节点
我需要读取一个复杂的XML文件,并且需要检索每个节点的特定父节点“Disorder”…让我展示一下XML文件:Php 读一本家长书';在复杂XML中使用Xpath的父节点,php,xml,xpath,Php,Xml,Xpath,我需要读取一个复杂的XML文件,并且需要检索每个节点的特定父节点“Disorder”…让我展示一下XML文件: <ClassificationNode> <Disorder id="14879"> <OrphaNumber>101943</OrphaNumber> <ExpertLink lang="en"> http://www.orpha.net/consor/cgi-bin/OC_
<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>
但是数组对我来说是零…我尝试了很多次使用其他xpath语法,但没有成功。我使用SimpleXML读取无序节点,因为XML很小(0.36MB),SimpleXML比XMLReader更简单。这就是我读取节点的代码:
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'] = ????? ;
在“??”中,我需要插入实际失调的失调父项的名称。
我努力了两天,什么都没有/
任何人都可以帮助我吗?在一般的XML/XPath意义上,父级应该是
。
。但是,在您的域中,父无序与XML/XPath父无序不同。从名为“瓦特壶腹癌”的疾病来看,你必须上升三次(。/../../..
)才能到达祖先的分类节点
,该节点包含名为“罕见的肝胆道肿瘤”的疾病
具体来说,考虑到您的XML(已修复为格式良好):
根据请求返回父级的名称:
"Rare hepatic and biliary tract tumor"
因此,您的PHP语句可以调整如下:
$parent = $simplexml->xpath("../../../Disorder/Name/text()");
假设您想要父疾病的名称在$parent
中,或者
$parent = $simplexml->xpath("../../../Disorder");
如果希望父元素本身位于$parent
中,则存在的问题是,通过XMLReader节点扩展而变成domeElement的文档片段不包含“父”响应。“子节点”(父节点/子节点是错误的术语,您在此处查找的是前面或后面的节点,而不是父节点或子节点):
101943
http://www.orpha.net/consor/cgi-bin/OC_Exp.php?lng=en&Expert=101943
罕见的肝胆道肿瘤
正如这个片段所示,它只是“父项”。您需要将整个ClassificationNode
元素作为xpath的基础。然后,您应该能够执行xpath查询,如。感谢各位的回答,我使用DOMNodeList解决了我的问题。我在PHP中创建了一个方法,该方法将需要提取数据的节点转换为DOMNodeList。使用NodeList很容易提取前缀为“childNodes”的数据。顺便说一下,谢谢你//
"Rare hepatic and biliary tract tumor"
$parent = $simplexml->xpath("../../../Disorder/Name/text()");
$parent = $simplexml->xpath("../../../Disorder");
<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>