XML Namespace, Sort and Other Utilities


Sometimes it is required to manage xml namespaces, sort or order a xml by element names in alphabetical order, sort xml repeating element by value in alphabetical order, add SOAP 1.1 and 1.2 envelopes to a xml, prettify a xml, convert xml to json, remove nil or nillable or null elements of a xml and remove all the annotations from a xsd.

All the actions are listed below. Paste xml in below textarea and perform appropriate action.

Click here to try


Remove namespaces from XML

XML may contain one or more namespaces and each element may belong to one namespace. The namespace can be declared at element level or at root element level. If declared at root element leven the each namespace is assigned with a prefix ex., xmlns:ns1="https://myxml.in/" and all elements belonging to that namespace are prefixed with the prefix value (ns1:name).

Sometimes these namespaces become a problem to handle and need to be removed, both declaration at element level or at root level along with prefixes if used.

XML namespace prefixes

<ns:root xmlns:ns="http://myxml.in/exaples"> <ns:name>Fname Lname</ns:name> <ns:age>25</ns:age> <ns:address>New York, USA</ns:address> </ns:root>

XML with namespace removed

<root> <name>Fname Lname</name> <age>25</age> <address>New York, USA</address> </root>

Click here to try Remove namespaces from XML option

Add SOAP 1.1 Envelope to XML

Each SOAP 1.1 document has Envelope and Body elements. The SOAP 1.1 Envelope and Body elements are declared under http://schemas.xmlsoap.org/soap/envelope/ namespace. The actual XML content is embedded inside the SOAP 1.1 Envelope. The Envelope is same for all SOAP documents but the body content changes.

Add the SOAP 1.1 Envelope for any XML content with click of a button.

Click here to try Adding SOAP 1.1 Envelope to XML option

Add SOAP 1.2 Envelope to XML

Each SOAP 1.2 document has Envelope and Body elements. The SOAP 1.2 Envelope and Body elements are declared under http://www.w3.org/2001/06/soap-envelope namespace. The actual XML content is embedded inside the SOAP 1.2 Envelope. The Envelope is same for all SOAP 1.2 documents but the body content changes.

Add the SOAP 1.2 Envelope for any XML content with click of a button.

Click here to try Adding SOAP 1.2 Envelope option

Optimize namespace prefixes of a XML

Each XML document may contain one or more namespaces. Each element may belong to one namespace and in such a scenarion some elements may belong to no namespace. The optimal way of declaring namespaces is declare them in the root element and assign a unique prefix for each one of the namespace. The prefix has to be prefixed to each element accordingly with : as separator. It makes the XML valid if same namespace is declared with multiple prefixes and use them at different places and some of elements having the same namespace declared locally again.

Each of the below XML is valid and are the different represention of same XML

XML with default namespace

<root xmlns="http://myxml.in/exaples"> <name>Fname Lname</name> <age>25</age> <address>New York, USA</address> </root>

XML with multiple namespace prefixes

<ns:root xmlns:ns="http://myxml.in/examples" xmlns:ns1="http://myxml.in/examples"> <ns:name>Fname Lname</ns:name> <ns1:age>25</ns1:age> <address xmlns="http://myxml.in/examples">New York, USA</address> </ns:root>

XML with optimized namespace prefixes

<ns:root xmlns:ns="http://myxml.in/exaples"> <ns:name>Fname Lname</ns:name> <ns:age>25</ns:age> <ns:address>New York, USA</ns:address> </ns:root>

Click here to try Optimize namespaces option

Format XML with default namespace prefix

A default namespace is a namespace declared in any element without having a prefix. If no declaration is present in any element and also without a prefix then the element belongs to parent's namespace.

XML with multiple namespace prefixes

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header/> <soapenv:Body> <ns:root xmlns:ns="http://myxml.in/exaples"> <ns:name>Fname Lname</ns:name> <ns:age>25</ns:age> <ns:address>New York, USA</ns:address> </ns:root> </soapenv:Body> </soapenv:Envelope>

XML with default namespace prefixes

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <Header/> <Body> <root xmlns="http://myxml.in/exaples"> <name>Fname Lname</name> <age>25</age> <address>New York, USA</address> </root> </Body> </Envelope>

Click here to try Format XML with default namespace option

Remove annotations from XSD

Annotation is an element inside any element or attribute declaration of a XSD to store the documentation or human readable description of the element. Any XSD with detailed descriptions increases the size of the xsd. The size is reduced if the annotations are removed.

Click here to try Remove annotations from XSD option

Sort XML elements and attraibutes alphabetically

Sorting all elements and attributes of a XML alphabetically makes it easy to find the required information.

The actual XML below

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <Header/> <Body> <root xmlns="http://myxml.in/exaples"> <name b="b" c="c" a="a">Fname Lname</name> <age>25</age> <address>New York, USA</address> </root> </Body> </Envelope>

The XML with sorted elements below

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <Body> <root xmlns="http://myxml.in/exaples"> <address>New York, USA</address> <age>25</age> <name a="a" b="b" c="c">Fname Lname</name> </root> </Body> <Header/> </Envelope>

Click here to try Sort XML elements and attributes option

Sort repeating XML element alphabetically using value

It is always better to compare 2 xml files with repeating elements after the repeating elements sorted on a value.

Four values have to be provided while using this option above. properties,property,name,element are the four values to sort below example.

properties - the parent element of the repeating element

property - the repeating element which is to be sorted

name - the child element of the repeating element to be used for sorting purpose

element - indicates if the child is an element or an attribute. Possible values are element or attribute

XML repeating property element

<properties> <property> <name>xyz</name> <value>10</value> </property> <property> <name>mno</name> <value>9</value> </property> <property> <name>abc</name> <value>20</value> </property> </properties>

XML repeating property element sorted on name elements value

<properties> <property> <name>abc</name> <value>20</value> </property> <property> <name>mno</name> <value>9</value> </property> <property> <name>xyz</name> <value>10</value> </property> </properties>

Click here to try Sorting repeating element of a XML option

Remove all xsi:nil true elements from XML

Any non string XML element should have xsi:nil="true" to avoid validation errors against XSD if element is declared as nillable in XSD. Removing these elements altogether to reduce the size of the xml. This may make the XML invalid with respect to XSD but facilitates small string for quicker analysis.

XML with xsi:nil elements

<ns:root xmlns:ns="http://myxml.in/exaples" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ns:name>Fname Lname</ns:name> <ns:age>25</ns:age> <ns:address>New York, USA</ns:address> <ns:nilOne xsi:nil="true" /> <ns:nilTwo xsi:nil="true" /> </ns:root>

XML with xsi:nil elements removed

<ns:root xmlns:ns="http://myxml.in/exaples" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ns:name>Fname Lname</ns:name> <ns:age>25</ns:age> <ns:address>New York, USA</ns:address> </ns:root>

Click here to try Removing xsi:nil elements from XML option

Prettify XML

Pretifying a XML string for more readability. Prettifying means to indent the elements properly as per the hierarchy.

Actual XML String

<ns:root xmlns:ns="http://myxml.in/exaples"> <ns:name>Fname Lname</ns:name> <ns:age>25</ns:age> <ns:address>New York, USA</ns:address> </ns:root>

Prettified XML String

<ns:root xmlns:ns="http://myxml.in/exaples"> <ns:name>Fname Lname</ns:name> <ns:age>25</ns:age> <ns:address>New York, USA</ns:address> </ns:root>

Click here to try Prettify XML option

Convert XML to JSON

Converting any XML to its corresponding JSON Structure.

XML String

<ns:root xmlns:ns="http://myxml.in/exaples"> <ns:name>Fname Lname</ns:name> <ns:age>25</ns:age> <ns:address>New York, USA</ns:address> </ns:root>

Corresponding JSON String

{ "root": { "name": "Fname Lname", "age": 25, "address": "New York, USA" } }

Click here to try Convert XML to JSON option