I have an XML looking like this:
<?xml version="1.0" encoding="utf-8"?>
<n0:OrderConfirmation xmlns:n0="http://company.org/interface/MVSI"
xmlns:ord="urn:company.com:Order:XYZ:001">
<MessageHeader>
<ID>001</ID>
<CreationDateTime>2020年09月10日T07:39:26.697Z</CreationDateTime>
</MessageHeader>
<Order>
<OrderID>00027847</SalesOrderID>
<Status>Confirmed</Status>
<Items>
<Article>
<ArticleID>ABC001</ArticleID>
<LineItem>000001</LineItem>
</Article>
</Items>
</Order>
</n0:OrderConfirmation>
I would say a better way would be to declare a default namespace like this:
<?xml version="1.0" encoding="utf-8"?>
<n0:OrderConfirmation xmlns:n0="http://company.org/interface/MVSI"
xmlns="urn:company.com:Order:XYZ:001">
<MessageHeader>
<ID>001</ID>
<CreationDateTime>2020年09月10日T07:39:26.697Z</CreationDateTime>
</MessageHeader>
<Order>
<OrderID>00027847</SalesOrderID>
<Status>Confirmed</Status>
<Items>
<Article>
<ArticleID>ABC001</ArticleID>
<LineItem>000001</LineItem>
</Article>
</Items>
</Order>
</n0:OrderConfirmation>
Or more simplified: the root element could be part of the default namespace:
<?xml version="1.0" encoding="utf-8"?>
<OrderConfirmation xmlns="urn:company.com:Order:XYZ:001">
<MessageHeader>
<ID>001</ID>
<CreationDateTime>2020年09月10日T07:39:26.697Z</CreationDateTime>
</MessageHeader>
<Order>
<OrderID>00027847</SalesOrderID>
<Status>Confirmed</Status>
<Items>
<Article>
<ArticleID>ABC001</ArticleID>
<LineItem>000001</LineItem>
</Article>
</Items>
</Order>
<OrderConfirmation>
because the n0
namespace value is global to the system and for all XML documents generated by the system.
I am wondering if this is a good practice in XML to let the middle elements in the "no-namespace" and alternatively to declare a namespace that is never used.
UPDATE: To give a better context of the situation, we are integrating different systems using an ESB in the whole enterprise. Systems can be in several departments across the company. In the actual situation we are transforming the incoming XML to a more normalized XML with a unique namespace related to the kind of data we're managing (an order in that case), like a canonical data model all other systems have to know about.
1 Answer 1
Some people think everything should be in a namespace, because it documents who is responsible for the design of the vocabulary. If you follow that line of thought, then this applies to the payload of your example messages as much as it applies to everything else.
Some people think it's a lot simpler to not use namespaces unless you need them.
Depends whether you like a big-project discipline and control approach or a small-project just-get-it-done-quickly approach.
-
Yes I understand the way of no-namespace concept for simplification, but the example in my question is a mix between both ways: root element is in a namespace while sub-elements are in the no-namespace. If we decide to use no-namespace, I guess this should be globally decided no?рüффп– рüффп2020年09月17日 07:45:23 +00:00Commented Sep 17, 2020 at 7:45
-
3I'm not a great believer in "rules of good practice" for this kind of thing. Use the technology in whatever way best meets your project requirements (which you haven't told us anything about).Michael Kay– Michael Kay2020年09月17日 07:48:56 +00:00Commented Sep 17, 2020 at 7:48
-
In my case it's not just a small tutorial about XML but we are ingrating different systems across a whole enterprise (updated the question with context). I think using namespaces is totally recommended in that case.рüффп– рüффп2020年09月17日 07:49:34 +00:00Commented Sep 17, 2020 at 7:49
-
Namespaces can be very beneficial, especially when you are dealing with many different systems. They greatly improve the ability to validate the XML to be sure that every message that is passed conforms to the expected or prescribed structure.Mike Robinson– Mike Robinson2021年02月14日 18:22:42 +00:00Commented Feb 14, 2021 at 18:22
-
1Yes: useful in big complex projects, an unnecessary complication in small simple ones.Michael Kay– Michael Kay2021年02月15日 12:58:24 +00:00Commented Feb 15, 2021 at 12:58
Explore related questions
See similar questions with these tags.