duanbogan5878 2015-03-13 16:40
浏览 45
已采纳

如何在PHP中重复子节点上选择和排序XML

I have an XML file of data about paintings. Each painting can belong to one or more galleries. I need to be able to select paintings by gallery, and then sort that list into position order. So, for example, I have XML as follows:

<Paintings>
  <Painting>
    <Title>Painting 1</Title>
    <Description>ABC</Description>
    <Galleries>
      <Gallery id="01">1</Gallery>
      <Gallery id="02">3</Gallery>
    </Galleries>
    <Picturefile>painting1</Picturefile>
  </Painting>
  <Painting>
    <Title>Painting 2</Title>
    <Description>DEF</Description>
    <Galleries>
      <Gallery id="02">2</Gallery>
      <Gallery id="03">2</Gallery>
    </Galleries>
    <Picturefile>painting2</Picturefile>
  </Painting>
  <Painting>
    <Title>Painting 3</Title>
    <Description>GHI</Description>
    <Galleries>
      <Gallery id="01">2</Gallery>
      <Gallery id="03">1</Gallery>
    </Galleries>
    <Picturefile>painting3</Picturefile>
  </Painting>
  <Painting>
    <Title>Painting 4</Title>
    <Description>JKL</Description>
    <Galleries>
      <Gallery id="02">1</Gallery>
      <Gallery id="03">3</Gallery>
    </Galleries>
    <Picturefile>painting4</Picturefile>
  </Painting>
</Paintings>

As an example, I need to select paintings in gallery "02" and then sort them into position order. In the above xml, the node value represnts the sort order for that gallery. For the select, I would use:

$gallerypics = ($paintings->xpath("Painting/Galleries/Gallery[@id='$galleryid']"));

It's at this point I'm stuck. How do I then sort $gallerypics on the node value for that specific gallery? I have the option of making the sort order an attribute rather than a value, but I'm still stuck on how to sort that. If there was only one Gallery node per painting, this is a no brainer. I'm stuck on how to do this with multiple Gallery nodes. Any input is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duanguoyin7008 2015-03-13 19:50
    关注

    Basic idea is just to select the paintings you're after, dump them into an array, and sort it based on your criteria.


    Example:

    //$xml = your xml string
    $galleryid = '02';
    
    $dom = new DOMDocument();
    $dom->loadXML($xml);
    $xpath = new DOMXPath($dom);
    
    $paintings = iterator_to_array($xpath->query("/Paintings/Painting[Galleries/Gallery/@id='$galleryid']"));
    usort(
        $paintings,
        function ($a, $b) use ($xpath, $galleryid) {
            $query = "number(Galleries/Gallery[@id='$galleryid'])";
            return $xpath->evaluate($query, $a) - $xpath->evaluate($query, $b);
        }
    );
    
    foreach ($paintings as $painting) {
        echo $xpath->evaluate('string(Title)', $painting), "
    ";
    }
    

    Output:

    Painting 4
    Painting 2
    Painting 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?