Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #14176
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Extracting values from xml file using php/xpath/namespace/simplexml/ |
| Date | 2014-08-17 19:43 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <1443898.x2o4gcXilq@PointedEars.de> (permalink) |
| References | <a8eb7607-8c39-4062-917c-d815b223a021@googlegroups.com> <lsfp0k$cpj$1@dont-email.me> <b2d1ddf1-3b88-4f33-9336-d5f77ea186f4@googlegroups.com> |
ofuuzo@gmail.com wrote:
>> On 8/13/2014 7:02 AM, ofuuzo@gmail.com wrote:
>> > I am new in PHP/XML. I can't figure out how to combine
>> > xpath/namespace/simplexml/php to extract the values of "dc.title" and
>> > "dc.date" in this xml file:
>>
>> > <?xml version="1.0" encoding="UTF-8"?> -<OAI-PMH
>> > xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/
>> > http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"
>> > xmlns="http://www.openarchives.org/OAI/2.0/"
>> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
>>
>> > <ListRecords>
>> > <record>
>> > <metadata xmlns:dc="http://purl.org/dc/elements/1.1/"
>> > xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/">
>> > <oai_dc:dc
>> > xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/">
>> > <dc:title>GAFBone</dc:title>
>> > <dc:date>2014-05-01T00:00:00Z</dc:date>
>> > </oai_dc:dc>
>> > </metadata>
>> > </record>
>> […]
>
> I figured it out and it works:
Given your *stated* requirements, you have written it too complicated,
though.
> <?php
>
> // […]
> $xpath->registerNamespace('oai', 'http://www.openarchives.org/OAI/2.0/');
> $xpath->registerNamespace('oai-dc',
> 'http://www.openarchives.org/OAI/2.0/oai_dc/');
You do not have to register namespaces that you do not use in your XPath
query. See below.
> $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
>
> $records = $xpath->evaluate('/oai:OAI-PMH/oai:ListRecords/oai:record',
> NULL, FALSE); foreach ($records as $entry) {
> // […]
> $dc = $xpath->evaluate('oai:metadata/oai-dc:dc/dc:*', $entry, FALSE);
> foreach ($dc as $dcElement){
> switch ($dcElement->localName) {
> case 'title' :
> echo 'Title: '.$dcElement->nodeValue;
> echo "\n";
> break;
> case 'date' :
> echo 'Date: '.$dcElement->nodeValue;
> echo "\n";
> break;
> case 'relation' :
> echo 'Relation: '.$dcElement->nodeValue;
> echo "\n";
> break;
>
> }
> }
> }
> ?>
You do not have to specify the whole path with XPath if you are certain of
uniqueness (or content with iteration over content-duplicates is
acceptable), and you certainly do not have to iterate over elements to find
matches. All of this is built into XPath, see the XPath 1.0
Specification [0], particularly the “Location Paths” section.
Here it is with SimpleXMLElement, which you can apply to your DOMDocument
approach, too:
<?php
$xml = new SimpleXMLElement('tmp/test-oai.xml', 0, true);
$xml->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
echo $xml->xpath('.//dc:title')[0] . PHP_EOL;
echo $xml->xpath('.//dc:date')[0] . PHP_EOL;
String concatenation (“.”) automatically calls an object’s __toString()
method (here: [1]), so does typecasting the value to string [2].
Note that function array dereferencing (“foo(…)[…]”) was introduced in
PHP 5.4 [3]; if you have not upgraded yet, you should, for better
security [4] and performance [5]. (I have tested this with PHP [CLI]
5.6.0RC3 on GNU/Linux.)
For your next posting to Usenet, please
- read <http://www.catb.org/~esr/faqs/smart-questions.html>;
- post *well-formed* XML, if any: <http://validator.w3.org/>;
- trim your quotations to the relevant minimum, see e.g.
<http://www.netmeister.org/news/learn2quote.html>;
- do not use the borken Google Groups, use a newsreader like KNode or
Thunderbird instead:
* <http://www.kde.org/applications/internet/knode/>
* <http://getthunderbird.com/>;
- get a real name.
_________
[0] <http://www.w3.org/TR/1999/REC-xpath-19991116/>
[1] <http://php.net/manual/en/simplexmlelement.tostring.php>
[2] <http://php.net/manual/en/language.types.type-juggling.php>
[3] <http://php.net/manual/en/migration54.new-features.php>
[4] <http://php.net/archive/2014.php#id2014-08-14-1>
[5] <http://www.lornajane.net/posts/2012/php-5-4-benchmarks>
--
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Extracting values from xml file using php/xpath/namespace/simplexml/ ofuuzo@gmail.com - 2014-08-13 04:02 -0700
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Jerry Stuckle <jstucklex@attglobal.net> - 2014-08-13 09:25 -0400
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ ofuuzo@gmail.com - 2014-08-14 03:29 -0700
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Jerry Stuckle <jstucklex@attglobal.net> - 2014-08-14 10:06 -0400
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-08-17 19:43 +0200
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Jerry Stuckle <jstucklex@attglobal.net> - 2014-08-17 15:21 -0400
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-08-17 23:41 +0200
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Jerry Stuckle <jstucklex@attglobal.net> - 2014-08-17 18:57 -0400
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-09-03 23:10 +0200
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Jerry Stuckle <jstucklex@attglobal.net> - 2014-09-03 20:11 -0400
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-09-04 02:42 +0200
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Jerry Stuckle <jstucklex@attglobal.net> - 2014-09-03 21:57 -0400
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-09-04 23:21 +0200
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Jerry Stuckle <jstucklex@attglobal.net> - 2014-09-04 20:43 -0400
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-09-05 04:08 +0200
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-05 02:50 +0000
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Jerry Stuckle <jstucklex@attglobal.net> - 2014-09-05 08:24 -0400
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-09-07 11:57 +0200
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ Jerry Stuckle <jstucklex@attglobal.net> - 2014-09-07 09:54 -0400
Re: Extracting values from xml file using php/xpath/namespace/simplexml/ "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2014-09-07 23:53 +0200
csiph-web