1
votes

I was wondering if someone could help me. My problem is that i'm currently using BaseX which has an XQuery editor. The problem that has come to my hand is that I have a large set of XMLs and I want to reduce them. Nevertheless, some of the information is on different nodes, and when I try to retrieve them they get permutated.

The code that I used to retrieve the data was:

    declare namespace tfd="http://www.sat.gob.mx/TimbreFiscalDigital";
    declare namespace cfdi= "http://www.sat.gob.mx/cfd/3";
    declare context item := document {  <?xml version="1.0" encoding="utf-8"?>
    <cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xmlns:cfdi="http://www.sat.gob.mx/cfd/3"     xsi:schemaLocation="http://www.sat.gob.mx/cfd/3   http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd  http://www.sat.gob.mx/implocal  http://www.sat.gob.mx/sitio_internet/cfd/implocal/implocal.xsd "   xmlns:implocal="http://www.sat.gob.mx/implocal" version="3.2" serie="PUELNR"   folio="3103" fecha="2014-08-12T19:48:57"  formaDePago="PAGO EN UNA SOLA   EXHIBICION" noCertificado="00001000000301760734" subTotal="221.55"  descuento="0.00" TipoCambio="1.00" Moneda="MXN" total="257.00"  tipoDeComprobante="ingreso" metodoDePago="NO IDENTIFICADO"  LugarExpedicion="MÉXICO, PUEBLA">
     <cfdi:Emisor rfc="DBM131209N41" nombre="DEUTSCHE BANK MÉXICO, SA, INST DE    BANCA MÚLTIPLE, DIVISIÓN FIDUCIARIA F/1765">
     <cfdi:DomicilioFiscal calle="RICARDO MARGAIN ZOZAYA" noExterior="605"  colonia="SANTA ENGRACIA" municipio="SAN PEDRO GARZA GARCIA" estado="NUEVO LEÓN"  pais="MÉXICO" codigoPostal="66267" />
     <cfdi:ExpedidoEn calle="CIRCUITO JUAN PABLO II" noExterior="1936" colonia="EX. HACIENDA LA NORIA" municipio="PUEBLA" estado="PUEBLA" pais="MÉXICO"  codigoPostal="72410" />
     <cfdi:RegimenFiscal Regimen="General de Ley P. M." />
  </cfdi:Emisor>
    <cfdi:Receptor rfc="MER551201D48" nombre="MERCK SA DE CV">
    <cfdi:Domicilio calle="CALLE 5" noExterior="7" colonia="FRACC.IND.ALCE BLANCO" municipio="NAUCALPAN" estado="ESTADO DE MÉXICO" pais="MÉXICO" codigoPostal="53370" />
    </cfdi:Receptor>
     <cfdi:Conceptos>
     <cfdi:Concepto cantidad="1.00" unidad="SERVICIO" noIdentificacion="0002" descripcion="CONSUMO" valorUnitario="221.55" importe="221.55" />
     </cfdi:Conceptos>
     <cfdi:Impuestos totalImpuestosTrasladados="35.45">
     <cfdi:Traslados>
      <cfdi:Traslado impuesto="IVA" tasa="16.00" importe="35.45" />
     </cfdi:Traslados>
    </cfdi:Impuestos>
    <cfdi:Complemento>
    <implocal:ImpuestosLocales version="1.0" TotaldeRetenciones="0" TotaldeTraslados="0" />
  <tfd:TimbreFiscalDigital version="1.0" UUID="c286f974-9779-4a7f-82aa-1bd2a3764bfc" FechaTimbrado="2014-08-12T20:52:48" selloCFD="dQctOu630HzV6ZOJ7LhEXoGUDl7SHc/9PTzWGc9InJLzrj3eZ30Pk5e0P2j+UHjPcDMxxrgHd9m1nyGY54hAu8hBg9LXU8ErvmuBOF6XnIMvcHGtvKqoW9PEUCtAB8NtuvFKiZ5ARWb7K4y261qN8SiEoJxZm/d856jLqagSGHo=" noCertificadoSAT="00001000000300171326" selloSAT="PXBVJ7lpB9QEqtkKEmsBKcD77P2GwXNlidxGg3/R6OTRCm7/T0Rc8QPeUSBgCVSjHKN/VRKDAtBtE9pSHY0fSUTe3z7QfmuVmE3GV52T/K0fmFAUmfG0jYQTj9gOX4Z3lb2Mx+QRV+JvQO2sSnq16qco3cdSo2+tqeHFdawkR0k=" xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/TimbreFiscalDigital/TimbreFiscalDigital.xsd" /></cfdi:Complemento>
  <cfdi:Addenda xmlns:ecfd="http://www.southconsulting.com/schemas/strict" xsi:schemaLocation="http://www.southconsulting.com/schemas/strict http://www.southconsulting.com/schemas/strict/ecfd.xsd">
  </cfdi:Addenda>
</cfdi:Comprobante>}
    declare context item := document {

     <?xml version="1.0" encoding="UTF-8"?>

<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3" xmlns:detallista="http://www.sat.gob.mx/detallista" xmlns:psgecfd="http://www.sat.gob.mx/psgecfd" xmlns:ecc="http://www.sat.gob.mx/ecc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.2" folio="131556" fecha="2014-07-01T08:13:34" noCertificado="00001000000301092647" formaDePago="Pago en una sola exhibicion" subTotal="156.8966" descuento="0.00" Moneda="NAL" total="182.00" tipoDeComprobante="ingreso" xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd http://www.posadas.com/MFE http://www.posadas.com/MFE/ADDENDAPOSADASVL1.xsd" xmlns:posadas="http://www.posadas.com/MFE"  metodoDePago="TARJETA DE CREDITO" NumCtaPago="1093" LugarExpedicion="AVENIDA COSTERA MIGUEL ALEMAN ACAPULCO DE JUAREZ GUERRERO MEXICO">

    <cfdi:Emisor rfc="ASO0408178B2" nombre="HOTELES Y VILLAS POSADAS, S.A. DE C.V.">

        <cfdi:DomicilioFiscal calle="AVENIDA PASEO DE LA REFORMA" noExterior="155" noInterior="PISO 4" colonia="LOMAS DE CHAPULTEPEC I SECCION" municipio="MIGUEL HIDALGO" estado="DISTRITO FEDERAL" pais="MEXICO" codigoPostal="11000"/>

        <cfdi:ExpedidoEn calle="AVENIDA COSTERA MIGUEL ALEMAN" noExterior="97" colonia="FRACCIONAMIENTO CLUB DEPORTIVO" municipio="ACAPULCO DE JUAREZ" pais="MEXICO" codigoPostal="39690" estado="GUERRERO"/>

        <cfdi:RegimenFiscal Regimen="NA"/>

    </cfdi:Emisor>

    <cfdi:Receptor rfc="MER551201D48" nombre="MERCK SA DE CV">

        <cfdi:Domicilio calle="CALLE 5 No. 7 FRACC. ALCE BLANCO" municipio="NAUCALPAN DE JUAREZ" estado="ESTADO DE MEXICO" pais="MX" codigoPostal="53370"/>

    </cfdi:Receptor>

    <cfdi:Conceptos>

        <cfdi:Concepto cantidad="1.00" unidad="SERVICIO" noIdentificacion="CONSUM" descripcion="CONSUMO" valorUnitario="156.8966" importe="156.8966"/>

    </cfdi:Conceptos>

    <cfdi:Impuestos totalImpuestosTrasladados="25.1034">

        <cfdi:Traslados>

            <cfdi:Traslado tasa="16.00" importe="25.1034" impuesto="IVA"/>

        </cfdi:Traslados>

    </cfdi:Impuestos>  <cfdi:Complemento>

    <tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/cfd/TimbreFiscalDigital/TimbreFiscalDigital.xsd" FechaTimbrado="2014-07-01T08:15:15" UUID="8a4f1be7-bdcb-4b22-b144-b3e41dd92e69" version="1.0"/>

  </cfdi:Complemento>

<cfdi:Addenda>
<posadas:RequestForPayment>
<posadas:Hoteleria cajero="210" folio="539809" importeaPagar="         200.00" propina="     18.00" > 
</posadas:Hoteleria>
</posadas:RequestForPayment>
</cfdi:Addenda>
</cfdi:Comprobante>}
    for $z in //tfd:TimbreFiscalDigital,
    $x in //cfdi:Conceptos/cfdi:Concepto[(@cantidad>0 and      @importe > 0)],
    $y in c//cfdi:ExpedidoEn
    return 
      <Factura>
        <Desc>{$x/@descripcion}</Desc>
        <Cantidad>{$x/@cantidad}</Cantidad>
        <Valor>{$x/@valorUnitario}</Valor>
        <CP>{$y/@codigoPostal}</CP>
        <UUID>{$z/@UUID}</UUID>
        <Fecha>{$z/@FechaTimbrado}</Fecha>
      </Factura>

Now what I expected was for it to return two sets per XML in the pack I have, so I would have 2k of here. Nevertheless I received like 200k sets cause it mixes every $z with every $x and every $y in each possible way. I want it to respect each XML, if it takes data from an XML, all the other data must be from the same XML.

I hope I made myself clear enough, thank you for your help.

1
In the future, make sure your code samples comply with MCVE rules -- that means, in part, that they need to be verifiable -- ie. they need to create your actual problem. Something that isn't valid XML but is pseudocode can't be run through an XQuery processor by someone who wants to test that their fix worked. See stackoverflow.com/help/mcve for more. - Charles Duffy
Anyhow, right now, your code is very much not clear enough. It's using "element" and "attribute" interchangeably in the example, yet the queries only look for elements, not attributes. For instance, one thing that might be a possible fix would be to make your queries for $y and $z relative to the location of $x across a different axis, but that makes an assumption about your data's layout that the question's content is insufficient to either confirm or deny. - Charles Duffy
If the only available option is to index over the three collections simultaneously rather than doing a cross product (x[1], y[1], z[1]; x[2], y[2], z[2]; etc), that's possible as well, but it's considerably more error-prone should your data have contents that don't comply with the structure you expect, so we'd want to know more about the structure of your data to know if it's the only option. - Charles Duffy
Ill attach an original xml and the code I used, thank you! - Antonio López Ruiz
So -- I had to change that a little to run, passing the XML as a context item and removing the external document contents, but it doesn't yield results as reformed, apparently because there is no cfdi:ExpeditoEn in the document as given. - Charles Duffy

1 Answers

1
votes

I found my own answer, for anybody interested in how to do it, I will post the code:

for $x in //cfdi:Comprobante,
    $y in $x//cfdi:Conceptos/cfdi:Concepto,
    $z in $x//cfdi:Complemento/tfd:TimbreFiscalDigital,
    $w in $x//cfdi:ExpedidoEn
return 
  <Factura>
    <Desc>{$y/@descripcion}</Desc>
    <Cantidad>{$y/@cantidad}</Cantidad>
    <Valor>{$y/@valorUnitario}</Valor>
    <Fecha>{$z/@FechaTimbrado}</Fecha>
    <Lugar>{$w/@codigoPostal}</Lugar>
    <UUID>{$z/@UUID}</UUID>       
  </Factura>

Thus, instead of having $w, $y and $z all searching from the root of the document (ie. //cfdi:ExpedidoEn), they're searching from their parent ($x//cfdi:ExpedidoEn).