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
106 stars 40 forks source link

No Prefix is geting added after adding the namespace context #53

Closed rajeev-jha1988 closed 1 year ago

rajeev-jha1988 commented 1 year ago
final MapBasedNamespaceContext aNSContext = UBL21NamespaceContext.getInstance ().getClone ();
    aNSContext.clear();
    aNSContext.addDefaultNamespaceURI ("urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
    aNSContext.addMapping ("cac", CUBL21.XML_SCHEMA_CAC_NAMESPACE_URL);
    aNSContext.addMapping ("cbc", CUBL21.XML_SCHEMA_CBC_NAMESPACE_URL);
    aNSContext.addMapping ("cec", CUBL21.XML_SCHEMA_CEC_NAMESPACE_URL);
    aNSContext.addMapping ("ds", CXMLDSig.NAMESPACE_URI);

   // typeUBL21WriterBuilder.setMarshallerCustomizer( GenericJAXBMarshaller )
    typeUBL21WriterBuilder.setNamespaceContext( aNSContext );

By default its adding NS in XML

<?xml version="1.0" encoding="UTF-8"?>

Dummy Invoice number 2022-11-16Z 10 10 10 Z 10 VAT 10 1 10 Dependencies: implementation group: 'com.helger.ubl', name: 'ph-ubl21', version: '6.7.0' // https://mvnrepository.com/artifact/com.helger.ubl/ph-ubl21-codelists implementation group: 'com.helger.ubl', name: 'ph-ubl21-codelists', version: '6.7.0'
phax commented 1 year ago

Hi @rajeev-jha1988 please post a bit more of your code.

You can btw. simplify your code if you like:

final MapBasedNamespaceContext aNSContext = new MapBasedNamespaceContext  ();
    aNSContext.addDefaultNamespaceURI ("urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
    aNSContext.addMapping ("cac", CUBL21.XML_SCHEMA_CAC_NAMESPACE_URL);
    aNSContext.addMapping ("cbc", CUBL21.XML_SCHEMA_CBC_NAMESPACE_URL);
    aNSContext.addMapping ("cec", CUBL21.XML_SCHEMA_CEC_NAMESPACE_URL);
    aNSContext.addMapping ("ds", CXMLDSig.NAMESPACE_URI);

It's important that you call .setNamespaceContext BEFORE you call write... or getAs... methods on the marshaller..

rajeev-jha1988 commented 1 year ago

Hi @phax ,

here is code , UBL21WriterBuilder typeUBL21WriterBuilder = UBL21Writer .invoice() ;

    final MapBasedNamespaceContext aNSContext = new MapBasedNamespaceContext  ();
    aNSContext.addDefaultNamespaceURI ("urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
    aNSContext.addMapping ("cac", CUBL21.XML_SCHEMA_CAC_NAMESPACE_URL);
    aNSContext.addMapping ("cbc", CUBL21.XML_SCHEMA_CBC_NAMESPACE_URL);
    aNSContext.addMapping ("cec", CUBL21.XML_SCHEMA_CEC_NAMESPACE_URL);
    aNSContext.addMapping ("ds", CXMLDSig.NAMESPACE_URI);

    typeUBL21WriterBuilder.setNamespaceContext( aNSContext );

    // Write to disk
    final ESuccess eSuccess = typeUBL21WriterBuilder
        .setUseSchema (false)
        .setFormattedOutput (true)
        .write (aInvoice, new File("target/dummy-invoice.xml"));
phax commented 1 year ago

Please also look at #34 - it contains a solution for your problem. Eventually your file does not get written, because the content of aInvoice is not correct. What does ESuccess tell you - is it successful or not?

rajeev-jha1988 commented 1 year ago

Hi @phax , Here is code which i am using now its giving success in ESuccess but still the prefix are dynamic like NS4,NS3 ... ` final String sCurrency = "SAR";

    // Create domain object
    final InvoiceType aInvoice = new InvoiceType ();

   // aInvoice.setUBLExtensions( ublExtensionsType );

    // Fill it
    aInvoice.setID ("Dummy Invoice number");
    aInvoice.setIssueDate ( PDTFactory.getCurrentXMLOffsetDateUTC ());

    final SupplierPartyType aSupplier = new SupplierPartyType ();
    aInvoice.setAccountingSupplierParty (aSupplier);

    final CustomerPartyType aCustomer = new CustomerPartyType ();
    aInvoice.setAccountingCustomerParty (aCustomer);

    final MonetaryTotalType aMT = new MonetaryTotalType ();
    aMT.setPayableAmount ( BigDecimal.TEN).setCurrencyID (sCurrency);
    aInvoice.setLegalMonetaryTotal (aMT);

    final InvoiceLineType aLine = new InvoiceLineType ();
    aLine.setID ("1");

    final ItemType aItem = new ItemType ();
    aLine.setItem (aItem);

    aLine.setLineExtensionAmount (BigDecimal.TEN).setCurrencyID (sCurrency);

    aInvoice.addInvoiceLine (aLine);

    {
        final TaxSubtotalType aTaxSubtotal = new TaxSubtotalType ();
        aTaxSubtotal.setTaxableAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
        aTaxSubtotal.setTaxAmount (BigDecimal.TEN).setCurrencyID (sCurrency);

        final TaxCategoryType aTaxCategory = new TaxCategoryType ();
        final IDType aTCID = new IDType ();
        aTCID.setSchemeID ("UNCL5305");
        aTCID.setSchemeAgencyID ("6");
        aTCID.setValue ("Z");
        aTaxCategory.setID (aTCID);

        aTaxCategory.setPercent (BigDecimal.TEN);

        final TaxSchemeType aTaxScheme = new TaxSchemeType ();
        final IDType aTSID = new IDType ();
        aTSID.setSchemeID ("UNCL5305");
        aTSID.setSchemeAgencyID ("6");
        aTSID.setValue ("VAT");
        aTaxScheme.setID (aTSID);
        aTaxCategory.setTaxScheme (aTaxScheme);

        aTaxSubtotal.setTaxCategory (aTaxCategory);

        final TaxTotalType aTaxTotal = new TaxTotalType ();
        aTaxTotal.setTaxAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
        aTaxTotal.addTaxSubtotal (aTaxSubtotal);
        aInvoice.addTaxTotal (aTaxTotal);
    }

    UBL21WriterBuilder<InvoiceType> typeUBL21WriterBuilder = UBL21Writer
        .invoice()
        ;

    final MapBasedNamespaceContext aNSContext = new MapBasedNamespaceContext  ();
    aNSContext.addDefaultNamespaceURI ("urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
    aNSContext.addMapping ("cac", CUBL21.XML_SCHEMA_CAC_NAMESPACE_URL);
    aNSContext.addMapping ("cbc", CUBL21.XML_SCHEMA_CBC_NAMESPACE_URL);
    aNSContext.addMapping ("cec", CUBL21.XML_SCHEMA_CEC_NAMESPACE_URL);
    aNSContext.addMapping ("ds", CXMLDSig.NAMESPACE_URI);

   // typeUBL21WriterBuilder.setMarshallerCustomizer( GenericJAXBMarshaller )
    typeUBL21WriterBuilder.setNamespaceContext( aNSContext );
   // typeUBL21WriterBuilder.getMarshallerCustomizer()

    // Write to disk
    final ESuccess eSuccess = typeUBL21WriterBuilder
        .setUseSchema (true)
        .setFormattedOutput (true)
        .write (aInvoice, new File("target/dummy-invoice.xml"));
    System.out.println(eSuccess.isSuccess ());`
phax commented 1 year ago

I used your code, create a file and it looks good. In the above commit, the only thing I changed is to use getAsString (...) instead of write (....)

The output in my console is:

<?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:ID>Dummy Invoice number</cbc:ID>
  <cbc:IssueDate>2022-11-17Z</cbc:IssueDate>
  <cac:AccountingSupplierParty />
  <cac:AccountingCustomerParty />
  <cac:TaxTotal>
    <cbc:TaxAmount currencyID="SAR">10</cbc:TaxAmount>
    <cac:TaxSubtotal>
      <cbc:TaxableAmount currencyID="SAR">10</cbc:TaxableAmount>
      <cbc:TaxAmount currencyID="SAR">10</cbc:TaxAmount>
      <cac:TaxCategory>
        <cbc:ID schemeID="UNCL5305" schemeAgencyID="6">Z</cbc:ID>
        <cbc:Percent>10</cbc:Percent>
        <cac:TaxScheme>
          <cbc:ID schemeID="UNCL5305" schemeAgencyID="6">VAT</cbc:ID>
        </cac:TaxScheme>
      </cac:TaxCategory>
    </cac:TaxSubtotal>
  </cac:TaxTotal>
  <cac:LegalMonetaryTotal>
    <cbc:PayableAmount currencyID="SAR">10</cbc:PayableAmount>
  </cac:LegalMonetaryTotal>
  <cac:InvoiceLine>
    <cbc:ID>1</cbc:ID>
    <cbc:LineExtensionAmount currencyID="SAR">10</cbc:LineExtensionAmount>
    <cac:Item />
  </cac:InvoiceLine>
</Invoice>
rajeev-jha1988 commented 1 year ago

Hi @phax, I have checked, when I am using the Project test case it's generating the right XML but when I am using it as Gradle it's not getting generated.

Please check my sample project https://github.com/rajeev-jha1988/xml-parser-ksa/blob/main/src/main/java/org/example/CreateXMLFromScratch.java

phax commented 1 year ago

Sorry, I can't help you here. I don't know Gradle and I don't have the time. What I saw: a) Don't push the folders build or target to GitHub - they should be created on the flay b) It seems like your code is not called from the commandline. Is your "Main" class configured correctly?

rajeev-jha1988 commented 1 year ago

Hi @phax , I checked it . looks like we need to add additional dependency to generate in above mention format , here are dependencies which I have added and after that its generated the XML with proper prefix define in class.

I am adding here to list all the mandatory dependencies for other if they faced same issue.

`

// https://mvnrepository.com/artifact/com.helger.ubl/ph-ubl21
implementation group: 'com.helger.ubl', name: 'ph-ubl21', version: '6.7.0'
// https://mvnrepository.com/artifact/com.helger.ubl/ph-ubl21-codelists
implementation group: 'com.helger.ubl', name: 'ph-ubl21-codelists', version: '6.7.0'
// https://mvnrepository.com/artifact/com.helger.commons/ph-commons
implementation group: 'com.helger.commons', name: 'ph-commons', version: '10.2.2'
// https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl
implementation group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '2.3.5'

`

Thanks for your time

phax commented 1 year ago

Congrats :) Glad to hear that it worked for you and thanks for sharing the solution as well. Very helpful!