Converting xml to csv from XQuery in BaseX

非 Y 不嫁゛ 提交于 2021-02-07 08:37:41

问题


The reason for which I'm writing is because I have a database of approx 1k of xmls in BaseX. I already cleaned it and got the info I wanted. Nevertheless, I want to be able to export that data in the CSV format. When I tried with the code given in the BaseX documentation it gave me this:

Desc,Cantidad,Valor,Fecha,Lugar,UUID
,,,,,
,,,,,

And it should show the data between the commas. What I am asking is how should I modify my code in order for the data to appear.

My code is the following:

declare namespace tfd="http://www.sat.gob.mx/TimbreFiscalDigital";
declare namespace cfdi= "http://www.sat.gob.mx/cfd/3";

declare option output:method "csv";
declare option output:csv "header=yes, separator=comma";

    declare context item := document {
<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 $x in //cfdi:Comprobante,
$y in $x//cfdi:Conceptos/cfdi:Concepto,
$z in $x//cfdi:Complemento/tfd:TimbreFiscalDigital,
$w in $x//cfdi:ExpedidoEn
return 
<csv>
    <Desc>{$y/@descripcion}</Desc>
    <Cantidad>{$y/@cantidad}</Cantidad>
    <Valor>{$y/@valorUnitario}</Valor>
    <Fecha>{$z/@FechaTimbrado}</Fecha>
    <Lugar>{$w/@codigoPostal}</Lugar>
    <UUID>{$z/@UUID}</UUID>
</csv>

回答1:


The XML required for CSV serialization is described in the CSV module documentation. The example from the BaseX documentation:

<csv>
  <record>
    <Name>Huber</Name>
    <First_Name>Sepp</First_Name>
    <Address>Hauptstraße 13</Address>
    <City>93547 Hintertupfing</City>
  </record>
</csv>

With other words, a <csv/> element is wrapping all data, each record is separated by <csv/> elements and finally columns are stored as elements with text node children.

Looking at your current output (by removing the serialization option), following is returned:

<csv>
  <Desc descripcion="CONSUMO"/>
  <Cantidad cantidad="1.00"/>
  <Valor valorUnitario="156.8966"/>
  <Fecha FechaTimbrado="2014-07-01T08:15:15"/>
  <Lugar codigoPostal="39690"/>
  <UUID UUID="8a4f1be7-bdcb-4b22-b144-b3e41dd92e69"/>
</csv>

You don't match the requirements in two ways:

  • no <record/> elements
  • values stored as attributes, not text nodes

If you correct both of these problems with a query like:

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 
<csv><record>
    <Desc>{$y/@descripcion/data()}</Desc>
    <Cantidad>{$y/@cantidad/data()}</Cantidad>
    <Valor>{$y/@valorUnitario/data()}</Valor>
    <Fecha>{$z/@FechaTimbrado/data()}</Fecha>
    <Lugar>{$w/@codigoPostal/data()}</Lugar>
    <UUID>{$z/@UUID/data()}</UUID></record>
</csv>

you get the expected CSV output:

Desc,Cantidad,Valor,Fecha,Lugar,UUID
CONSUMO,1.00,156.8966,2014-07-01T08:15:15,39690,8a4f1be7-bdcb-4b22-b144-b3e41dd92e69



回答2:


open the file in an editor and replace all <\ by ;<\
next open the file in BaseX and export as csv



来源:https://stackoverflow.com/questions/37845324/converting-xml-to-csv-from-xquery-in-basex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!