Sorting a DOM document alphabetically by Attribute Name with XSLT

1 Likes
over 1 year ago

While working on a driver development with some very long input DOM documents, it became annoying to have to scroll through the level 3 traces to check the DOM document for various attributes while troubleshooting.

As the initial order of the attributes (prior to processing) did not matter, I created a short XSLT stylesheet and linked it in the driver Input Transform Policy set (ITP) as the first item. This stylesheet sorts the DOM document so that all the attributes are listed in alphabetic order. Reading the driver traces became a lot easier after this was implemented. Most of the stylesheet is the standard XSLT template created by Designer. I modified the initial comments and then expanded the default 'xsl:apply-templates' to add the 'xsl:sort' option to sort by 'attr-name'.

Though this could be linked in any policy set, I advise caution when considering that. I have had to troubleshoot a number of issues where poor coding resulted in the same attribute being added, removed or modified multiple times within a single DOM document. Using this stylesheet in those situations, could result in mis-ordering the attribute changes - though I have not tested that. (Better still, clean up the policies with coding that results in the same attribute being changed multiple times in a single DOM document.)

I hope you find this helpful.

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet exclude-result-prefixes="query cmd dncv" version="1.0" xmlns:cmd="http://www.novell.com/nxsl/java/com.novell.nds.dirxml.driver.XdsCommandProcessor" xmlns:dncv="http://www.novell.com/nxsl/java/com.novell.nds.dirxml.driver.DNConverter" xmlns:query="http://www.novell.com/nxsl/java/com.novell.nds.dirxml.driver.XdsQueryProcessor" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- parameters passed in from the DirXML engine -->
<xsl:param name="srcQueryProcessor"/>
<xsl:param name="destQueryProcessor"/>
<xsl:param name="srcCommandProcessor"/>
<xsl:param name="destCommandProcessor"/>
<xsl:param name="dnConverter"/>
<xsl:param name="fromNds"/>
<!-- identity transformation template -->
<!-- Sort the input Document so that -->
<!-- the attributes are in alphabetic -->
<!-- order by "attr-name" -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:sort select="@attr-name"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Labels:

Support Tip
How To-Best Practice
Comment List
Anonymous
Related Discussions
Recommended