Search code examples
xmlabapabap-st

Could we remove the sorting of attributes while using ST?


I have to generate a XML based on structures, I use Simple Transformation to generate it. It seems that while performing the serialization there is an automatic sorting of the attributes of the XML.

Example, sample of ST :

<?sap.transform simple?> 
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" xmlns:def="http://www.sap.com/abapxml/types/defined" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">   <tt:root name="TOP" type="ddic:ZZ_STRUCTURE_XML"/>   
<tt:template>   
<Element1 tt:ref="TOP">       
<tt:attribute name="Name" value-ref="FIELD01"/>       
<tt:attribute name="Version" value-ref="FIELD02"/>       
<tt:attribute name="Tag" value-ref="FIELD03"/>     
</Element2>   
</tt:template> 
</tt:transform>

Expected result (XML) :

<?xml version="1.0" encoding="utf-8"?>
<Element1 Name="xx" Version="xx" Tag="xx"/>

Actual result (XML) :

<?xml version="1.0" encoding="utf-8"?>
<Element1 Name="xx" Tag="xx" Version="xx"/>

Is there a way to remove the alphabetical sorting of the attributes using ST ? I know that this unexpected sort doesn't have impact for the parsing of the XML but still it is not the expected result.


Solution

  • Unfortunately, you cannot control the order of attributes, as this depends on the implementation details of the underlying library. Therefore, there is no guarantee of a consistent order, which may vary based on the release version and / or the XML technology used.

    As you correctly pointed out, the order of attributes is irrelevant according to the XML specification, and an XML parser should not expect a particular order. For reference, the XML specification states:

    3.1 Start-Tags, End-Tags, and Empty-Element Tags
    ...
    the order of attribute specifications in a start-tag or empty-element tag is not significant