dongsong8932 2015-12-31 11:10
浏览 41
已采纳

访问具有谓词属性的XML元素,该属性在PHP中动态更新

Newbie here.Trouble with xpath() function in PHP.

XML file (data.xml):

<?xml version="1.0" ?> 
<menu>
<food type="Pizza">

    <item>
            <name>  Tomato &amp; Cheese  </name>
            <small price="5.50"/>
            <large price="9.75"/>

    </item>

    <item>
            <name>   Onions </name>
            <small price="6.85"/>
            <large price="10.85"/>

    </item>
</food>
</menu>

PHP code:

   <?php 
   $xml = simplexml_load_file("data.xml");
   $types = $xml->xpath("/menu/food/@type");
   foreach($types as $type){
      echo("<h1>$type</h1>");
      $menu = $xml->xpath("/menu/food[@type=$type]");     
      echo("<ul>");
      foreach($menu as $submenu){
         echo ("<li>$submenu</li>");
      }
      echo("</ul>");
   } 
   ?>

I have many types of food. I would like to access them and their subtypes in order. There is something wrong with my approach above. I can't seem to find it. Below is my intended output. How do I arrive at that with xpath()?

Intended Output:

PIZZA

=>Tomato & Cheese

=> Onions

=>

FOO

=> BAR

  • 写回答

2条回答 默认 最新

  • dtq81142 2015-12-31 11:20
    关注

    You need only a single xpath query, like this:

    $xml = simplexml_load_file("data.xml");
    
    // Get food items and iterate over them
    $foods = $xml->xpath("/menu/food");
    foreach($foods as $food){
    
        // print type attribute
        echo("<h1>{$food["type"]}</h1>");
    
        // Iterate over food items and print their names
        echo("<ul>");
        foreach($food->item as $item){
            echo ("<li>$item->name</li>");
        }
        echo("</ul>");
    }
    

    Having this xml:

    <?xml version="1.0" ?>
    <menu>
    <food type="Pizza">
        <item>
                <name>Tomato &amp; Cheese</name>
                <small price="5.50"/>
                <large price="9.75"/>
        </item>
        <item>
                <name>Onions</name>
                <small price="6.85"/>
                <large price="10.85"/>
        </item>
    </food>
    <food type="Pasta">
        <item>
                <name>Bolognese</name>
                <small price="5.50"/>
                <large price="9.75"/>
        </item>
        <item>
                <name>Carbonara</name>
                <small price="6.85"/>
                <large price="10.85"/>
        </item>
    </food>
    </menu>
    

    ... the above code will produce:

    <h1>Pizza</h1>
    <ul>
      <li>Tomato & Cheese</li>
      <li>Onions</li>
    </ul>
    
    <h1>Pasta</h1>
    <ul>
      <li>Bolognese</li>
      <li>Carbonara</li>
    </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)