【问题标题】:Symfony2 XPath returns duplicate nodesSymfony2 XPath 返回重复节点
【发布时间】:2016-04-03 16:22:57
【问题描述】:

我正在尝试从 XML 文件执行一些 sql 查询,但是当我从标签获取查询时,它返回查询的次数比预期的要多。

<?xml version="1.0" encoding="utf-8"?>
<informes>
    <informe>
    </informe>
    <indicadores>
        <indicador>
            <sql>
                SELECT concat('Total Facturacion: ', round(sum(total))) as valor FROM infomatrix.dtmax_facturacion
            </sql>
            <link>

            </link>
        </indicador>
        <indicador>
           <sql>
                SELECT concat('Stock total: ', round(sum(cantidad))) as valor FROM infomatrix.dtmax_stock
           </sql>
           <link>

           </link>
        </indicador>
    </indicadores>
</informes>

这是我的 php 代码:

    $sqlIndicador = $crawler->filterXpath('informes/indicadores/indicador');
    $elements = array();
    foreach($sqlIndicador as $element){
        $elements[] = $element->getElementsByTagName("sql")->item(0)->nodeValue;
    }
    $indicadores = array();
    foreach ($elements as $indicador) {
        $data = $repositorio->obtenerSqlIndicador($indicador);
        array_push($indicadores,$data);
    }

【问题讨论】:

    标签: php mysql symfony xpath nodes


    【解决方案1】:

    由于节点之间的结构相同,因此 $sqlIndicador 返回 2 个结果,您正在迭代并获得另外 2 个结果。

    将您的过滤器设为“informes/indicadores”,然后循环进入它,这将是“indicador”元素,在这种情况下会产生 2 个结果。并从这个元素中得到 sql。

    【讨论】:

    • 它没有用。它可能会返回两个查询,但每个查询会返回四次。
    • 你能做一个phpfiddle之类的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    相关资源
    最近更新 更多