Count elements of an XML file that contain a text

2

I would like to use PHP to show only the elements of an XML with the value

<disponible>si</disponible>

I have an .xml file with the following format:

<?xml version="1.0" encoding="UTF-8"?>
<libros>
    <OctavioPaz>
         <libro>Titulo de Libro</libro>
         <anio>Year</anio>
         <formato>.ebook</formato>
         <disponible>no</disponible>
    </OctavioPaz>
    <JaimeSabines>
         <libro>Titulo de Libro</libro>
         <anio>Year</anio>
         <formato>.ebook</formato>
         <disponible>no</disponible>
    </JaimeSabines>
    <JuanRulfo>
         <libro>Titulo de Libro</libro>
         <anio>Year</anio>
         <formato>.ebook</formato>
         <disponible>si</disponible>
    </JuanRulfo>
    <JuanJoseArreola>
         <libro>Titulo de Libro</libro>
         <anio>Year</anio>
         <formato>.ebook</formato>
         <disponible>no</disponible>
    </JuanJoseArreola>
    <AlfonsoReyes>
         <libro>Titulo de Libro</libro>
         <anio>Year</anio>
         <formato>.ebook</formato>
         <disponible>si</disponible>
    </AlfonsoReyes>
    <MartinLuisGuzman>
         <libro>Titulo de Libro</libro>
         <anio>Year</anio>
         <formato>.ebook</formato>
         <disponible>no</disponible>
    </MartinLuisGuzman>
</libros>

I use the following code to be able to show only some nodes:

<?php
$xml= "./libros/lista.xml";
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$disponible = $xmlDoc->getElementsByTagName("disponible");
$available = $disponible->length;
echo $available;
?>

The problem is that it counts all the <disponible></disponible> with either si and no , resulting in: 6.

Expected result:

Libros Disponibles ahora: 2 

How could I do this?

    
asked by Joseph Gonzaga 31.03.2017 в 05:19
source

3 answers

2

Iterating the Elements% co_of% returning getElementsByTagName ("nametag") , with (Nodos)

$xml= "./libros/lista.xml";
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
/* Busca en el documento los Nodos con Tag = "disponible"*/
$disponible = $xmlDoc->getElementsByTagName("disponible");
$count=0;
/* Iteramos los Nodos */
foreach ($disponible as $book) {
     /* Comparamos su valor por medio de su propiedad nodeValue
      si es igual a "si" , incrementamos el contador */
     if(($book->nodeValue)=="si") $count++;
}
/* Imprimimos la variable $count con un Texto Personalizado (el que desee)*/
echo "Libros Disponibles Ahora : " . $count;
    
answered by 31.03.2017 / 07:23
source
2

Track your result in foreach . That way, you would check if the content of the tag is the one you are looking for, and there you would incementarías a counter.

In the following example, we use $i to count the number of elements that match:

$i=0;
foreach($disponibles as $disponible)
{
  If($disponible->disponible=="si")
   {
      $i++; //si coincidió, incrementar el contador
   }
}
echo $i; //se imprime el valor final

'

    
answered by 31.03.2017 в 07:07
0

For counting you can use XPath and the count function. See the solution given in this another query

On the other hand, if you have control over the XML generation process, I would recommend that you "normalize" it, because as it is, its processing is a bit complicated ... For each book, I propose a structure of the type:

 <libro>
         <titulo>Titulo de Libro</titulo>
         <autor>JuanRulfo</autor>
         <anio>Year</anio>
         <formato>.ebook</formato>
         <disponible>si</disponible>
    </libro>

With this structure you could easily retrieve all the books whose "available" element has a certain value:  XPath: //libro[./displonible = 'si']

Greetings

    
answered by 31.03.2017 в 12:18