phax / ph-ubl

Java library for reading and writing UBL 2.0, 2.1, 2.2, 2.3 and 2.4 documents
Apache License 2.0
110 stars 40 forks source link

UBL 2.1 unused namespace #25

Closed patricksan closed 4 years ago

patricksan commented 4 years ago

Dear,

First of all, thanks for your library.

I'm generating one invoice, but I noticed that one namespace is added without being used in the xml:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
          xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
          xmlns:cec="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
          xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
    <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
    ....
    ....

</Invoice>

The xmlns:cec="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" is added as namespace, but there is no reference of cec in the xml.

I'm saving the file using these lines:

var status = UBL21Writer.invoice().write(ublInvoice, file);

Is that a bug?

I'm using: JAVA 11 with ph-ubl 6.2.0

Thank you

patricksan commented 4 years ago

@phax, I tried this, but it didn't work:

MapBasedNamespaceContext context = UBL21NamespaceContext.getInstance().getClone();
context.removeMapping("urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2");
writer.setNamespaceContext(context);

var status = writer.write(ublInvoice, file);

Also this:

final MapBasedNamespaceContext context = UBL21NamespaceContext.getInstance().getClone();
context.clear();
context.addDefaultNamespaceURI("urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
context.addMapping("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2");
context.addMapping("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2");
writer.setNamespaceContext(context);

var status = writer.write(ublInvoice, file);
patricksan commented 4 years ago

links to #10

phax commented 4 years ago

Try

MapBasedNamespaceContext context = UBL21NamespaceContext.getInstance().getClone();
context.removeMapping("cec");
writer.setNamespaceContext(context);
patricksan commented 4 years ago

Thanks @phax I get this exception:

Caused by: java.lang.IllegalStateException: Internal inconsistency removing 'cec' and 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2'
    at com.helger.xml.namespace.MapBasedNamespaceContext.removeMapping(MapBasedNamespaceContext.java:244)

I tried to use getInstance() instead of getInstance().getClone(). It works in the sense to remove the cec namespace but it adds another one:

xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
phax commented 4 years ago

Okay, I can now reproduce the internal error - I will look at this one separately. Basically: never modify the singleton UBL21NamespaceContext.getInstance() - only modify clones.

The system adds the ns2 prefix most likely, because your UBL contains an element that needs the urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2 namespace URI....? Can you paste the full document here?

patricksan commented 4 years ago

Thanks @phax for the support.

The file doesn't contain the ns2. You can see the file here:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
         xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
         xmlns:cec="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
         xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
    <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
    <cbc:ID>2024025255</cbc:ID>
    <cbc:UUID>861dbf17-19a9-4795-b732-004dba0d93cd</cbc:UUID>
    <cbc:IssueDate>2020-01-17</cbc:IssueDate>
    <cbc:DueDate>2020-02-16</cbc:DueDate>
    <cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
    <cbc:Note>note</cbc:Note>
    <cbc:DocumentCurrencyCode>EUR</cbc:DocumentCurrencyCode>
    <cac:InvoicePeriod>
        <cbc:StartDate>2020-01-01</cbc:StartDate>
        <cbc:EndDate>2020-01-31</cbc:EndDate>
    </cac:InvoicePeriod>
    <cac:AccountingSupplierParty>
        <cac:Party>
            <cac:PartyIdentification>
                <cbc:ID schemeID="BE:VAT">BE123456789012</cbc:ID>
            </cac:PartyIdentification>
            <cac:PartyName>
                <cbc:Name>Fake Name</cbc:Name>
            </cac:PartyName>
            <cac:PartyLegalEntity>
                <cac:RegistrationAddress>
                    <cbc:StreetName>streetName</cbc:StreetName>
                    <cbc:CityName>cityName</cbc:CityName>
                    <cbc:PostalZone>postalZone</cbc:PostalZone>
                </cac:RegistrationAddress>
            </cac:PartyLegalEntity>
            <cac:Contact>
                <cbc:Name>Name example</cbc:Name>
                <cbc:Telephone>0032478989497</cbc:Telephone>
                <cbc:Telefax>0032478989498</cbc:Telefax>
                <cbc:ElectronicMail>john@xxxxxx.com</cbc:ElectronicMail>
            </cac:Contact>
        </cac:Party>
    </cac:AccountingSupplierParty>
    <cac:AccountingCustomerParty>
        <cac:Party>
            <cac:PartyIdentification>
                <cbc:ID schemeID="BE:VAT">BE0438700000</cbc:ID>
            </cac:PartyIdentification>
            <cac:PartyName>
                <cbc:Name>XXXXXXX</cbc:Name>
            </cac:PartyName>
            <cac:Language>
                <cbc:ID/>
            </cac:Language>
            <cac:PartyLegalEntity>
                <cac:RegistrationAddress>
                    <cbc:StreetName>XXXXXX 17</cbc:StreetName>
                    <cbc:CityName>XXXXXX</cbc:CityName>
                    <cbc:PostalZone>2900</cbc:PostalZone>
                </cac:RegistrationAddress>
            </cac:PartyLegalEntity>
        </cac:Party>
    </cac:AccountingCustomerParty>
    <cac:Delivery>
        <cbc:ActualDeliveryDate>2020-01-01</cbc:ActualDeliveryDate>
        <cac:DeliveryAddress>
            <cbc:StreetName>XXXXXX</cbc:StreetName>
            <cbc:CityName>XXXXXX</cbc:CityName>
            <cbc:PostalZone>2070</cbc:PostalZone>
        </cac:DeliveryAddress>
    </cac:Delivery>
    <cac:PaymentMeans>
        <cbc:PaymentMeansCode>31</cbc:PaymentMeansCode>
        <cbc:PaymentDueDate>2020-02-16</cbc:PaymentDueDate>
        <cac:PayeeFinancialAccount>
            <cbc:ID schemeName="IBAN">BE34409000000000</cbc:ID>
        </cac:PayeeFinancialAccount>
    </cac:PaymentMeans>
    <cac:TaxTotal>
        <cbc:TaxAmount currencyID="EUR">194.05</cbc:TaxAmount>
        <cac:TaxSubtotal>
            <cbc:TaxableAmount currencyID="EUR">924.05</cbc:TaxableAmount>
            <cbc:TaxAmount currencyID="EUR">194.05</cbc:TaxAmount>
            <cac:TaxCategory>
                <cbc:ID schemeID="UNCL5305">S</cbc:ID>
                <cbc:Percent>21</cbc:Percent>
                <cac:TaxScheme>
                    <cbc:ID>VAT</cbc:ID>
                </cac:TaxScheme>
            </cac:TaxCategory>
        </cac:TaxSubtotal>
    </cac:TaxTotal>
    <cac:LegalMonetaryTotal>
        <cbc:LineExtensionAmount currencyID="EUR">924.05</cbc:LineExtensionAmount>
        <cbc:TaxExclusiveAmount currencyID="EUR">924.05</cbc:TaxExclusiveAmount>
        <cbc:TaxInclusiveAmount currencyID="EUR">194.05</cbc:TaxInclusiveAmount>
        <cbc:PayableAmount currencyID="EUR">1118.1</cbc:PayableAmount>
    </cac:LegalMonetaryTotal>
    <cac:InvoiceLine>
        <cbc:ID>1</cbc:ID>
        <cbc:InvoicedQuantity>31</cbc:InvoicedQuantity>
        <cbc:LineExtensionAmount currencyID="EUR">36</cbc:LineExtensionAmount>
        <cac:InvoicePeriod>
            <cbc:StartDate>2020-01-01</cbc:StartDate>
            <cbc:EndDate>2020-01-31</cbc:EndDate>
        </cac:InvoicePeriod>
        <cac:TaxTotal>
            <cbc:TaxAmount currencyID="EUR">194.05</cbc:TaxAmount>
            <cac:TaxSubtotal>
                <cbc:TaxableAmount currencyID="EUR">924.05</cbc:TaxableAmount>
                <cbc:TaxAmount currencyID="EUR">194.05</cbc:TaxAmount>
                <cac:TaxCategory>
                    <cbc:ID schemeID="UNCL5305">S</cbc:ID>
                    <cbc:Percent>21</cbc:Percent>
                    <cac:TaxScheme>
                        <cbc:ID>VAT</cbc:ID>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </cac:TaxSubtotal>
        </cac:TaxTotal>
        <cac:Item>
            <cbc:Description>Description</cbc:Description>
            <cac:AdditionalItemProperty>
                <cbc:Name>Channel</cbc:Name>
                <cbc:Value>EMA</cbc:Value>
            </cac:AdditionalItemProperty>
            <cac:AdditionalItemProperty>
                <cbc:Name>ArrivalTime</cbc:Name>
                <cbc:Value></cbc:Value>
            </cac:AdditionalItemProperty>
            <cac:AdditionalItemProperty>
                <cbc:Name>StartGlobalInvoicePeriod</cbc:Name>
                <cbc:Value>StartGlobalInvoicePeriodValue</cbc:Value>
            </cac:AdditionalItemProperty>
            <cac:AdditionalItemProperty>
                <cbc:Name>EndGlobalInvoicePeriod</cbc:Name>
                <cbc:Value>EndGlobalInvoicePeriodValue</cbc:Value>
            </cac:AdditionalItemProperty>
            <cac:AdditionalItemProperty>
                <cbc:Name>Annex</cbc:Name>
                <cbc:Value>AnnexValue</cbc:Value>
            </cac:AdditionalItemProperty>
        </cac:Item>
        <cac:Price>
            <cbc:PriceAmount currencyID="EUR">36</cbc:PriceAmount>
            <cbc:BaseQuantity>1</cbc:BaseQuantity>
        </cac:Price>
    </cac:InvoiceLine>
</Invoice>

In my example generated there is no cec.

I see that your code (UBL21NamespaceContext), you initialise:

  protected UBL21NamespaceContext ()
  {
    addMapping ("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
    addMapping ("xs", XMLConstants.W3C_XML_SCHEMA_NS_URI);
    addMapping ("cac", CUBL21.XML_SCHEMA_CAC_NAMESPACE_URL);
    addMapping ("cbc", CUBL21.XML_SCHEMA_CBC_NAMESPACE_URL);
    addMapping ("cec", CUBL21.XML_SCHEMA_CEC_NAMESPACE_URL);
    addMapping ("ds", CXMLDSig.NAMESPACE_URI);
  }

But cec is not removed when not present.

phax commented 4 years ago

That is indeed weird. Well the underlying XML "text" is created by JAXB and we only have limited control over it. So if you remove the "cec" mapping, but JAXB creates the "ns2" namespace prefix, I don't know why:

var writer = UBL21Writer.invoice();
MapBasedNamespaceContext context = UBL21NamespaceContext.getInstance().getClone();
context.removeMapping("cec");
writer.setNamespaceContext(context);

var status = writer.write(ublInvoice, file);

Ah you are using Java 10 or newer??? Are you including JAXB 2.3.x in your code?

patricksan commented 4 years ago

Hi @phax ,

We are using Java 11 ( oracle 11.0.5) In our classpath we have JAXB 2.3.2

I noticed that you create and already close a bug #17. Is it automatically deployed to Maven Repository?

phax commented 4 years ago

17 is still open. Can you explain to be if UBL.PE still requires UBL 2.0 or whether everything is on UBL 2.1 now? My spanish is a bit poor ;-)

patricksan commented 4 years ago

Sorry @phax, I meant https://github.com/phax/ph-commons/issues/17. This one you closed. The open item #17 I have no knowledge about it.

My Spanish is also poor. But I think I could help if you give more details. :-)

phax commented 4 years ago

Ah okay - no release for that one yet. That is an edge case that only occurs, if you modify the singleton directly (so don't call "removeMapping" directory on UBL21NamespaceContext.getInstance()) - if you remove only on the "clone" you are fine.

patricksan commented 4 years ago

Hi @phax, if I use the getClone() as I added in previous message, we get this exception:

Caused by: java.lang.IllegalStateException: Internal inconsistency removing 'cec' and 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2'
    at com.helger.xml.namespace.MapBasedNamespaceContext.removeMapping(MapBasedNamespaceContext.java:244)

That is why I asked about the release.

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.

phax commented 4 years ago

I am nevertheless closing this issue now. Please re-open, if you see the need for that.