Extract groups from an XML file with XPath?

0

I have an XML like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<productos>
    <TITULO>DATOS TABLA PRODUCTOS</TITULO>
    <producto>
        <cod_prod>1010</cod_prod>
        <cod_zona>10</cod_zona>
    </producto>
    <producto>
        <cod_prod>1011</cod_prod>
        <cod_zona>10</cod_zona>
    </producto>
    <producto>
        <cod_prod>1012</cod_prod>
        <cod_zona>20</cod_zona>
    </producto>
    <producto>
        <cod_prod>1612</cod_prod>
        <cod_zona>30</cod_zona>
    </producto>
</productos>

I need to group the products by zone.

With the instruction:

distinct-values( //producto/cod_zona )

I get:

10
20
30

I need to get the following:

Zona: 10
Zona: 20
Zona: 30

I'm testing with concat () but cardinality fails me, or does not group them, for example:

//producto/concat( 
    "Zona: ", 
    distinct-values( cod_zona/string( ))
)

produces the following output:

Zona: 10
Zona: 10
Zona: 20
Zona: 30

I need a way to do it in XPath, not XQuer-FLWOR:

for $zonas in doc("productos.xml")/distinct-values( //producto/cod_zona ) 
        return concat(
            "Zona: ", 
            $zonas
        )

DOM, or others ... It has didactic purposes and only XPath is supported.

    
asked by Orici 06.02.2017 в 00:50
source

1 answer

0

@Orici, consider the following expression:

for $n in distinct-values( //producto/cod_zona ) return concat('Zona:', $n)

    
answered by 06.02.2017 в 04:04