0
$\begingroup$

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

<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>

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

            echo $cn++;
        }
    }
    $c++;
} 
$\endgroup$
3
$\begingroup$

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

<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
    <xsl:output method="html"/>

    <xsl:template match="/">
        <div>
            <xsl:apply-templates select="//Gene-commentary_heading[ text() = 'GeneOntology']"/>
        </div>
    </xsl:template>

    <xsl:template match="Gene-commentary_heading">
        <pre><xsl:value-of select="name(.)"/></pre>
        <pre><xsl:value-of select="."/></pre>
        <xsl:apply-templates select="..//Other-source_anchor"/>
    </xsl:template>

    <xsl:template match="Other-source_anchor">
        <pre><xsl:value-of select="name(.)"/></pre>
        <pre><xsl:value-of select="."/></pre>
    </xsl:template>

</xsl:stylesheet>
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.