<p>use a XSLT processor (<a href="http://php.net/manual/en/class.xsltprocessor.php" rel="nofollow noreferrer">http://php.net/manual/en/class.xsltprocessor.php</a>) with the following stylesheet.</p>

<pre class="lang-xslt prettyprint-override"><code>&lt;?xml version='1.0' encoding="UTF-8"?&gt;
&lt;xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'&gt;
    &lt;xsl:output method="html"/&gt;

    &lt;xsl:template match="/"&gt;
        &lt;div&gt;
            &lt;xsl:apply-templates select="//Gene-commentary_heading[ text() = 'GeneOntology']"/&gt;
        &lt;/div&gt;
    &lt;/xsl:template&gt;

    &lt;xsl:template match="Gene-commentary_heading"&gt;
        &lt;pre&gt;&lt;xsl:value-of select="name(.)"/&gt;&lt;/pre&gt;
        &lt;pre&gt;&lt;xsl:value-of select="."/&gt;&lt;/pre&gt;
        &lt;xsl:apply-templates select="..//Other-source_anchor"/&gt;
    &lt;/xsl:template&gt;

    &lt;xsl:template match="Other-source_anchor"&gt;
        &lt;pre&gt;&lt;xsl:value-of select="name(.)"/&gt;&lt;/pre&gt;
        &lt;pre&gt;&lt;xsl:value-of select="."/&gt;&lt;/pre&gt;
    &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;
</code></pre>

CC BY-SA 4.0
2018-05-16T15:14:03.673Z
2018-05-17T00:39:03.397Z
2018-05-17T00:39:03.397Z
Pierre
<p>I would like to retrieve all Other-source_anchor on the level of DNA binding from within a Gene Ontology XML file using php. </p>

<p>If I try this with my code, I only get Gene Ontology back and not the nodeValues in Other-source_anchor. Could someone tell me how to do this? </p>

<h1>Xml file were I wwant to get the nodeValues from</h1>

<pre class="lang-xml prettyprint-override"><code>&lt;Gene-commentary_heading&gt;GeneOntology&lt;/Gene-commentary_heading&gt;
      &lt;Gene-commentary_source&gt;
        &lt;Other-source&gt;
          &lt;Other-source_pre-text&gt;Provided by&lt;/Other-source_pre-text&gt;
          &lt;Other-source_anchor&gt;GOA&lt;/Other-source_anchor&gt;
          &lt;Other-source_url&gt;http://www.ebi.ac.uk/GOA/&lt;/Other-source_url&gt;
        &lt;/Other-source&gt;
      &lt;/Gene-commentary_source&gt;
      &lt;Gene-commentary_comment&gt;
        &lt;Gene-commentary&gt;
          &lt;Gene-commentary_type value="comment"&gt;254&lt;/Gene-commentary_type&gt;
          &lt;Gene-commentary_label&gt;Function&lt;/Gene-commentary_label&gt;
          &lt;Gene-commentary_comment&gt;
            &lt;Gene-commentary&gt;
              &lt;Gene-commentary_type value="comment"&gt;254&lt;/Gene-commentary_type&gt;
              &lt;Gene-commentary_source&gt;
                &lt;Other-source&gt;
                  &lt;Other-source_src&gt;
                    &lt;Dbtag&gt;
                      &lt;Dbtag_db&gt;GO&lt;/Dbtag_db&gt;
                      &lt;Dbtag_tag&gt;
                        &lt;Object-id&gt;
                          &lt;Object-id_id&gt;3677&lt;/Object-id_id&gt;
                        &lt;/Object-id&gt;
                      &lt;/Dbtag_tag&gt;
                    &lt;/Dbtag&gt;
                  &lt;/Other-source_src&gt;
                  &lt;Other-source_anchor&gt;DNA binding&lt;/Other-source_anchor&gt;
</code></pre>

<h1>code:</h1>

<pre class="lang-php prettyprint-override"><code>$esearch_test = "https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&amp;format=text";
$result = file_get_contents($esearch_test);
$xml = simplexml_load_string($result);
$doc = new DOMDocument();
$doc = DOMDocument::loadXML($xml);
$c = 1;
foreach($doc-&gt;getElementsByTagName('Gene-commentary_heading') as $node) {
    if ($node-&gt;textContent =="GeneOntology"){
        echo "&lt;pre&gt;"."$c: ".$node-&gt;nodeName."&lt;/pre&gt;";
        echo "&lt;pre&gt;"."$c: ".$node-&gt;nodeValue."&lt;/pre&gt;";
        $cn = 1;
        foreach ($node-&gt;getElementsByTagName('Other-source_anchor') as $subnode){
            echo "&lt;pre&gt;"."$c: ".$subnode-&gt;nodeName."&lt;/pre&gt;";
            echo "&lt;pre&gt;"."$c: ".$subnode-&gt;nodeValue."&lt;/pre&gt;"; 

            echo $cn++;
        }
    }
    $c++;
} 
</code></pre>

<gene-ontology><phylogenetics>
NCBI xml-format Printing nodeValues from tags with the same names
# NCBI xml-format Printing nodeValues from tags with the same names

(Asked by: username_0 on 2018-05-16)

I would like to retrieve all Other-source_anchor on the level of DNA binding from within a Gene Ontology XML file using php. 
If I try this with my code, I only get Gene Ontology back and not the nodeValues in Other-source_anchor. Could someone tell me how to do this? 
Xml file were I wwant to get the nodeValues from
GeneOntology
      
        
          Provided by
          GOA
          http://www.ebi.ac.uk/GOA/
        
      
      
        
          254
          Function
          
            
              254
              
                
                  
                    
                      GO
                      
                        
                          3677
                        
                      
                    
                  
                  DNA binding

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 ""."$c: ".$node->nodeName."";
        echo ""."$c: ".$node->nodeValue."";
        $cn = 1;
        foreach ($node->getElementsByTagName('Other-source_anchor') as $subnode){
            echo ""."$c: ".$subnode->nodeName."";
            echo ""."$c: ".$subnode->nodeValue.""; 

            echo $cn++;
        }
    }
    $c++;
}

**Tags:** gene-ontology, phylogenetics

---

## 1 answers

### Answer by username_1 on 2018-05-16. Score: 3

use a XSLT processor (http://php.net/manual/en/class.xsltprocessor.php) with the following stylesheet.


---

