Page 1 of 1

A problem with a DOM representation...

Posted: Thu Apr 26, 2007 5:16 am
by Pesho
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi all,

I guess my problem is really stupid, but I just can't figure it out.
I have to extract information from a DOM representation. My XML content is:

[syntax="xml"]
<record>
      <header>
        <identifier>oai:ub.rug.nl:dbi/4357aa6e091c0</identifier>
      </header>
      <metadata>
        <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/            http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
          <dc:title>Just take her seriously</dc:title>
          <dc:creator>Weijmar Schultz, W.C.M.</dc:creator>
        </oai_dc:dc>
      </metadata>
</record>
first I try to extract information about the <metadata> tag:[/syntax]

Code: Select all

$records = $dom->documentElement->getElementsByTagName('metadata');
foreach($records as $record) {
	$children = $record->childNodes;
	foreach($children as $child) {
		echo $child->nodeName;
	}
}
And it works as expected. The output is: #textoai_dc:dc#text

However, when I try the same thing for the <dc:creator> tag, the otput is EMPTY.....

The following code:

Code: Select all

$records = $dom->documentElement->getElementsByTagName('dc:creator');
foreach($records as $record) {
	echo $record->nodeName;
}
also gives an empty output.


I'm totally confused why it doesn't return any information about the <dc:creator> tag.


Thanks in advance Smiley
Pesho


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Apr 26, 2007 8:18 am
by volka
Works fine for me

Code: Select all

<?php
$xml = <<< eox
<record>
      <header>
        <identifier>oai:ub.rug.nl:dbi/4357aa6e091c0</identifier>
      </header>
      <metadata>
        <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/            http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
          <dc:title>Just take her seriously</dc:title>
          <dc:creator>Weijmar Schultz, W.C.M.</dc:creator>
        </oai_dc:dc>
      </metadata>
</record>
eox;

echo phpversion(), "\n";

$dom = DOMDocument::loadxml($xml);
//$records = $dom->documentElement->getElementsByTagNameNS('http://purl.org/dc/elements/1.1/', 'creator');
$records = $dom->documentElement->getElementsByTagName('creator');
foreach($records as $record) {
   echo '. ', $record->nodeName;
}
prints
5.2.1
. dc:creator