Sunday, March 8, 2020

Removing ns0: prefix from xml output from BizTalk/Logic app XSLT map

Introduction

Xml, which stands for stands for EXtensible Markup Language was designed to describe data and to do that you use tags. It's basic purpose is to focus on what data is  - self decsribing. 

And as we have a provision to add our own tags, it becomes Extensible.

Having this properties makes it very suitable for defining a message (Records, elements and attributes) and exchange it across, also data can be stored in it and can be refered/updated as an when needed (most of the configurations are xml based).

In case of where XML is involved in messaging, it becomes vital to have Namespace as it helps in avoiding element name clashes/conflicts.

A Namespace is a set of unique names. Namespace is a mechanisms by which element and attribute name can be assigned to a group. The Namespace is identified by URI(Uniform Resource Identifiers).

An XML instance may contain element or attribute names from more than one XML vocabulary. If each vocabulary is given a namespace, the ambiguity between identically named elements or attributes can be resolved.

For example, you are dealing with OrderXML and you are receiving multiple OrderXML from different vendors - so how do you differentiate which xml(same structure) is from whom - It is with namespace.


To define xml you use xsd and when you create xsd in BizTalk/Logic app Project(using visual studio) few namespaces automatically gets added, also gets added is Target namespace(identifier - that created xsd belongs to this project ) to say that this xsd belong to it.


default namespaces in xsd

The elements (schema, element, complexType, sequence, …) of the schema itself belong to the namespace http://www.w3.org/2001/XMLSchema.

The namespaces which gets added automatically are actually to tell our schema editor that we are referring to definitions in them and it is done with help of namespace prefix - xs and b in above image.  Note that for Target Namespace there is no prefix .

The target namespace is the place were your declared elements and attributes in your schema belong to (you can refer them as Local).

Consider Target Namespace is to schema is what a namespace is to .Net Object. 

Read more about Schema -- BizTalk Server : Deep dive in schema design

How ns0 gets added in output of map using above schema as destination


Now when we do map from one schema to another, we create new xml and to which namespace it
belongs to it gets added and thus you see namespace and it's prefix

 ns0 added in map result

If you see above ns0 gets added in test map result, although there was no prefix associated with TragetNamespace in xsd -- It is the mapper which does it and ns0 is the default prefix.

So if I use xsd having prefix already associated with TargetNamespace  -- Still mapper will add the default prefix ns0.


How to remove default prefix ns0 and have prefix as per our preference


To do this we need to bypass the mapper and for that we make changes in xslt generated
by the mapper.

Right click the map, select debug and you should see the path where xslt is generated,Copy the path.

Now edit the xslt and add preferred prefix and save it
change prefix ns0
You can use saved xslt in Logic app or in BizTalk by setting the custom xslt path and get desired output as below-

As can be seen , edi is now added as prefix appears with Root node.

What if you are required to have this prefix associated with all the local elements and local attributes?

To do that we use following properties Element FormDefault and Attribute FormDefault
This properties are only relevant if a target namespace(it is optional) is used and only for local declared attributes and elements.

Qualified means: “belongs to a target namespace”.
Unqualified means: “not belonging to a target namespace”.

When Default or Unqualified is used -- No Prefix is associated and for
Qualified -- Prefix is associated








Related Post 

No comments:

Post a Comment

If you have any suggestions or questions or want to share something then please drop a comment