1

I'm creating a simple web app using html and php, this app reads an XML file provided by a user on a webpage and then prints some values content on it.

The files are sent to the server by POST and then are read by a php script.

This is a fragment of my xml test file:

<cfdi:Impuestos totalImpuestosTrasladados="72.07">
    <cfdi:Traslados>
        <cfdi:Traslado importe="50.00" impuesto="IEPS" tasa="16.00"/>
        <cfdi:Traslado importe="22.07" impuesto="IVA" tasa="16.00"/>
    </cfdi:Traslados>
</cfdi:Impuestos>

And this is my code made on php:

<?php

foreach ($_FILES['archivos']['name'] as $f => $name) {
$tempPath = $_FILES["archivos"]["tmp_name"][$f];

//Load xml file directly on the temp path
$xml        = simplexml_load_file($tempPath, null, true);
$namespaces = $xml->getDocNamespaces();

//Creating and loading DOM object
$dom = new DOMDocument('1.0','utf-8');
$dom->load($tempPath);

//Getting IVAs' values
foreach ($xml->xpath('//cfdi:Comprobante//cfdi:Impuestos//cfdi:Traslados//cfdi:Traslado') as $impuestos){
    $IVA = $impuestos['importe'];
    echo("IVA = ".$IVA."<br>");
}
}
?>

That prints:

IVA = 50.00
IVA = 22.07

Print's screenshot

On this case I want to print only the IVA's value but it takes the IEPS' value too because IEPS is on the same path as IVA. These values are only on this part of the file and I don't know if there's a way to separate them.

I just want to select only the IVA's value. Would you help me please?:(

The complete XML file is: Here

4
  • Check the value of $impuestos['impuesto'] with an if! Commented Dec 30, 2017 at 1:41
  • It would work only on specific cases, what I need is to check if the value corresponds to IVA and not to other concepts. Commented Dec 30, 2017 at 1:44
  • So in other words if($impuestos['impuesto']==="IVA")? Commented Dec 30, 2017 at 1:48
  • What a dumb, you're right. This is my first time using php, I apologize. Thanks!. Commented Dec 30, 2017 at 7:09

1 Answer 1

1

Try:

foreach ($xml->xpath("//cfdi:Comprobante//cfdi:Impuestos//cfdi:Traslados//cfdi:Traslado[@impuesto='IVA']") as $impuestos){
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.