简体   繁体   中英

php xml xpath foreach to get values of nodes with same name

Guys,

I have this xml code:

<items>
<item type="boardgame" id="84876">
<versions>
<item type="boardgameversion" id="317166">
<link type="boardgameversion" id="84876" value="The Castles of Burgundy" inbound="true"/>
<name type="primary" sortindex="1" value="Chinese Edition"/>
<link type="boardgamepublisher" id="9068" value="Broadway Toys LTD"/>
<link type="language" id="2181" value="Chinese"/>
</item>

<item type="boardgameversion" id="269360">
<link type="boardgameversion" id="84876" value="The Castles of Burgundy" inbound="true"/>
<name type="primary" sortindex="1" value="Hobby World Russian first edition"/>
<link type="boardgamepublisher" id="18852" value="Hobby World"/>
<link type="boardgameartist" id="11886" value="Julien Delval"/>
<link type="boardgameartist" id="4959" value="Harald Lieske"/>
<link type="language" id="2202" value="Russian"/>
</item>

<item type="boardgameversion" id="141049">
<link type="boardgameversion" id="84876" value="The Castles of Burgundy" inbound="true"/>
<name type="primary" sortindex="1" value="Ravensburger English/French Edition"/>
<link type="boardgamepublisher" id="9" value="alea"/>
<link type="boardgamepublisher" id="34" value="Ravensburger Spieleverlag GmbH"/>
<link type="boardgameartist" id="11886" value="Julien Delval"/>
<link type="boardgameartist" id="4959" value="Harald Lieske"/>
<link type="language" id="2184" value="English"/>
<link type="language" id="2187" value="French"/>
</item>

<item type="boardgameversion" id="69303">
<link type="boardgameversion" id="84876" value="The Castles of Burgundy" inbound="true"/>
<name type="primary" sortindex="1" value="Ravensburger Multilingual First Edition"/>
<link type="boardgamepublisher" id="9" value="alea"/>
<link type="boardgamepublisher" id="34" value="Ravensburger Spieleverlag GmbH"/>
<link type="boardgameartist" id="11886" value="Julien Delval"/>
<link type="boardgameartist" id="4959" value="Harald Lieske"/>
<link type="language" id="2184" value="English"/>
<link type="language" id="2187" value="French"/>
<link type="language" id="2188" value="German"/>
</item>

<item type="boardgameversion" id="134173">
<link type="boardgameversion" id="84876" value="The Castles of Burgundy" inbound="true"/>
<name type="primary" sortindex="1" value="Ravensburger Multilingual Second Edition"/>
<link type="boardgamepublisher" id="9" value="alea"/>
<link type="boardgamepublisher" id="34" value="Ravensburger Spieleverlag GmbH"/>
<link type="boardgameartist" id="11886" value="Julien Delval"/>
<link type="boardgameartist" id="4959" value="Harald Lieske"/>
<link type="language" id="2184" value="English"/>
<link type="language" id="2187" value="French"/>
<link type="language" id="2188" value="German"/>
</item>
</versions>
</item>
</items>

I want to get the values of <link type="language"...> for every version item.

My php code is:

$language = $xmlinfo2->item->versions->item;
$lang = $language->xpath('//link[@type="language"]');
$num = 1;

    foreach($language as $lang1) {
       echo $num . ". ";
         foreach ($lang as $lang2){
           $lang3 = $lang2->attributes();
           echo $lang3['value'] . ", ";
         }
         echo "<br>";
         $num++;
    }

Its results is (it writes all languages for all versions out):

  1. Chinese, Russian, English, French, English, French, German, English, French, German,

  2. Chinese, Russian, English, French, English, French, German, English, French, German,

  3. Chinese, Russian, English, French, English, French, German, English, French, German,

  4. Chinese, Russian, English, French, English, French, German, English, French, German,

  5. Chinese, Russian, English, French, English, French, German, English, French, German,

How do I have to change my php code to get this results?

  1. Chinese

  2. Russian

  3. English, French

  4. English, French, German

  5. English, French, German

Thank you for your help!

You've got a couple of problems here:

1) Using // at the start of an xpath selector means fetch these elements from anywhere in the document. So you end up with a list of all the languages from all the items each time you loop.

2) Your xpath() call needs to be inside the loop, or it won't work in the context of the current <item> that you're looking at.

So you can use something like:

foreach ($language as $lang1) {
    $lang = $lang1->xpath('link[@type="language"]');

    echo $num . ". ";

    foreach ($lang as $lang2){
        $lang3 = $lang2->attributes();
        echo $lang3['value'] . ", ";
    }

    echo "<br>";
    $num++;
}

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