Как вы печатаете nodeValue с тем же именем тега (на разных уровнях) и разными значениями? - PullRequest
0 голосов
/ 15 мая 2018

По ссылке [https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text][1] Я получил формат XML. Из этого формата я получил всю информацию между Gene-commentary_headings. Поэтому я использовал DOMDocuments и getElemenetsByTagName. Теперь я пытаюсь найти строку; например с именем GeneOntology. GeneOntology находится по метке 22 в Gene-commmentary_heading. Я извлекаю только информацию, которая находится внутри части Gene-commentary_headings.

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

Теперь я пытаюсь напечатать, например, все теги с именем Other-source_anchor. Например

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

Но есть и на GOA с тем же тегом, но на более высоком уровне. Я хотел бы только получить метки на уровне связывания ДНК. Если я использую

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

Я не получил результата. если я изменю $ node на $ doc, я получу все значения nodeValues ​​с помощью тега. Как мне убедиться, что я получаю только nodeValues ​​тега Other-source_anchor на уровне связывания ДНК?

Ниже - код, который я написал:

<code>$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."
"; // echo "
"."$c: ".$node->nodeName."
"; // echo "
"."$c: ".$node->nodeValue."
"; foreach ($ doc-> getElementsByTagName ('Other-source_anchor') как $ subnode) { echo "
"."$c: ".$subnode->nodeName."
"; echo "
"."$c: ".$subnode->nodeValue."
"; } } $ C ++; # 22: Генная Онтология }

Часть 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>

1 Ответ

0 голосов
/ 22 мая 2018

Функция

Функция, которая извлекает информацию для заданного пути в строковом формате

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);
...