Reputation: 2092
i need to read a complex XML file and i need to retrieve the specific parent node of each node called "Disorder"...let me show the xml file:
<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>
Each tag Disorder have a Disorder parent, in this case the Disorder Name "Carcinoma of the ampulla of Vater" it's a child of Disorder "Rare hepatic and biliary tract tumor". I have tried to retrieve theses values with XPath in PHP, that's my code:
$parent = $simplexml->xpath("../../Disorder/Name");
But the array reaches zero to me....i tried a lot of times with others xpath syntaxes, but no success. I'm using SimpleXML to read the Disorder nodes because the XML is small (0.36MB) and the SimpleXML is more simple than XMLReader.That's the code where im reading the nodes:
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'] = ????? ;
In "?????" is where i need to insert the name of Disorder parent of actual Disorder. I tried so hard for 2 days and nothing...:/
Anyone can help me ?
Upvotes: 1
Views: 765
Reputation: 197648
The problem you have is that the document fragment you turn into a DOMElement through node-expansion from XMLReader does not contain the "parent" resp. "child" (parent/child are wrong terms even, you're looking for preceding or following nodes here, not parent or child nodes):
<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>
As this fragment shows, it's the "parent" only. You need to take the whole ClassificationNode
element as the base for your xpath. You should then be able to do the xpath query like already outlined by kjhughes.
Upvotes: 1
Reputation: 111511
In the general XML/XPath sense, the parent would be ..
. However, in the sense of your domain, the parent disorder is not the same as the XML/XPath parent. From the point of the Disorder
named "Carcinoma of the ampulla of Vater", you have to go up three times (../../..
) to get to the ancestral ClassificationNode
that contains the Disorder
named "Rare hepatic and biliary tract tumor".
Specifically, given your XML (repaired to be 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>
This XPath
//Disorder[@id='21130']/../../../Disorder/Name/text()
returns the name of the parent as requested:
"Rare hepatic and biliary tract tumor"
So, your PHP statement can be adjusted as follows:
$parent = $simplexml->xpath("../../../Disorder/Name/text()");
assuming that you want the name of the parent disorder in $parent
, or just,
$parent = $simplexml->xpath("../../../Disorder");
if you want the parent disorder element itself in $parent
.
Upvotes: 2