如何打印具有相同标记名称(在不同级别)和不同值的 nodeValue?

How do you print nodeValue with the same tag name (at different levels) and different values?

通过 link [https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text][1] 我检索了 XML 格式。从这种格式中,我检索了 Gene-commentary_headings 之间的所有信息。因此我使用了 DOMDocuments 和 getElemenetsByTagName。现在,我正在尝试检索该行;例如名称为 GeneOntology。 GeneOntology 位于 Gene-commmentary_heading 的 22 标记处。我只检索位于 Gene-commentary_headings 部分的信息。

<Gene-commentary_heading>GeneOntology</Gene-commentary_heading>.

现在,我正在尝试打印例如所有名称为 Other-source_anchor 的标签。例如

<Other-source_anchor>DNA binding</Other-source_anchor>

但GOA上也有相同标签但级别更高的。我只想检索 DNA 结合级别的标签。如果我使用

foreach($node->getElementsByTagName('Other-source_anchor') as $subnode)

我没有得到结果。如果我用 $doc 更改 $node,我会检索所有带有标签的节点值。我如何确保只在 DNA 绑定级别检索 Other-source_anchor 标签的节点值?

下面是我写的代码:

$esearch_test = "https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text";
$result = file_get_contents($esearch_test);
$xml = simplexml_load_string($result);

$doc = new DOMDocument();
$doc = DOMDocument::loadXML($xml);
$c = 1;

foreach($doc->getElementsByTagName('Gene-commentary_heading') as $node) {
    if ($node->textContent =="GeneOntology"){
        // echo "<pre>"."$c: ".$node->textContent."</pre>";
        // echo "<pre>"."$c: ".$node->nodeName."</pre>";
        // echo "<pre>"."$c: ".$node->nodeValue."</pre>";
        foreach ($doc->getElementsByTagName('Other-source_anchor') as $subnode){
            echo "<pre>"."$c: ".$subnode->nodeName."</pre>";
            echo "<pre>"."$c: ".$subnode->nodeValue."</pre>"; 
        }
    }

    $c++; # 22: GeneOntology

} 

我在上面的代码中使用的 xml 文件的一部分。

<Gene-commentary_heading>**GeneOntology**</Gene-commentary_heading>
      <Gene-commentary_source>
        <Other-source>
          <Other-source_pre-text>Provided by</Other-source_pre-text>
          <Other-source_anchor>GOA</Other-source_anchor>
          <Other-source_url>http://www.ebi.ac.uk/GOA/</Other-source_url>
        </Other-source>
      </Gene-commentary_source>
      <Gene-commentary_comment>
        <Gene-commentary>
          <Gene-commentary_type value="comment">254</Gene-commentary_type>
          <Gene-commentary_label>Function</Gene-commentary_label>
          <Gene-commentary_comment>
            <Gene-commentary>
              <Gene-commentary_type value="comment">254</Gene-commentary_type>
              <Gene-commentary_source>
                <Other-source>
                  <Other-source_src>
                    <Dbtag>
                      <Dbtag_db>GO</Dbtag_db>
                      <Dbtag_tag>
                        <Object-id>
                          <Object-id_id>3677</Object-id_id>
                        </Object-id>
                      </Dbtag_tag>
                    </Dbtag>
                  </Other-source_src>
                  <Other-source_anchor>DNA binding</Other-source_anchor>
                  <Other-source_post-text>evidence: IEA</Other-source_post-text>
                </Other-source>
              </Gene-commentary_source>
            </Gene-commentary>
            <Gene-commentary>

函数

以字符串格式检索给定路径信息的函数

function xml_retriever($xml_link,$path){
    $result = file_get_contents($xml_link);
    $xml = simplexml_load_string($result);
    $doc = new DOMDocument();
    $doc = DOMDocument::loadXML($xml);
    $xpath = new DOMXPath($doc);
    $entries = $xpath->query($path);
    $attr = '';
    foreach($entries as $node){
        $attr .= '|'.' '.$node->nodeValue. "\r\n";
        $attr = ltrim($attr, '|');
    }
    return $attr;
}

功能测试

简单测试功能是否有效

# Example query and example path
$esearch_test = "https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text";
$query = "/Entrezgene/Entrezgene_properties/Gene-commentary[3]/Gene-commentary_comment/Gene-commentary[1]/Gene-commentary_comment/Gene-commentary[*]/Gene-commentary_source/Other-source/Other-source_anchor";
# Print the result
echo xml_retriever($esearch_test,$query);