简体   繁体   English

php xml xpath foreach获取具有相同名称的节点的值

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

Guys, 伙计们

I have this xml code: 我有这个xml代码:

<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. 我想为每个版本项目获取<link type="language"...>的值。

My php code is: 我的PHP代码是:

$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? 如何更改我的PHP代码才能获得此结果?

  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. 1)在xpath选择器的开头使用//表示从文档中的任何位置获取这些元素。 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. 2)您的xpath()调用需要位于循环内,否则它将在您正在查看的当前<item>上下文中不起作用。

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++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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