EBG
EBG

Reputation: 51

return different results in different lines

I found similar questions but I tried them and I did not get it to work. I got "ventas" collection and inside the collection I have fours diferents xml files.

These are the xml files:

productos.xml

<productos>
<product categoria="A" pvp="20">
    <codigo>A2015</codigo>
    <nombre>Cuchillas</nombre>
    <stock>100</stock>
</product>
<product categoria="A" pvp="5">
    <codigo>A2005</codigo>
    <nombre>Laminas</nombre>
    <stock>57</stock>
</product>
<product categoria="B" pvp="15">
    <codigo>B1501</codigo>
    <nombre>Comida</nombre>
    <stock>50</stock>
</product>
<product categoria="C" pvp="20">
    <codigo>C2020</codigo>
    <nombre>Sopa</nombre>
    <stock>100</stock>
</product>
<product categoria="A" pvp="100">
    <codigo>A1001</codigo>
    <nombre>Tela</nombre>
    <stock>100</stock>
</product>
</productos>

clientes.xml

<clientes>
<clien numero="01">
    <nombre>Juan</nombre>
    <poblacion>Madrid</poblacion>
    <tlf>111111111</tlf>
    <direccion>Alcala</direccion>
</clien>

<clien numero="02">
    <nombre>Luis</nombre>
    <poblacion>Barcelona</poblacion>
    <tlf>2222222222</tlf>
    <direccion>Canalets</direccion>
</clien>

<clien numero="03">
    <nombre>Sara</nombre>
    <poblacion>Madrid</poblacion>
    <tlf>01012101212</tlf>
    <direccion>Sol</direccion>
</clien>
</clientes>

facturas.xml

<facturas>
<factura numero="100">
    <fecha>01/01/01</fecha>
    <importe>25</importe>
    <numcliente>01</numcliente>
</factura>
<factura numero="101">
    <fecha>01/01/01</fecha>
    <importe>40</importe>
    <numcliente>01</numcliente>
</factura>
<factura numero="102">
    <fecha>01/01/01</fecha>
    <importe>4</importe>
    <numcliente>02</numcliente>
</factura>
<factura numero="103">
    <fecha>01/01/01</fecha>
    <importe>80</importe>
    <numcliente>02</numcliente>
</factura>
<factura numero="104">
    <fecha>01/01/01</fecha>
    <importe>75</importe>
    <numcliente>01</numcliente>
</factura>
</facturas>

detallefacturas.xml

<detallefacturas>
<factura numero="100">
    <codigo>Z</codigo>
    <producto descuento="0">
        <codigo>A2015</codigo>
        <unidades>2</unidades>
        <importe>0</importe>
    </producto>
    <producto descuento="0">
        <codigo>A2005</codigo>
        <unidades>1</unidades>
        <importe>0</importe>
    </producto>
</factura>
<factura numero="101">
    <codigo>X</codigo>
    <producto descuento="0">
        <codigo>B1501</codigo>
        <unidades>5</unidades>
        <importe>0</importe>
    </producto>
    <producto descuento="0">
        <codigo>A2015</codigo>
        <unidades>4</unidades>
        <importe>0</importe>
    </producto>
</factura>
<factura numero="102">
    <codigo>Y</codigo>
    <producto descuento="0">
        <codigo>C2020</codigo>
        <unidades>15</unidades>
        <importe>0</importe>
    </producto>
    <producto descuento="0">
        <codigo>A2015</codigo>
        <unidades>2</unidades>
        <importe>0</importe>
    </producto>
</factura>
<factura numero="103">
    <codigo>Q</codigo>
    <producto descuento="0">
        <codigo>C2020</codigo>
        <unidades>2</unidades>
        <importe>0</importe>
    </producto>
    <producto descuento="0">
        <codigo>A2015</codigo>
        <unidades>2</unidades>
        <importe>0</importe>
    </producto>
</factura>
<factura numero="104">
    <codigo>H</codigo>
    <producto descuento="0">
        <codigo>A1001</codigo>
        <unidades>10</unidades>
        <importe>0</importe>
    </producto>
    <producto descuento="0">
        <codigo>A2015</codigo>
        <unidades>2</unidades>
        <importe>0</importe>
    </producto>
</factura>
<factura numero="105">
    <codigo>U</codigo>
    <producto descuento="0">
        <codigo>C2020</codigo>
        <unidades>2</unidades>
        <importe>0</importe>
    </producto>
</factura>
</detallefacturas>

Last result:

<detalle>
    <codigo>A2015</codigo>
    <nombre>Cuchillas</nombre>
    <cant>2</cant>
    <pvp>20</pvp>
    <importe/>
</detalle>
<detalle>
    <codigo>A2005</codigo>
    <nombre>Cuchillas</nombre>
    <cant>1</cant>
    <pvp>20</pvp>
    <importe/>
</detalle>
<detalle>
    <codigo>A2015</codigo>
    <nombre>Laminas</nombre>
    <cant>2</cant>
    <pvp>5</pvp>
    <importe/>
</detalle>
<detalle>
    <codigo>A2005</codigo>
    <nombre>Laminas</nombre>
    <cant>1</cant>
    <pvp>5</pvp>
    <importe/>
</detalle>

In the last attemp it returns the data in different elements but duplicate all the data. In each element it uses each pvp.

Upvotes: 0

Views: 38

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167696

I think you want to change

for $productos in collection("/ventas")/productos
let $nombre := $productos/product[codigo=$codigos/codigo]/nombre,
    $pvp := $productos/product[codigo=$codigos/codigo]/@pvp

to

for $producto in collection("/ventas")/productos/product[codigo=$codigos/codigo]
let $nombre := $producto/nombre,
    $pvp := $producto/@pvp

As for the further problems raised in a comment, well, you haven't shown your input structure and I think you are trying to solve various problems with one question. Do you now have the right number of detalle elements with the right nombre child data and the right pvp value?

For the codigo to be correct I think you are better off to use

for $p1 in collection("/ventas")/productos/product,
    $p2 in collection("/ventas")/detallefacturas/factura[@numero="100"]/producto where $p1/codigo = $p2/codigo
let $nombre := $producto/nombre,
    $pvp := $producto/@pvp
return 
<detalle>
        <codigo>{data($p2/codigo)}</codigo>
        <nombre>{data($nombre)}</nombre>
        <cant>{data($p2/unidades)}</cant>
        <pvp>{data($pvp)}</pvp>
        <importe></importe>
</detalle>

It might help to edit your question with sample data or ask a new question with sample data on that problem.

Upvotes: 1

Related Questions