ZUGFeRD / ZUV

ZUgferd validator using Verapdf
Apache License 2.0
23 stars 4 forks source link

Validate XRechnung #28

Closed jstaerk closed 4 years ago

jstaerk commented 4 years ago

since the xrechnung xsl files are available in their configuration https://github.com/itplr-kosit/validator-configuration-xrechnung/releases one could check if they could be added e.g. on a warning or notice level

jstaerk commented 4 years ago

actually it's more https://github.com/itplr-kosit/xrechnung-schematron/releases which should be added, some derivative of /schematron/cii/XRechnung-CII-validation.sch .

factur-x.txt is a sample file which correctly fills the report.xml with the message that the seller contact is missing with something like

java -jar saxon9he.jar -o:report.xml -s:factur-x.txt XRechnung-CII-validation.xsl

in ZUV's XMLValidator.java around line 263 there is the additional check for the CEN schematron for Generation 2 (=ZF2 or FX1) files with EN16931 profile, that something like

    validateSchematron(zfXML, "/xslt/XRechnung-CII-validation.xsl", 27);

could be added there after copying the XSLT to src/main/resources/xslt. This would introduce an error type 27 for xrechnung issues. My issue is that this currently causes a nullpointerexception, I assume because XRechnung-CII-validation.xsl is XSLT2: So far I could not convert it to XSLT1, neither could I get ph-schematron to process any XSLT2 files.

jstaerk commented 4 years ago

with this Main

package phtest;

import java.io.StringReader;
import java.util.List;

import javax.xml.transform.stream.StreamSource;

import com.helger.schematron.ISchematronResource;
import com.helger.schematron.svrl.SVRLHelper;
import com.helger.schematron.svrl.jaxb.FailedAssert;
import com.helger.schematron.svrl.jaxb.FiredRule;
import com.helger.schematron.svrl.jaxb.SchematronOutputType;
import com.helger.schematron.xslt.SchematronResourceXSLT;

public class Main {

    public static void main(String[] args) {
        Main m=new Main();
        m.validate();
    }
    public void validate() {
        ISchematronResource aResSCH = null;
        aResSCH = SchematronResourceXSLT.fromClassPath("xr.xsl");
        int firedRules=0; 
        int failedRules=0;
        if (aResSCH != null) {
            if (!aResSCH.isValidSchematron()) {
                throw new IllegalArgumentException("Invalid Schematron!");
            }
            SchematronOutputType sout;
            try {
                sout = aResSCH.applySchematronValidationToSVRL(new StreamSource(this.getClass().getClassLoader().getResourceAsStream("zugferd_2p1_EN16931_Einfach.xml")));
                List<Object> failedAsserts = sout.getActivePatternAndFiredRuleAndFailedAssert();
                if (failedAsserts.size() > 0) {
                    for (Object object : failedAsserts) {
                        if (object instanceof FailedAssert) {
                            FailedAssert failedAssert=(FailedAssert)object;                         System.out.println(SVRLHelper.getAsString(failedAssert.getText()));
                            failedRules++;
                        } else if (object instanceof FiredRule) {
                            firedRules++;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (firedRules == 0) {
                System.out.println("No rules fired");
            }
        }
    }

}

and this POM

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.mustangproject</groupId>
    <artifactId>phtest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.helger</groupId>
            <artifactId>ph-schematron</artifactId>
            <version>5.6.0</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <!-- mvn help:effective-pom will otherwise tell it just defaults to 
                        2.3.2 - which does not release in the maven repo, and neither shows any error 
                        message :-( -->
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-release-plugin</artifactId>
                    <version>2.5.3</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>phtest.main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!-- http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven 
                        mvn clean compile assembly:single -->
                    <!-- or whatever version you use -->
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <configuration>
                    <transformers>
                        <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>phtest.Main</mainClass>
                        </transformer>
                    </transformers>
                    <minimizeJar>false</minimizeJar>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                        <filter>
                            <artifact>log4j:log4j</artifact>
                            <includes>
                                <include>**</include>
                            </includes>
                        </filter>
                        <filter>
                            <artifact>commons-logging:commons-logging</artifact>
                            <includes>
                                <include>**</include>
                            </includes>
                        </filter>
                        <filter>
                            <artifact>com.sun.xml.bind:jaxb-impl</artifact>
                            <includes>
                                <include>**</include>
                            </includes>
                        </filter>
                        <filter>
                            <artifact>com.sun.xml.bind:jaxb-core</artifact>
                            <includes>
                                <include>**</include>
                            </includes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <excludes />
                            </artifactSet>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

and the xslt being generated using java -jar saxon9he.jar -o:/xr.xsl -s:./schematron/cii/XRechnung-CII-validation.sch ./stf-master/iso-schematron-xslt2/iso_svrl_for_xslt2.xsl I get a couple of Error: unrecognized element in ISO Schematron namespace: check spelling and capitalizationparam but a XSLT file which then nullpointerexcepts, when I use the transformation to xslt1 java -jar saxon9he.jar -o:/xr.xsl -s:./schematron/cii/XRechnung-CII-validation.sch ./stf-master/iso-schematron-xslt1/iso_svrl_for_xslt1.xsl I get even more severe errors and no XSLT output at all

jstaerk commented 4 years ago

some updates from https://github.com/phax/ph-schematron/blob/master/ph-schematron/src/test/java/com/helger/schematron/supplementary/Issue16Test.java : pom.xml, java and XSLT files in the project phtest2.zip

jstaerk commented 4 years ago

after a manual attempt Philip Helger has converted the schematron with his maven tasks, which unfortunately does not work when I try it. Nevertheless, I switched back to ph-schematron and I'm now using his xsl from https://github.com/phax/ph-bdve/tree/master/ph-bdve-xrechnung/src/main/resources/schematron/1.2.2 for the time being

jstaerk commented 4 years ago

should now works for XR 1.2.2 and XR 2.0 in mustang 2 alpha https://mustangproject.org