phax / ph-ubl

Java library for reading and writing UBL 2.0, 2.1, 2.2, 2.3 and 2.4 (CS01) documents

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No Prefix is geting added after adding the namespace context

rajeev-jha1988 opened this issue · comments

commented
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

<ns4:Invoice xmlns:ns4="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:ns3="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
ns2:UBLExtensions
ns2:UBLExtension
<ns2:ExtensionContent />
</ns2:UBLExtension>
</ns2:UBLExtensions>
Dummy Invoice number
2022-11-16Z
<ns3:AccountingSupplierParty />
<ns3:AccountingCustomerParty />
ns3:TaxTotal
10
ns3:TaxSubtotal
10
10
ns3:TaxCategory
Z
10
ns3:TaxScheme
VAT
</ns3:TaxScheme>
</ns3:TaxCategory>
</ns3:TaxSubtotal>
</ns3:TaxTotal>
ns3:LegalMonetaryTotal
10
</ns3:LegalMonetaryTotal>
ns3:InvoiceLine
1
10
<ns3:Item />
</ns3:InvoiceLine>
</ns4:Invoice>

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'

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..

commented

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"));

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?

commented

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 ());`

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>
commented

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

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?

commented

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

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