简体   繁体   English

php DOMXpath,帮助获取节点值

[英]php DOMXpath, help getting node value

I have this XML: 我有这个XML:

<?xml version="1.0" encoding="utf-8"?>
<cfdi:Comprobante >
<cfdi:Conceptos>
<cfdi:Concepto cantidad="1.00" unidad="PZA" noIdentificacion="RRGARACION" descripcion="51571 CADENA ORO AMRILLO 45CMTS FAVOR DE SOLDAR EL ESLABON DE LADO DEL BROCHE" valorUnitario="81.90" importe="81.90" />
<cfdi:Concepto cantidad="1.00" unidad="PZA" noIdentificacion="RRGARACION" descripcion="51570 CADENA ORO BLANCO C/CORAZON FAVOR DE SOLDAR A 16 CMTS DEL BROCHE" valorUnitario="206.90" importe="206.90" />
<cfdi:/Conceptos>
<cfdi:Impuestos>
<cfdi:Traslados>
<cfdi:Traslado impuesto="IVA" tasa="16" importe="46.20" />
</cfdi:Traslados>
</cfdi:Impuestos>
</cfdi:Comprobante>

and I need to get the "46.20" value of Traslado / importe. 我需要获得Traslado / importe的“ 46.20”值。 how can I do it? 我该怎么做? I tried something like this: 我尝试过这样的事情:

$xp = new DOMXpath($xml);
$data['IVA']= getpath("//@Traslados[@impuesto='IVA']/@importe");

but didn't work. 但是没有用

There are a couple of corrections to the XML ( adding the name space and moving the / to the start of <cfdi:/Conceptos> . 对XML进行了一些更正(添加名称空间并将/移至<cfdi:/Conceptos>的开头。

In the XML, you have to register the namespace in the xpath object so it can work with it and the xpath expression itself is updated... 在XML中,您必须在xpath对象中注册名称空间,以便它可以使用它并更新xpath表达式本身...

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$xml= <<< XML
<?xml version="1.0" encoding="utf-8"?>
<Comprobante xmlns:cfdi="cfdi">
    <cfdi:Conceptos>
        <cfdi:Concepto cantidad="1.00" unidad="PZA" 
            noIdentificacion="RRGARACION" 
            descripcion="51571 CADENA ORO AMRILLO 45CMTS FAVOR DE SOLDAR EL ESLABON DE LADO DEL BROCHE" valorUnitario="81.90" importe="81.90" />
        <cfdi:Concepto cantidad="1.00" unidad="PZA" 
            noIdentificacion="RRGARACION" 
            descripcion="51570 CADENA ORO BLANCO C/CORAZON FAVOR DE SOLDAR A 16 CMTS DEL BROCHE" valorUnitario="206.90" importe="206.90" />
    </cfdi:Conceptos>
    <cfdi:Impuestos>
        <cfdi:Traslados>
            <cfdi:Traslado impuesto="IVA" tasa="16" importe="46.20" />
        </cfdi:Traslados>
    </cfdi:Impuestos>
</Comprobante>
XML;

$doc = new DOMDocument();
$doc->loadXML($xml);
$xp = new DOMXpath($doc);
$xp->registerNamespace('cfdi', 'cfdi');
print_r( $xp->query("//cfdi:Traslados/*[@impuesto='IVA']/@importe"));

So for starters, you're working with invalid XML. 因此,对于初学者来说,您正在使用无效的XML。 It should look something like this: 它看起来应该像这样:

<?xml version="1.0" encoding="utf-8"?>
<cfdi:Comprobante xmlns:cfdi="https://some/uri">
    <cfdi:Conceptos>
        <cfdi:Concepto cantidad="1.00" unidad="PZA" noIdentificacion="RRGARACION" descripcion="51571 CADENA ORO AMRILLO 45CMTS FAVOR DE SOLDAR EL ESLABON DE LADO DEL BROCHE" valorUnitario="81.90" importe="81.90" />
        <cfdi:Concepto cantidad="1.00" unidad="PZA" noIdentificacion="RRGARACION" descripcion="51570 CADENA ORO BLANCO C/CORAZON FAVOR DE SOLDAR A 16 CMTS DEL BROCHE" valorUnitario="206.90" importe="206.90" />
    </cfdi:Conceptos>
    <cfdi:Impuestos>
        <cfdi:Traslados>
            <cfdi:Traslado impuesto="IVA" tasa="16" importe="46.20" />
        </cfdi:Traslados>
    </cfdi:Impuestos>
</cfdi:Comprobante>

(Note the xmlns attribute and the correct closing slashes.) (请注意xmlns属性和正确的结束斜杠。)

Next, you're calling a function getpath which came from where? 接下来,您要调用一个函数getpath ,它来自哪里? I don't know. 我不知道。 But you're passing it an invalid XPath query. 但是您传递给它一个无效的XPath查询。 You're looking for the attribute of the Traslados element when you want the Traslado element instead. 您正在寻找的属性Traslados元素,当你想在Traslado元素来代替。 Try this out: 试试看:

$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<cfdi:Comprobante xmlns:cfdi="https://some/uri">
    <cfdi:Conceptos>
        <cfdi:Concepto cantidad="1.00" unidad="PZA" noIdentificacion="RRGARACION" descripcion="51571 CADENA ORO AMRILLO 45CMTS FAVOR DE SOLDAR EL ESLABON DE LADO DEL BROCHE" valorUnitario="81.90" importe="81.90" />
        <cfdi:Concepto cantidad="1.00" unidad="PZA" noIdentificacion="RRGARACION" descripcion="51570 CADENA ORO BLANCO C/CORAZON FAVOR DE SOLDAR A 16 CMTS DEL BROCHE" valorUnitario="206.90" importe="206.90" />
    </cfdi:Conceptos>
    <cfdi:Impuestos>
        <cfdi:Traslados>
            <cfdi:Traslado impuesto="IVA" tasa="16" importe="46.20" />
        </cfdi:Traslados>
    </cfdi:Impuestos>
</cfdi:Comprobante>
XML;

$doc = new DomDocument();
$doc->loadXML($xml);
$xp = new DOMXpath($doc);
$xp->registerNamespace("cfdi", "https://some/uri");
$nodes = $xp->query("//cfdi:Traslados/*[@impuesto='IVA']/@importe");
foreach ($nodes as $node) {
    var_dump($node->nodeValue);
}

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

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