UBL 2.0 NameSpace question
GunterO opened this issue · comments
Hello,
I have a question about adding name spaces.
My sample XML (which I need to make) has these name spaces:
_xmlns:qdt:urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2
_xmlns:ccts:urn:oasis:names:specification:ubl:schema:xsd:CoreComponentParameters-2
_xmlns:stat:urn:oasis:names:specification:ubl:schema:xsd:DocumentStatusCode-1.0
_xmlns:cbc:urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2
_xmlns:cac:urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2
_xmlns:udt:urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2
_xmlns:urn:oasis:names:specification:ubl:schema:xsd:Invoice-2
My generated invoice XML has these name spaces:
_xmlns:ns4: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
__prefix:ns4
This is the code I tried to add for example qdt, but it's not added:
MapBasedNamespaceContext aNsCtx = UBL20NamespaceContext.getInstance().getClone();
aNsCtx.addMapping("qdt", "urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2");
aWriter.setNamespaceContext(aNsCtx);
final ESuccess eSuccess = aWriter.write(aInvoice, new File(fn));
Any idea what I'm doing wrong?
Thanks!
Also, then I don't clone the instance, the ns4 prefix is not there.
_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
Used code:
final ESuccess eSuccess = UBL20Writer.invoice().write(aInvoice, new File(fn));
Why is that?
Thanks!
I could get rid of the prefix by adding this:
aNsCtx.addDefaultNamespaceURI("urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
But the extra namespace/mapping are not added dough.
Hi. So
MapBasedNamespaceContext aNsCtx = UBL20NamespaceContext.getInstance().getClone();
aNsCtx.addMapping("qdt", "urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2");
aWriter.setNamespaceContext(aNsCtx);
final ESuccess eSuccess = aWriter.write(aInvoice, new File(fn));
would be the correct solution.
What is __prefix:ns4
exactly. Can you paste the XML here?
Is it
prefix="ns4"
or so?
Thanks for your quick reply!
Correct, that's the code I use. However, when I add this line, the "ns4" prefix is not there:
aNsCtx.addDefaultNamespaceURI("urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
This "__prefix:ns4" is something the XML viewer is showing because of the prefix in the XML file.
I can't drop the XML extension here, so I renamed the file to sample.txt.
Ah okay got it - you have a weird XML viewer ;-)
Try this - combining both:
MapBasedNamespaceContext aNsCtx = UBL20NamespaceContext.getInstance().getClone();
aNsCtx.addMapping("qdt", "urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2");
aNsCtx.addDefaultNamespaceURI("urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
aWriter.setNamespaceContext(aNsCtx);
final ESuccess eSuccess = aWriter.write(aInvoice, new File(fn));
Yes, that's what I tried in the first place, but it is resulting in an XML without the "ns4" prefix, but without the "qdt" namespace line:
sample2.txt
It says "thew NullPointerException" - is "fn" or "aInvoice" null
?
Can you send me a bit more code privately - see the pom.xml for my email address. Thx
I just did, thanks!
Reason/solution:
In general, the underlying JAXB only writes the XML namespaces it (whysoever) needs.
And as the qdt namespace ( "urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2") is not used, it is not emitted.
If you would use an element that would require this namespace, than it would be there.