migrator improvements less additional namespace prefixes

This commit is contained in:
Jochen Stärk
2017-10-28 23:04:16 +02:00
parent 77df03a98b
commit a9b7c972db
3 changed files with 68 additions and 14 deletions

21
pom.xml
View File

@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.mustangproject.ZUGFeRD</groupId> <groupId>org.mustangproject.ZUGFeRD</groupId>
<artifactId>mustang</artifactId> <artifactId>mustang</artifactId>
<version>1.4.2-SNAPSHOT</version> <version>1.4.3-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Mustang</name> <name>Mustang</name>
<description>The Mustang project is a java library to read and write ZUGFeRD meta data inside your invoice PDFs</description> <description>The Mustang project is a java library to read and write ZUGFeRD meta data inside your invoice PDFs</description>
@@ -44,6 +44,17 @@
<maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.target>1.7</maven.compiler.target>
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency> <dependency>
<groupId>org.glassfish.jaxb</groupId> <groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId> <artifactId>jaxb-runtime</artifactId>
@@ -77,6 +88,12 @@
<artifactId>jargs</artifactId> <artifactId>jargs</artifactId>
<version>2.0-SNAPSHOT</version> <version>2.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>9.5.1-8</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@@ -206,7 +223,7 @@
<target>2.1</target> <target>2.1</target>
<packageName>org.mustangproject.ZUGFeRD.model</packageName> <packageName>org.mustangproject.ZUGFeRD.model</packageName>
<sources> <sources>
<source>src/main/schema/ZUGFeRD1p0.xsd</source> <source>src/main/resources/schema/ZUGFeRD1p0.xsd</source>
</sources> </sources>
</configuration> </configuration>
</plugin> </plugin>

View File

@@ -22,11 +22,12 @@ import javax.xml.transform.stream.StreamSource;
public class ZUGFeRDMigrator { public class ZUGFeRDMigrator {
static final ClassLoader cl = ZUGFeRDMigrator.class.getClassLoader(); static final ClassLoader cl = ZUGFeRDMigrator.class.getClassLoader();
private static final TransformerFactory factory = getTransformerFactory(); private static TransformerFactory factory = null;
private static final String resourcePath = ""; //$NON-NLS-1$ private static final String resourcePath = ""; //$NON-NLS-1$
private static TransformerFactory getTransformerFactory() { private static TransformerFactory getTransformerFactory() {
TransformerFactory fact = TransformerFactory.newInstance(); TransformerFactory fact = new net.sf.saxon.TransformerFactoryImpl();
//TransformerFactory.newInstance();
fact.setURIResolver(new ClasspathResourceURIResolver()); fact.setURIResolver(new ClasspathResourceURIResolver());
return fact; return fact;
} }
@@ -80,6 +81,7 @@ http://countwordsfree.com/xmlviewer
public static void applySchematronXsl(final InputStream xmlFile, public static void applySchematronXsl(final InputStream xmlFile,
final OutputStream EN16931Outstream) throws TransformerException { final OutputStream EN16931Outstream) throws TransformerException {
factory=getTransformerFactory();
Transformer transformer = factory.newTransformer(new StreamSource(cl.getResourceAsStream(resourcePath+"COMFORTtoEN16931.xsl"))); Transformer transformer = factory.newTransformer(new StreamSource(cl.getResourceAsStream(resourcePath+"COMFORTtoEN16931.xsl")));
transformer.transform(new StreamSource(xmlFile), new StreamResult(EN16931Outstream)); transformer.transform(new StreamSource(xmlFile), new StreamResult(EN16931Outstream));
} }

View File

@@ -1,21 +1,47 @@
<xsl:stylesheet version="1.0" <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:12" xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:15"
xmlns:rsm="urn:ferd:CrossIndustryDocument:invoice:1p0"> xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100"
xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:100"
xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
>
<!-- use saxon6 java -cp saxon.jar com.icl.saxon.StyleSheet -o target.xml <!-- use saxon6 java -cp saxon.jar com.icl.saxon.StyleSheet -o target.xml
ZUGFeRD-invoice.xml v1to2.xsl see also http://www.lenzconsulting.com/namespaces-in-xslt/ ZUGFeRD-invoice.xml v1to2.xsl see also http://www.lenzconsulting.com/namespaces-in-xslt/
extension-element-prefixes="exsl str datetime uw" --> extension-element-prefixes="exsl str datetime uw" -->
<xsl:output encoding="UTF-8" indent="yes" method="xml" /> <xsl:output encoding="UTF-8" indent="yes" method="xml" />
<!-- copy elements and all attributes but remove namespaces start
Will otherwise add sth like xmlns:rsm="urn:ferd:CrossIndustryDocument:invoice:1p0" on elements.
src: https://stackoverflow.com/questions/12465002/remove-namespace-declaration-from-xslt-stylesheet-with-xslt
This only seems to work in saxon, in xalan namespaces are still created-->
<!-- Copy elements -->
<xsl:template match="*" priority="-1">
<xsl:element name="{name()}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<!-- Copy all other nodes -->
<xsl:template match="node()|@*" priority="-2">
<xsl:copy />
</xsl:template>
<!-- copy elements and all attributes but remove namespaces end -->
<xsl:template match="//*[local-name() = 'CrossIndustryDocument']"> <xsl:template match="//*[local-name() = 'CrossIndustryDocument']">
<!-- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100" <!-- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100" xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100" --> xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100" -->
<xsl:element name="rsm:CrossIndustryInvoice" > <rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:qdt="urn:un:unece:uncefact:data:Standard:QualifiedDataType:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100">
<xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
Migrated by Mustangproject XSLT
<xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
<xsl:apply-templates select="*" /> <xsl:apply-templates select="*" />
</xsl:element> </rsm:CrossIndustryInvoice>
</xsl:template> </xsl:template>
<!-- xsl:template match="//*[local-name() = 'HeaderExchangedDocument']"> <!-- xsl:template match="//*[local-name() = 'HeaderExchangedDocument']">
@@ -40,6 +66,7 @@
<xsl:apply-templates select="@*|node()" /> <xsl:apply-templates select="@*|node()" />
</ram:RateApplicablePercent> </ram:RateApplicablePercent>
</xsl:template> </xsl:template>
<xsl:template match="//*[local-name() = 'SpecifiedSupplyChainTradeDelivery']"> <xsl:template match="//*[local-name() = 'SpecifiedSupplyChainTradeDelivery']">
<ram:SpecifiedLineTradeDelivery> <ram:SpecifiedLineTradeDelivery>
@@ -107,7 +134,18 @@
<xsl:apply-templates select="@*|node()" /> <xsl:apply-templates select="@*|node()" />
</ram:ApplicableHeaderTradeSettlement> </ram:ApplicableHeaderTradeSettlement>
</xsl:template> </xsl:template>
<!-- rename and add -->
<xsl:template match="//*[local-name() != 'HeaderExchangedDocument']/*[local-name() = 'IssueDateTime']">
<ram:FormattedIssueDateTime">
<qdt:DateTimeString>
<xsl:apply-templates select="@*|node()" />
</qdt:DateTimeString>
</ram:FormattedIssueDateTime>
</xsl:template>
<!-- rename and reorder --> <!-- rename and reorder -->
<xsl:template <xsl:template
match="//*[local-name() = 'SpecifiedSupplyChainTradeTransaction']"> match="//*[local-name() = 'SpecifiedSupplyChainTradeTransaction']">
@@ -144,10 +182,7 @@
<!-- rest --> <!-- rest -->
<!-- this is the identity transform: it copies everything that isn't matched <!-- this is the identity transform: it copies everything that isn't matched
by a more specific template --> by a more specific template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" /><!-- @*|node() -->
</xsl:copy>
</xsl:template>
</xsl:stylesheet> </xsl:stylesheet>