Skip to content Skip to sidebar Skip to footer

Setting The Order Of Xml Attributes Via Xslt?

I have HTML in XML format which I am parsing using XSLT. My HTML looks like this: Test

Solution 1:

XSLT produces as output a result tree conforming to the XDM data model, and in the XDM model, attributes are unordered. Since they have no order, it follows that XSLT stylesheet instructions cannot control the order.

The only opportunity for controlling the order arises during serialization, when the unordered attribute nodes in the result tree are converted to an ordered sequence of name="value" pairs in the lexical XML output. The standard serialization properties available in XSLT (any version) do not provide any way of controlling this. Saxon however has an extension attribute saxon:attribute-order - see

http://www.saxonica.com/documentation/index.html#!extensions/output-extras/serialization-parameters

Solution 2:

Input HTML (with wellformation):

<html><head><metacharset="utf-8" /><title>Test</title></head><body><imgheight="13"width="12"src="google.gif?"id="id1"/></body></html>

XSLT:

<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:outputmethod="xml"omit-xml-declaration="yes"indent="yes"/><xsl:templatematch="@*|node()"><xsl:copy><xsl:apply-templatesselect="@*|node()"/></xsl:copy></xsl:template><xsl:templatematch="img"><xsl:copy><xsl:for-eachselect="@*[not(name()='src')]"><xsl:sortselect="name()"/><xsl:attributename="{name()}"><xsl:value-ofselect="."/></xsl:attribute></xsl:for-each><xsl:apply-templatesselect="@*[name()='src']"/><xsl:apply-templates/></xsl:copy></xsl:template></xsl:stylesheet>

Result:

<html><head><metacharset="utf-8"/><title>Test</title></head><body><imgheight="13"id="id1"width="12"src="google.gif?"/></body></html>

Solution 3:

In addition to <img height="" width="' src="google.gif?<>" /> not being well-formed as commented by Martin Honnen...

Attribute order is insignificant per the XML Recommendation:

Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.

Therefore, XSLT provides no way to constrain attribute ordering.

If you refuse to embrace this recommendation to ignore ordering for attributes, see Martin Honnen's suggestions regarding how to control attribute ordering output to an earlier question on XSLT attribute ordering.

Post a Comment for "Setting The Order Of Xml Attributes Via Xslt?"