简体   繁体   中英

XML transformation using PHP5 DOM

I am developing an application were I need to transform XML documents that look like this(words.xml):

<?xml version='1.0' encoding='ISO-8859-1'?>
<!DOCTYPE words SYSTEM "words.dtd">
<words>
<word id="word_1">Alfa</word>
<word id="word_2">Beta</word>
<word id="word_3">Gamma</word>
<word id="word_4">Delta</word>
<word id="word_5">Zeta</word>
</words>

Using PHP5 and DOM. I would like the result to be (in this case):

word_1 = Alfa

My PHP code is this:

<?php

$xmlHitzakDok = new DOMDocument();

if($xmlHitzakDok->load("words.xml") === FALSE){die('Errorea hitzen xml-a kargatzean');}

$xPath_Hitzak = new DOMXPath($xmlHitzakDok);

$Hurrengo_Hitza = 'word_1';

foreach ($xPath->query('//words/word') AS $item)
{
   if ($item->getAttribute('id') == $Hurrengo_Hitza)
   {
    echo $item->getAttribute('id') . " = " . $item->nodeValue . "<br />";
   }
}

I am getting no results. Which is the problem?

您应该使用:$ xPath-> query('// words / word')而不是$ xPath_Hitzak-> query('// words / word')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM