skip to Main Content

I am having problems when "migrating" my query in which I parse a column with XML stored in it, i "migrating" from T-SQL to Postgres, since my current development requires it. In T-SQL this is my current query:

My query in T-SQL, takes a column that contains XML that are retrieved from another system, I create the prefixes for the xml with the "XMLNAMESPACES" and then I do an INSERT INTO to save the query in a previously created table, with the subquery I "choose" the values ​​or "columns" that I want to return, cast them and rename them at the same time. After this I cast the "XML" column to "XML" and it is already in "VARCHAR" and finally the .nodes function is called to return the table derived from the query

WITH XMLNAMESPACES('http://www.sat.gob.mx/cfd/4' AS cfdi, 
                    'http://www.sat.gob.mx/Pagos20' AS pago20, 
                    'http://www.sat.gob.mx/TimbreFiscalDigital' AS tfd)

INSERT INTO [CFDI_SON990511MI0_RECIBIDOS].[dbo].[DataXml_SON990511MI0_RepsRecibidos_2023] (UUID, DoctoRelacionado, NumParcialidad, ImpPagado, FePago)

SELECT 
        prx4_0.*
FROM
(
    SELECT 
            UUID,
            x.value('(@IdDocumento)', 'NVARCHAR(64)') AS DoctoRelacionado,
            x.value('(@NumParcialidad)', 'TINYINT') AS NumParcialidad,
            x.value('(@ImpPagado)', 'DECIMAL(26, 8)') AS ImpPagado,
            x.value('(/cfdi:Comprobante/@Fecha)[1]', 'DATETIME') AS FePago
    FROM [dbo].[XML]
    
    --El casteo ocurre aqui
    CROSS APPLY(SELECT CAST(XML AS XML)) A(TheXml)

    --Se llama a la funcion .nodes para retornar la tabla derivada de la consulta
    CROSS APPLY TheXml.nodes('/cfdi:Comprobante/cfdi:Complemento/pago20:Pagos/pago20:Pago/pago20:DoctoRelacionado') B(x)
) prx4_0
GO

This is an XML to be able to reproduce:

-- DDL and sample data population, start
DECLARE @tbl TABLE (UUID UNIQUEIDENTIFIER PRIMARY KEY, xmldata NVARCHAR(MAX));
INSERT @tbl (UUID, xmldata) VALUES
('AAA1ADA1-9151-4C90-AA62-43590DCC7C81', 
        N'<cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd
            http://www.sat.gob.mx/Pagos http://www.sat.gob.mx/sitio_internet/cfd/Pagos/Pagos10.xsd" 
            LugarExpedicion="37480" TipoDeComprobante="P" Total="0" Moneda="XXX" Certificado="MIIG" 
            SubTotal="0" NoCertificado="00001000000504465028" Sello="V1" Fecha="2023-01-09T17:09:53" Version="3.3" 
            xmlns:cfdi="http://www.sat.gob.mx/cfd/3" xmlns:pago10="http://www.sat.gob.mx/Pagos">
        <cfdi:Emisor Rfc="CARE891026810" Nombre="MARIA ELIZABETH CHAVEZ RAMIREZ" RegimenFiscal="621"></cfdi:Emisor>
        <cfdi:Receptor Rfc="SON990511MI0" Nombre="SONIGAS SA DE CV" UsoCFDI="P01"></cfdi:Receptor>
        <cfdi:Conceptos>
            <cfdi:Concepto ClaveProdServ="84111506" Cantidad="1" ClaveUnidad="ACT" Descripcion="Pago" ValorUnitario="0" Importe="0"></cfdi:Concepto>
        </cfdi:Conceptos>
        <cfdi:Complemento>
            <pago10:Pagos Version="1.0">
                <pago10:Pago FechaPago="2022-12-27T00:00:00" FormaDePagoP="03" MonedaP="MXN" Monto="31508.65">
                    <pago10:DoctoRelacionado IdDocumento="AAA1D7E6-23AD-44FC-A544-9CB47D9CFCCD" MonedaDR="MXN" MetodoDePagoDR="PPD" NumParcialidad="1" ImpSaldoAnt="999.99" ImpPagado="999.99" ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA124C5-48FC-4409-BB76-8A23BF6318F3" MonedaDR="MXN" MetodoDePagoDR="PPD" NumParcialidad="1" ImpSaldoAnt="6904.91" ImpPagado="6904.91" ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA17CA8-222A-4146-A943-58AAFD62C7F3" MonedaDR="MXN" MetodoDePagoDR="PPD" NumParcialidad="1" ImpSaldoAnt="2284.99" ImpPagado="2284.99" ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA111BC-4CE8-4572-8C75-2D673F1ED9F4" MonedaDR="MXN" MetodoDePagoDR="PPD" NumParcialidad="1" ImpSaldoAnt="1800.00" ImpPagado="1800.00" ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA1686A-7605-423D-809F-A6EE240E9587" MonedaDR="MXN" MetodoDePagoDR="PPD" NumParcialidad="1" ImpSaldoAnt="3229.99" ImpPagado="3229.99" ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA1783C-A24A-4DC6-8A87-54591E555CCE" MonedaDR="MXN" MetodoDePagoDR="PPD" NumParcialidad="1" ImpSaldoAnt="2599.99" ImpPagado="2599.99" ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA17618-B009-4A80-8C12-CF7F697A512E" MonedaDR="MXN" MetodoDePagoDR="PPD" NumParcialidad="1" ImpSaldoAnt="3167.95" ImpPagado="3167.95" ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA1DBB5-B99E-4BFB-AD27-FC249EDEEBCE" MonedaDR="MXN" MetodoDePagoDR="PPD" NumParcialidad="1" ImpSaldoAnt="2972.86" ImpPagado="2972.86" ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA19C00-65FB-4E83-9AD7-2EF660694ECF" MonedaDR="MXN" MetodoDePagoDR="PPD" NumParcialidad="1" ImpSaldoAnt="4629.99" ImpPagado="4629.99" ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA1CD78-9581-4F43-8741-8C9506E26B2C" MonedaDR="MXN" MetodoDePagoDR="PPD" NumParcialidad="1" ImpSaldoAnt="2739.99" ImpPagado="2739.99" ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                </pago10:Pago>
            </pago10:Pagos>
            <registrofiscal:CFDIRegistroFiscal xmlns:schemaLocation="http://www.sat.gob.mx/registrofiscal http://www.sat.gob.mx/sitio_internet/cfd/cfdiregistrofiscal/cfdiregistrofiscal.xsd" Version="1.0" Folio="2301200000009898" xmlns:registrofiscal="http://www.sat.gob.mx/registrofiscal">
            </registrofiscal:CFDIRegistroFiscal>
            <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/TimbreFiscalDigitalv11.xsd" Version="1.1" UUID="AAA1ADA1-9151-4C90-AA62-43590DCC7C81" FechaTimbrado="2023-01-09T17:20:57" RfcProvCertif="SAT970701NN3" SelloCFD="V1cULko9bjPwFcWBsjJbPDHo92KXkK2VT12bxm9F+9XiiD4oNDE3mpLaf4L1iKndhotlcx14MphlBAfRBRm/ue+eR6YAR+v+t3ZLXpD0QzhJzKCF0WSIkt7kDMhJmroYluMs+D2GwELC9t/LxhmjY5k5OyEBoQm75hfD1EpfN+j1MzZrRj/DuLLp4oVnb64/wfwKmJ0BxtsR7Dwpe7Q/SvuR7W3GnuRIqL/gFKBfsH7qW9fP+4owoX2iERCX5RWIt+yVOejOGMPlMzE7pdihEuulq32aGV1AlyZNqKG9FukbOnSqRJnZdqmz/VqufWEC1h7C8S105tCuDGfldyDy4Q==" NoCertificadoSAT="00001000000504465028" SelloSAT="U09+TX0gGO45iDdPi0gN9CttScMvfj5G7OF8biKdijrPIWSNO7PzYdoGauKvOP6UCgW/x8MpRN+TfoVvdVEmpFrQC1jD7dpbAqWTeh+q1Qsb+5dm3lbgHxp3ImHACR7DORIxOadF/1oj6s0t69UGkTmsDOs90uVDsLL8RWf/yKTwRN+PpSdqy3FCHLMMOfZKZM76xjIVPQT6Hl2n8HhtaBTL/CiKb095wHTl/mXfX5u7UNlOoUUsG2wMeo+lJOqf5e0M0cJ4V3t5Cqr8ZhZu6atLDBqTSDpDSbJh1f3H9OLqoOTMgzSLkmU0jA+Q0TdknP7ujhkZPK2c/gJaJO91Cw==" />
        </cfdi:Complemento>
    </cfdi:Comprobante>');
-- DDL and sample data population, end

and this is my current code in postgres:

WITH
    XMLNAMESPACES AS (
        SELECT 
            'http://www.sat.gob.mx/cfd/3' AS cfdi, 
            'http://www.sat.gob.mx/Pagos' AS pago10, 
            'http://www.sat.gob.mx/TimbreFiscalDigital' AS tfd,
            'http://www.sat.gob.mx/sitio_internet/cfd/cfdiregistrofiscal/cfdiregistrofiscal.xsd' AS registrofiscal)
            
     SELECT 
        "UUID"
        , unnest(xpath('/cfdi:Comprobante/@Serie',"XML"::xml , '{{cfdi, http://www.sat.gob.mx/cfd/3}}')::varchar[]) AS cfd_serie
    from public."Pago_XML";

This is the error I get when I want to perform the query:

SQL Error [2200M]: ERROR: could not parse XML document¶  Detail: line 1: xmlns:schemaLocation: 'http://www.sat.gob.mx/registrofiscal http://www.sat.gob.mx/sitio_internet/cfd/cfdiregistrofiscal/cfdiregistrofiscal.xsd' is not a valid URI¶p://www.sat.gob.mx/sitio_internet/cfd/cfdiregistrofiscal/cfdiregistrofiscal.xsd"¶

I was already investigating my table already generated in T-SQL, it does not give me this error and it is not possible for me to make a replacement for that "URI" since the table is from a provider. I would greatly appreciate any help, if you need more information about the XML, table or query I can provide it.

This my actual table on T-SQL and and it is also the output I expect with the postgres query

UUID FePago DoctoRelacionado NumParcialidad ImpPagado
0009034a-3feb-4b2e-88fa-4605e5c6a7d4 2023-01-20 12:00:00.000 EE98EEDA-915B-B34D-8FFF-DD7FA49E2490 1 287124.37000000
0010D6C2-AB5E-45F5-B8AB-5EE57C79BAE8 2022-12-28 12:00:00.000 FADF8224-0D47-48E6-B8A4-362861D9EB00 1 16923.24000000
00157E99-984B-42A0-BA55-D57D76E9155E 2023-04-26 12:00:00.000 D912107F-C1E9-4078-8F8D-7D954446CF8A 1 14160.70000000

T-SQL VERSION (SELECT @@VERSION)

version                                                   |
----------------------------------------------------------|
Microsoft SQL Server 2019 (RTM-GDR) (KB5029377) - 15.0.2104.1 (X64)   Aug 16 2023 00:09:21   Copyright (C) 2019 Microsoft Corporation  Developer Edition (64-bit) on Windows 10 Pro 10.0 <X64> (Build 19045: ) (Hypervisor)|

Postgres version (SELECT version();)

version                                                   |
----------------------------------------------------------|
PostgreSQL 15.3, compiled by Visual C++ build 1914, 64-bit|

2

Answers


  1. Chosen as BEST ANSWER

    The answer that @Yitzhak Kavinsky gave me served me perfectly, since there are 3 tables that have to be formed with 3 different XML formats, the first 2 I was successful with their due adjustments. With the latter you have a slight problem when parsing the XML.

    This is the error: ERROR: Column XPath expression returned more than one value.

    The error comes from this "row":

    UUIDRelaciona VARCHAR(64) PATH '/cfdi:Comprobante/cfdi:CfdiRelacionados/cfdi:CfdiRelacionado/@UUID'
    

    When invoking the XMLTABLE function, you specify an XPath expression that generates rows and my the row generation expression is the XPath expression is this:

    '/cfdi:Comprobante/cfdi:Conceptos/cfdi:Concepto/cfdi:Impuestos/cfdi:Traslados/cfdi:Traslado'
    

    I based myself on that documentation to better understand the XMLTABLE:

    https://www.ibm.com/docs/es/i/7.5?topic=sssf-using-xmltable-reference-xml-content-as-relational-table

    I also tried the examples that come there, such as: Returning a row for each value and Handling non-existent path values, but I did not have success. I leave a DDL with a test case.

    CREATE TABLE emp (uuid TEXT NULL, doc xml NULL);
    INSERT INTO emp (uuid, doc) VALUES('126F9C73-DD92-2943-AA07-320EB2955100',
    '<?xml version="1.0" encoding="iso-8859-1"?>
        <cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd" 
        xmlns:cfdi="http://www.sat.gob.mx/cfd/4" Version="4.0" Serie="NC" Folio="1500" Fecha="2023-06-06T14:58:38" Sello="FK" 
        FormaPago="99" NoCertificado="00001000000504344657" 
        Certificado="MIj" SubTotal="9006.47" Moneda="MXN" Total="10447.51" TipoDeComprobante="E" Exportacion="01" MetodoPago="PUE" 
        LugarExpedicion="52160">
            <cfdi:CfdiRelacionados TipoRelacion="01">
                <cfdi:CfdiRelacionado UUID="8D6A947B-55D5-7F47-9541-4DFEDFADFD76" />
                <cfdi:CfdiRelacionado UUID="22F27E3B-938F-A747-873F-CB12D8C3D618" />
                <cfdi:CfdiRelacionado UUID="02FC251C-D156-0F4D-B9D6-0FD3A099CD0E" />
                <cfdi:CfdiRelacionado UUID="36FAACBA-6134-2640-AEB0-07F2C3913E94" />
                <cfdi:CfdiRelacionado UUID="016E4B16-2A41-1746-B5A1-FD34722965F6" />
            </cfdi:CfdiRelacionados>
            <cfdi:Emisor Rfc="CVE160307HY1" Nombre="CADENA 24" RegimenFiscal="601" />
            <cfdi:Receptor Rfc="SON990511MI0" Nombre="SONIGAS" DomicilioFiscalReceptor="37500" RegimenFiscalReceptor="601" UsoCFDI="G02" />
            <cfdi:Conceptos>
                <cfdi:Concepto ClaveProdServ="84111506" Cantidad="1" ClaveUnidad="ACT" Descripcion="Nota de crédito de Interés moratorio 22314 01/feb./2023" ValorUnitario="320.16" Importe="320.16" ObjetoImp="02">
                    <cfdi:Impuestos>
                        <cfdi:Traslados>
                            <cfdi:Traslado Base="320.16" Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="51.23" />
                        </cfdi:Traslados>
                    </cfdi:Impuestos>
                </cfdi:Concepto>
                <cfdi:Concepto ClaveProdServ="84111506" Cantidad="1" ClaveUnidad="ACT" Descripcion="Nota de crédito de Interés moratorio 22988 01/mar./2023" ValorUnitario="1723.86" Importe="1723.86" ObjetoImp="02">
                    <cfdi:Impuestos>
                        <cfdi:Traslados>
                            <cfdi:Traslado Base="1723.86" Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="275.82" />
                        </cfdi:Traslados>
                    </cfdi:Impuestos>
                </cfdi:Concepto>
                <cfdi:Concepto ClaveProdServ="84111506" Cantidad="1" ClaveUnidad="ACT" Descripcion="Nota de crédito de Interés moratorio 25094 11/abr./2023" ValorUnitario="1794.99" Importe="1794.99" ObjetoImp="02">
                    <cfdi:Impuestos>
                        <cfdi:Traslados>
                            <cfdi:Traslado Base="1794.99" Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="287.20" />
                        </cfdi:Traslados>
                    </cfdi:Impuestos>
                </cfdi:Concepto>
                <cfdi:Concepto ClaveProdServ="84111506" Cantidad="1" ClaveUnidad="ACT" Descripcion="Nota de crédito de Interés moratorio 25773 01/may./2023" ValorUnitario="3198.69" Importe="3198.69" ObjetoImp="02">
                    <cfdi:Impuestos>
                        <cfdi:Traslados>
                            <cfdi:Traslado Base="3198.69" Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="511.79" />
                        </cfdi:Traslados>
                    </cfdi:Impuestos>
                </cfdi:Concepto>
                <cfdi:Concepto ClaveProdServ="84111506" Cantidad="1" ClaveUnidad="ACT" Descripcion="Nota de crédito de Interés moratorio 26356 01/jun./2023" ValorUnitario="1968.77" Importe="1968.77" ObjetoImp="02">
                    <cfdi:Impuestos>
                        <cfdi:Traslados>
                            <cfdi:Traslado Base="1968.77" Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="315.00" />
                        </cfdi:Traslados>
                    </cfdi:Impuestos>
                </cfdi:Concepto>
            </cfdi:Conceptos>
            <cfdi:Impuestos TotalImpuestosTrasladados="1441.04">
                <cfdi:Traslados>
                    <cfdi:Traslado Base="9006.47" Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.160000" Importe="1441.04" />
                </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/TimbreFiscalDigitalv11.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="1.1" UUID="126F9C73-DD92-2943-AA07-320EB2955100" FechaTimbrado="2023-06-06T15:00:30" RfcProvCertif="SCD110105654" SelloCFD="FKEzCA" NoCertificadoSAT="00001000000502000436" SelloSAT="XLR" />
            </cfdi:Complemento>
        </cfdi:Comprobante>');
    -- DDL and sample data population, end
    
    
    SELECT 
        uuid
        , t.*
    FROM emp
        , XMLTABLE(
                XMLNAMESPACES('http://www.sat.gob.mx/cfd/4' AS cfdi, 
                        'http://www.sat.gob.mx/TimbreFiscalDigital' AS tfd, 
                        'http://www.sat.gob.mx/ConsumoDeCombustibles11' AS consumodecombustibles11,
                        'http://www.w3.org/2001/XMLSchema-instance' AS xsi,
                        'http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd' AS schemaLocation),
                '/cfdi:Comprobante/cfdi:Conceptos/cfdi:Concepto/cfdi:Impuestos/cfdi:Traslados/cfdi:Traslado'
                PASSING XMLPARSE(DOCUMENT emp.doc)
                COLUMNS
                        UUIDRelaciona VARCHAR(64) PATH '(/cfdi:Comprobante/cfdi:CfdiRelacionados/cfdi:CfdiRelacionado/@UUID)[1]'
                        , TpRelaciona INT PATH '(/cfdi:Comprobante/cfdi:CfdiRelacionados/@TipoRelacion)[1]' 
                        , FeEmisión DATE PATH '/cfdi:Comprobante/@Fecha'
                        , EmisorRfc VARCHAR(32) PATH '/cfdi:Comprobante/cfdi:Emisor/@Rfc' 
                        , EmisorNom VARCHAR(256) PATH '/cfdi:Comprobante/cfdi:Emisor/@Nombre' 
                        , TotalCfdiEgresos DECIMAL(10, 2) PATH '/cfdi:Comprobante/@Total'
                        , EgresoCfdiRelacionado DECIMAL(26, 8) PATH '@Base'
                        , ImporteCfdiRelecionado DECIMAL(26, 8) PATH '@Importe'
                         ) AS t;
    

    This is my current table:

    uuid                                |uuidrelaciona                       |tprelaciona|feemisión |emisorrfc   |emisornom|totalcfdiegresos|egresocfdirelacionado|importecfdirelecionado|
    ------------------------------------|------------------------------------|-----------|----------|------------|---------|----------------|---------------------|----------------------|
    126F9C73-DD92-2943-AA07-320EB2955100|8D6A947B-55D5-7F47-9541-4DFEDFADFD76|          1|2023-06-06|CVE160307HY1|CADENA 24|        10447.51|         320.16000000|           51.23000000|
    126F9C73-DD92-2943-AA07-320EB2955100|8D6A947B-55D5-7F47-9541-4DFEDFADFD76|          1|2023-06-06|CVE160307HY1|CADENA 24|        10447.51|        1723.86000000|          275.82000000|
    126F9C73-DD92-2943-AA07-320EB2955100|8D6A947B-55D5-7F47-9541-4DFEDFADFD76|          1|2023-06-06|CVE160307HY1|CADENA 24|        10447.51|        1794.99000000|          287.20000000|
    126F9C73-DD92-2943-AA07-320EB2955100|8D6A947B-55D5-7F47-9541-4DFEDFADFD76|          1|2023-06-06|CVE160307HY1|CADENA 24|        10447.51|        3198.69000000|          511.79000000|
    126F9C73-DD92-2943-AA07-320EB2955100|8D6A947B-55D5-7F47-9541-4DFEDFADFD76|          1|2023-06-06|CVE160307HY1|CADENA 24|        10447.51|        1968.77000000|          315.00000000|
    

    This is the output I intend to obtain:

    uuid                                |uuidrelaciona                       |tprelaciona|feemisión |emisorrfc   |emisornom|totalcfdiegresos|egresocfdirelacionado|importecfdirelecionado|
    ------------------------------------|------------------------------------|-----------|----------|------------|---------|----------------|---------------------|----------------------|
    126F9C73-DD92-2943-AA07-320EB2955100|8D6A947B-55D5-7F47-9541-4DFEDFADFD76|          1|2023-06-06|CVE160307HY1|CADENA 24|        10447.51|         320.16000000|           51.23000000|
    126F9C73-DD92-2943-AA07-320EB2955100|22F27E3B-938F-A747-873F-CB12D8C3D618|          1|2023-06-06|CVE160307HY1|CADENA 24|        10447.51|        1723.86000000|          275.82000000|
    126F9C73-DD92-2943-AA07-320EB2955100|02FC251C-D156-0F4D-B9D6-0FD3A099CD0E|          1|2023-06-06|CVE160307HY1|CADENA 24|        10447.51|        1794.99000000|          287.20000000|
    126F9C73-DD92-2943-AA07-320EB2955100|36FAACBA-6134-2640-AEB0-07F2C3913E94|          1|2023-06-06|CVE160307HY1|CADENA 24|        10447.51|        3198.69000000|          511.79000000|
    126F9C73-DD92-2943-AA07-320EB2955100|016E4B16-2A41-1746-B5A1-FD34722965F6|          1|2023-06-06|CVE160307HY1|CADENA 24|        10447.51|        1968.77000000|          315.00000000|
    

  2. There is incorrect xsi:schemaLocation values in the XML.

    I had to remove them from the XML.

    PostgreSQL XML shredding is similar to Oracle implementation.

    dbfiddle

    Postres SQL

    -- DDL and sample data population, start
    CREATE TABLE tbl (ID UUID PRIMARY KEY, xmldata VARCHAR(8192));
    INSERT INTO tbl (ID, xmldata) VALUES
    ('AAA1ADA1-9151-4C90-AA62-43590DCC7C81', 
      '<cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      LugarExpedicion="37480" TipoDeComprobante="P" Total="0"
                      Moneda="XXX" Certificado="MIIG" SubTotal="0"
                      NoCertificado="00001000000504465028" Sello="V1"
                      Fecha="2023-01-09T17:09:53" Version="3.3"
                      xmlns:cfdi="http://www.sat.gob.mx/cfd/3"
                      xmlns:pago10="http://www.sat.gob.mx/Pagos">
        <cfdi:Emisor Rfc="CARE891026810" Nombre="MARIA ELIZABETH CHAVEZ RAMIREZ"
                     RegimenFiscal="621"></cfdi:Emisor>
        <cfdi:Receptor Rfc="SON990511MI0" Nombre="SONIGAS SA DE CV" UsoCFDI="P01"></cfdi:Receptor>
        <cfdi:Conceptos>
            <cfdi:Concepto ClaveProdServ="84111506" Cantidad="1" ClaveUnidad="ACT"
                           Descripcion="Pago" ValorUnitario="0" Importe="0"></cfdi:Concepto>
        </cfdi:Conceptos>
        <cfdi:Complemento>
            <pago10:Pagos Version="1.0">
                <pago10:Pago FechaPago="2022-12-27T00:00:00" FormaDePagoP="03"
                             MonedaP="MXN" Monto="31508.65">
                    <pago10:DoctoRelacionado IdDocumento="AAA1D7E6-23AD-44FC-A544-9CB47D9CFCCD"
                                             MonedaDR="MXN" MetodoDePagoDR="PPD"
                                             NumParcialidad="1" ImpSaldoAnt="999.99"
                                             ImpPagado="999.99" ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA124C5-48FC-4409-BB76-8A23BF6318F3"
                                             MonedaDR="MXN" MetodoDePagoDR="PPD"
                                             NumParcialidad="1"
                                             ImpSaldoAnt="6904.91"
                                             ImpPagado="6904.91"
                                             ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA17CA8-222A-4146-A943-58AAFD62C7F3"
                                             MonedaDR="MXN" MetodoDePagoDR="PPD"
                                             NumParcialidad="1"
                                             ImpSaldoAnt="2284.99"
                                             ImpPagado="2284.99"
                                             ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA111BC-4CE8-4572-8C75-2D673F1ED9F4"
                                             MonedaDR="MXN" MetodoDePagoDR="PPD"
                                             NumParcialidad="1"
                                             ImpSaldoAnt="1800.00"
                                             ImpPagado="1800.00"
                                             ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA1686A-7605-423D-809F-A6EE240E9587"
                                             MonedaDR="MXN" MetodoDePagoDR="PPD"
                                             NumParcialidad="1"
                                             ImpSaldoAnt="3229.99"
                                             ImpPagado="3229.99"
                                             ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA1783C-A24A-4DC6-8A87-54591E555CCE"
                                             MonedaDR="MXN" MetodoDePagoDR="PPD"
                                             NumParcialidad="1"
                                             ImpSaldoAnt="2599.99"
                                             ImpPagado="2599.99"
                                             ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA17618-B009-4A80-8C12-CF7F697A512E"
                                             MonedaDR="MXN" MetodoDePagoDR="PPD"
                                             NumParcialidad="1"
                                             ImpSaldoAnt="3167.95"
                                             ImpPagado="3167.95"
                                             ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA1DBB5-B99E-4BFB-AD27-FC249EDEEBCE"
                                             MonedaDR="MXN" MetodoDePagoDR="PPD"
                                             NumParcialidad="1"
                                             ImpSaldoAnt="2972.86"
                                             ImpPagado="2972.86"
                                             ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA19C00-65FB-4E83-9AD7-2EF660694ECF"
                                             MonedaDR="MXN" MetodoDePagoDR="PPD"
                                             NumParcialidad="1"
                                             ImpSaldoAnt="4629.99"
                                             ImpPagado="4629.99"
                                             ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                    <pago10:DoctoRelacionado IdDocumento="AAA1CD78-9581-4F43-8741-8C9506E26B2C"
                                             MonedaDR="MXN" MetodoDePagoDR="PPD"
                                             NumParcialidad="1"
                                             ImpSaldoAnt="2739.99"
                                             ImpPagado="2739.99"
                                             ImpSaldoInsoluto="0"></pago10:DoctoRelacionado>
                </pago10:Pago>
            </pago10:Pagos>
            <registrofiscal:CFDIRegistroFiscal Version="1.0"
                                               Folio="2301200000009898"
                                               xmlns:registrofiscal="http://www.sat.gob.mx/registrofiscal">
            </registrofiscal:CFDIRegistroFiscal>
            <tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital"
                                     Version="1.1"
                                     UUID="AAA1ADA1-9151-4C90-AA62-43590DCC7C81"
                                     FechaTimbrado="2023-01-09T17:20:57"
                                     RfcProvCertif="SAT970701NN3"
                                     SelloCFD="V1cULko9bjPwFcWBsjJbPDHo92KXkK2VT12bxm9F+9XiiD4oNDE3mpLaf4L1iKndhotlcx14MphlBAfRBRm/ue+eR6YAR+v+t3ZLXpD0QzhJzKCF0WSIkt7kDMhJmroYluMs+D2GwELC9t/LxhmjY5k5OyEBoQm75hfD1EpfN+j1MzZrRj/DuLLp4oVnb64/wfwKmJ0BxtsR7Dwpe7Q/SvuR7W3GnuRIqL/gFKBfsH7qW9fP+4owoX2iERCX5RWIt+yVOejOGMPlMzE7pdihEuulq32aGV1AlyZNqKG9FukbOnSqRJnZdqmz/VqufWEC1h7C8S105tCuDGfldyDy4Q=="
                                     NoCertificadoSAT="00001000000504465028"
                                     SelloSAT="U09+TX0gGO45iDdPi0gN9CttScMvfj5G7OF8biKdijrPIWSNO7PzYdoGauKvOP6UCgW/x8MpRN+TfoVvdVEmpFrQC1jD7dpbAqWTeh+q1Qsb+5dm3lbgHxp3ImHACR7DORIxOadF/1oj6s0t69UGkTmsDOs90uVDsLL8RWf/yKTwRN+PpSdqy3FCHLMMOfZKZM76xjIVPQT6Hl2n8HhtaBTL/CiKb095wHTl/mXfX5u7UNlOoUUsG2wMeo+lJOqf5e0M0cJ4V3t5Cqr8ZhZu6atLDBqTSDpDSbJh1f3H9OLqoOTMgzSLkmU0jA+Q0TdknP7ujhkZPK2c/gJaJO91Cw=="/>
        </cfdi:Complemento>
    </cfdi:Comprobante>');
    -- DDL and sample data population, end
    
    SELECT ID, t.* 
    FROM tbl
      , XMLTABLE(
            xmlnamespaces('http://www.sat.gob.mx/cfd/3' as "cfdi",
                'http://www.sat.gob.mx/Pagos' as "pago10"),
           '/cfdi:Comprobante/cfdi:Complemento/pago10:Pagos/pago10:Pago/pago10:DoctoRelacionado'
           PASSING XMLPARSE(DOCUMENT tbl.xmldata)
           COLUMNS 
                   IdDocumento varchar(64) PATH '@IdDocumento',
                 NumParcialida int         PATH '@NumParcialidad'
    ) as t;
    

    Output

    id iddocumento numparcialida
    aaa1ada1-9151-4c90-aa62-43590dcc7c81 AAA1D7E6-23AD-44FC-A544-9CB47D9CFCCD 1
    aaa1ada1-9151-4c90-aa62-43590dcc7c81 AAA124C5-48FC-4409-BB76-8A23BF6318F3 1
    aaa1ada1-9151-4c90-aa62-43590dcc7c81 AAA17CA8-222A-4146-A943-58AAFD62C7F3 1
    aaa1ada1-9151-4c90-aa62-43590dcc7c81 AAA111BC-4CE8-4572-8C75-2D673F1ED9F4 1
    aaa1ada1-9151-4c90-aa62-43590dcc7c81 AAA1686A-7605-423D-809F-A6EE240E9587 1
    aaa1ada1-9151-4c90-aa62-43590dcc7c81 AAA1783C-A24A-4DC6-8A87-54591E555CCE 1
    aaa1ada1-9151-4c90-aa62-43590dcc7c81 AAA17618-B009-4A80-8C12-CF7F697A512E 1
    aaa1ada1-9151-4c90-aa62-43590dcc7c81 AAA1DBB5-B99E-4BFB-AD27-FC249EDEEBCE 1
    aaa1ada1-9151-4c90-aa62-43590dcc7c81 AAA19C00-65FB-4E83-9AD7-2EF660694ECF 1
    aaa1ada1-9151-4c90-aa62-43590dcc7c81 AAA1CD78-9581-4F43-8741-8C9506E26B2C 1
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search