note 49660 added to function.dom-domelement-getattribute
| From: | tw3k at tw3k dot net | Date: | Fri, 04 Feb 2005 00:28:55 +0000 |
| Subject: | note 49660 added to function.dom-domelement-getattribute | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-84496@lists.php.net to get a copy of this message | ||
A simple example using getAttribute.
$xml = <<< XML
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD Simplified DocBook XML V4.1.2.5//EN"
"http://www.oasis-open.org/docbook/xml/simple/4.1.2.5/sdocbook.dtd">
<article>
<title id="Site" lang="en" pagenum="1"
revisionflag="added" role="Web Page">My Site</title>
<section id="Welcome" label="Active" lang="en"
revisionflag="added" role="Content Page">
<title id="Intro" lang="en" pagenum="1"
revisionflag="added">Welcome</title>
<para>
<inlinemediaobject>
<imageobject>
<imagedata fileref="img/image.jpg" format="JPG" width="150px"
depth="150px" />
</imageobject>
<textobject>
<phrase>Alternative image text.</phrase>
</textobject>
</inlinemediaobject>
Paragraph One.
</para>
<para>Paragraph two.</para>
<para>Paragraph three.</para>
</section>
</article>
XML;
$dom = DomDocument::loadXML($xml);
$xpath = new domxpath($dom);
$imagedata = $xpath->query("//article/section[@id =
'Welcome']/para/inlinemediaobject/imageobject/imagedata");
$imagephrase = $xpath->query("//article/section[@id =
'Welcome']/para/inlinemediaobject/textobject/phrase");
$image = image($imagedata,$imagephrase);
echo "<pre>"; print_r($image); echo "</pre>";
function image($imagedata,$imagephrase)
{
foreach ($imagedata as $img) {
foreach ($imagephrase as $node)
{
$alt = $node->textContent;
}
$fileref = $img -> getAttribute('fileref');
$width = $img -> getAttribute('width');
$depth = $img -> getAttribute('depth');
$imageArray = array("src" => $fileref, "width" => $width,
"height" => $depth, "alt" => $alt);
}
return $imageArray;
}
/*************/
Expected result:
Array
(
[src] => img/image.jpg
[width] => 150px
[height] => 150px
[alt] => Alternative image text.
)
----
Manual Page -- http://www.php.net/manual/en/function.dom-domelement-getattribute.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49660
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49660&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49660&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49660&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49660&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49660&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49660&report=yes
Search -- http://master.php.net/manage/user-notes.php