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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法