phax / ph-schematron

Java Schematron library that supports XSLT and native application
Apache License 2.0
116 stars 36 forks source link

Rule not being applied with PH-Schematron 5.2.0 #88

Closed jorgeimar closed 1 year ago

jorgeimar commented 5 years ago

Hello, im using ph-schematron to validate data in an xml file but i found that the following rule is not being applied:

        <sch:rule context="/cfdi:Comprobante/cfdi:Conceptos/cfdi:Concepto/cfdi:Impuestos/cfdi:Traslados/cfdi:Traslado[@TipoFactor!='Exento']">
            <sch:let name="impuesto" value="@Impuesto"/>
            <sch:let name="tasaOCuota" value="@TasaOCuota"/>
            <sch:report flag="CFDI33195" test="count(/cfdi:Comprobante/cfdi:Impuestos/cfdi:Traslados/cfdi:Traslado[@Impuesto = $impuesto and @TasaOCuota = $tasaOCuota]) = 0">El campo Importe correspondiente a Traslado no es igual a la suma de los importes de los impuestos trasladados registrados en los conceptos donde el impuesto del concepto sea igual al campo impuesto de este elemento y la TasaOCuota del concepto sea igual al campo TasaOCuota de este elemento.</sch:report>
        </sch:rule>

And im using the following xml:


<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3"
    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"
    Version="3.3" Serie="SX" Folio="2495" Fecha="2019-09-23T17:29:56"
    Sello="Hssq2E7KeNs0z=="
    FormaPago="99" NoCertificado="00001000000400000000"
    Certificado="MLOPbDCCBFSgAwIBAgIUMDAwMDEwMDAwMD=="
    CondicionesDePago="09" SubTotal="6270.93" Moneda="MXN" TipoCambio="1" Total="6321.20"
    TipoDeComprobante="I" MetodoPago="PPD" LugarExpedicion="42080">
    <cfdi:Emisor Nombre="AASASAS" Rfc="CAAK090911P85"
        RegimenFiscal="601"/>
    <cfdi:Receptor Rfc="CAAK090911P85" Nombre="AASASAS"
        UsoCFDI="G01"/>
    <cfdi:Conceptos>
        <cfdi:Concepto ClaveProdServ="50192300" Cantidad="6" ClaveUnidad="XBX" Unidad="CAJ"
            Descripcion="MIX WAFFLE" ValorUnitario="108.13" Importe="648.78">
            <cfdi:Impuestos>
                <cfdi:Traslados>
                    <cfdi:Traslado Base="648.78" Impuesto="002" TipoFactor="Tasa"
                        TasaOCuota="0.000000" Importe="0.00"/>
                    <cfdi:Traslado Base="600.72" Impuesto="003" TipoFactor="Tasa"
                        TasaOCuota="0.080000" Importe="48.06"/>
                </cfdi:Traslados>
            </cfdi:Impuestos>
        </cfdi:Concepto>
    </cfdi:Conceptos>
    <cfdi:Impuestos TotalImpuestosTrasladados="50.27">
        <cfdi:Traslados>
            <cfdi:Traslado Impuesto="002" TipoFactor="Tasa" TasaOCuota="0.000000" Importe="0.00"/>
        </cfdi:Traslados>
    </cfdi:Impuestos>
</cfdi:Comprobante>

When i use ph-schematron (ver 5.2.0) i got no errors but i expect to get the report flag CFDI33195. Using camel-schematron i got the flag i want. I really want to use PH-Schematron, could you help me?.

Thanks in advance.

phax commented 5 years ago

That is indeed weird. When using the "Pure" implementation, it works, when switching to the XSLT-based version it fails. I'm trying to find out what's going wrong.

jorgeimar commented 5 years ago

Hello Philip,

     Thanks for your help, i didnt notice about the pure implementation, ill check that. I’ll wait for your comments about the xslt based versión.

Regards!!!

Enviado desde mi iPhone

El 25 sep 2019, a la(s) 5:04, Philip Helger notifications@github.com escribió:

That is indeed weird. When using the "Pure" implementation, it works, when switching to the XSLT-based version it fails. I'm trying to find out what's going wrong.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

jorgeimar commented 5 years ago

Hello Philip, have you found why the xslt-based version doesn't work?.

Thanks in advace

phax commented 5 years ago

No unfortunately not, I am stuck - something with the namespaces but I don't know what :( Its weird..

AndrewSales commented 5 years ago

Hi, I tried this out with yesterday's 5.2.1 anttask snapshot.

Using SchematronResourcePure, I get no failed-assertions and no successful-reports. Using SchematronResourceSCH instead, I get a successful-report, whose flag is however ERROR, in light of the log message:

[main] WARN com.helger.schematron.svrl.DefaultSVRLErrorLevelDeterminator - Cannot convert the SVRL flag 'CFDI33195' to an error level. Using default error level instead!

So I'm a bit confused... it seems to me the XSLT version does produce the expected output, albeit with a modified flag value. But I guess the point is that both "pure" and XSLT-based should tally with one another :)

phax commented 5 years ago

So I think I tracked down the problem. The XSLT based version is handling the LET differently than the pure version:

      <sch:let name="impuesto" value="@Impuesto"/>
      <sch:let name="tasaOCuota" value="@TasaOCuota"/>

In XSLT the "@Impuesto" is actually resolved to the value of the XML attribute "Impuesto", in the pure implementation it uses "@Impuesto" as a String --> the time of evaluation of "@Impuesto" is different which creates the difference.

This is a bug in the pure implementation. For my own reference:

The ISO 2016 spec states on that in general:

The variable is substituted into assertion tests and other expressions in the same rule before the test or expression is evaluated. The query language binding specifies which lexical conventions are used to detect references to variables.

Default query language binding states:

The let value is interpreted according to Production 14 of XPath, as returning a string value.

and

A Schematron let expression is treated as an XSLT1 variable. The XSLT1 $ delimiter signifies the use of a variables in an context expression, assertion test, name query, value-of query or let expression. The character not followed by the name of an in-scope variable shall be treated as a literal character.

Query language binding for XSLT 2 states:

The let value is interpreted according to Production 1 of XPath2.

and

A Schematron let expression is treated as an XSLT2 variable. The XSLT2 $ delimiter signifies the use of a variables in an context expression, assertion test, name query, value-of query or let expression. The character not followed by the name of an in-scope variable shall be treated as a literal character.

Query language binding for XPath 2 states:

The let element should not be used.

Query language binding for EXSLT states:

The let value is interpreted according to Production 14 of XPath, as returning a string value.

and

A Schematron let expression is treated as an XSLT1 variable. The XSLT1 $ delimiter signifies the use of a variables in an context expression, assertion test, name query, value-of query or let expression. The character not followed by the name of an in-scope variable shall be treated as a literal character.

Query language binding for STX states:

The let value is interpreted according to Production 1 of STX,[3] as returning a string value.

and

A Schematron let expression is treated as an STX[3] variable. The STX[3] $ delimiter signifies the use of a variables in an context expression, assertion test, name query, value-of query or let expression. The character not followed by the name of an in-scope variable shall be treated as a literal character.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

pigelvy commented 1 year ago

Hi all,

I've been struggling with pure Schematron, XPath2 and variables. Today, in the logs, I noticed that variables are not properly substituted in the XPath filter predicates of my rule tests.

Is there any technical barrier that makes this issue unfixable?

I'm new to Schematron but this issue feels like a very serious limitation of Pure Schematron. I'm using ph-schematron 7.0.1.

phax commented 1 year ago

Yes, that is a very severe limitation to pure-schematron - unfortunately. Please prefer the ISO Schematron solution, unless you are sure, you don't need that feature :(

The evaluation of XPath in Schematron variables will be a performance penalty - maybe this can be done with an opt-in/opt-out mechanism

phax commented 1 year ago

Closing this in favour of the newer #143 - tackle it there.

phax commented 8 months ago

Part of the v8.0.0 release - finally