繁体   English   中英

PHP xPath-如何限制多个查询中的值?

[英]PHP xPath - How to limit the value in multiple queries?

我必须从XML文档的每个块中选择4个值中的3个。 到目前为止有效。

但是现在我必须限制结果。 Row [1]包含1到300之间的整数,但我只需要300之前的值。

$xpath = new \DOMXPath($dom);

$this->nodes = $xpath->query("
        //row[position()>1]//cell[1]/data
        | //row[position()>1]//cell[2]/data
        | //row[position()>1]//cell[3]/data
        | //row[position()>1]//cell[data<300]
    ");

XML文档如下所示:

   <Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s63"><Data ss:Type="Number">62</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">..</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Ethiopia PDR</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Ethiopia PDR</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s63"><Data ss:Type="Number">63</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">EE</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Estonia</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">the Republic of Estonia</Data></Cell>
   </Row>

编辑:

在您的帮助下,它现在可以工作,

$this->nodes = $xpath->query("
        //row[position()>1]/cell[data<300]/data
        | //row[position()>1]/cell[2]/data
        | //row[position()>1]/cell[3]/data
    ");

但是现在最后有一些查询似乎忽略的行:

   <Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s63"><Data ss:Type="Number">5600</Data></Cell>
    <Cell ss:StyleID="s64"/>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Antarctic Region</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">Antarctic Region</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s63"><Data ss:Type="Number">5706</Data></Cell>
    <Cell ss:StyleID="s64"/>
    <Cell ss:StyleID="s63"><Data ss:Type="String">European Union</Data></Cell>
    <Cell ss:StyleID="s63"><Data ss:Type="String">European Union</Data></Cell>
   </Row>

现在,第二个“单元格”为空,查询将忽略仅打印300以下的数据集。 或者更好的是,这是不想要的结果:

European Union - Antarctic Region

我的结果中还显示了这些以及ID超过300的更多内容。 我猜我只会得到row [1] + row [2] + row [3] ???

//Row[1]//Cell[1]/Data[.<=300]

阅读我对复杂性的评论(可以查询吗?)后,尝试cell[data<300]/data ,它也返回数据,而不是单元格。

简介:也许优化查询类似于

  //row[position()>1 and ./cell[1]/data<300]//data

但是您说“ 4个值中的3个”(并显示它们是前3个)...好吧,现在理解了...检查是否是,

  //row[position()>1 and ./cell[1]/data<300]/cell[position()<4]/data

暂无
暂无

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

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