XSLT: Positioning of newly added attribute


i am copying a xml document and add a attribute to certain nodes (see: xslt how to add attributes to copy-of)

Thing is, my nodes already have some existing attributes, f.e.:

<element id="123" dbfield="table">

When i add a new attribute, it gets positioned in front of the first attribute, just like that:

<element version="default" id="123" dbfield="table">

I would prefer to have it as the last attribute, like this:

<element  id="123" dbfield="table" version="default">

Is there a way to set the position of the new "version"-attribute? Thank you for your help!

Show source
| xml   | xslt   2016-11-21 13:11 2 Answers

Answers to XSLT: Positioning of newly added attribute ( 2 )

  1. 2016-11-21 14:11

    The XSLT language does not provide a way to control the order of attributes, because the XML language specification states explicitly that the order of attributes is not significant.

    Still, most processors will write out the attributes in the order of the instructions given to them - so you just need to change the order from (presumably, as you did not show your current code):

    <xsl:template match="element">
            <xsl:attribute name="version">default</xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>


    <xsl:template match="element">
            <xsl:apply-templates select="@*"/>
            <xsl:attribute name="version">default</xsl:attribute>
  2. 2016-11-21 15:11

    Recent versions of Saxon (PE and EE) have a serialization property saxon:attribute-order. Writing:

    <xsl:output saxon:attribute-order="id dbfield version"/>

    will ensure that the attributes matching these names are serialized in the defined order, followed by attributes that are not present in the list.

    While it's true that you should never treat attribute order as significant in the sense of causing receiving software to behave differently, I do agree with you that one of the design goals for XML is to be human-readable, and using a consistent attribute order helps to achieve this goal.

Leave a reply to - XSLT: Positioning of newly added attribute

◀ Go back