Note that there are some explanatory texts on larger screens.

plurals
  1. POXSLT to sort on value of multiple attributes
    primarykey
    data
    text
    <p>I have a huge XML-formatted configuration file. The system doesn't care about the order of tags, but we humans do! (Primarily for the purpose of version comparisons.) <a href="https://superuser.com/q/355175/9350">I already received</a> the XSLT below which works well, but I've discovered that it's not quite enough. </p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:template match="*"&gt; &lt;xsl:copy&gt; &lt;xsl:copy-of select="@*"/&gt; &lt;xsl:apply-templates&gt; &lt;xsl:sort select="(@name, name())[1]"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>I want to</strong> sort all tags recursively by the value of their <code>name</code> attribute (this works!) but because the attribute is not always present, it must also sort by further attributes, any of which may or may not be present in any given element. </p> <p>I have basically zero understanding of XSLT so I'm experimenting. I've hacked the above into this, but it doesn't work as desired. The result of this seems to be identical to the above. </p> <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:template match="*"&gt; &lt;xsl:copy&gt; &lt;xsl:copy-of select="@*"/&gt; &lt;xsl:apply-templates&gt; &lt;xsl:sort select="@name"/&gt; &lt;xsl:sort select="@row" data-type="number"/&gt; &lt;xsl:sort select="@col" data-type="number"/&gt; &lt;xsl:sort select="@sequence" data-type="number"/&gt; &lt;xsl:sort select="@tabindex" data-type="number"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>My data looks similar to this, and the problem is that the <code>cell</code> elements are not sorted at all (within their <code>grid</code> group) because they have no <code>name</code> attribute. This is why I'd like to extend the sorting logic to use <code>name</code> attribute when present, else the sort should be done using additional attributes like <code>tabindex</code>. Within any given group, the same attributes can be assumed to be present.</p> <pre><code>&lt;sections&gt; &lt;section name="SomeList"&gt; &lt;caption&gt; &lt;![CDATA[Candidates]]&gt; &lt;/caption&gt; ... &lt;parameters&gt; &lt;parameter name="pageSize"&gt; &lt;![CDATA[50]]&gt; &lt;/parameter&gt; &lt;/parameters&gt; ... &lt;grid&gt; &lt;cell row="0" col="7" tabindex="9" colspan="10"&gt; &lt;field name="Entered" /&gt; &lt;/cell&gt; &lt;/grid&gt; &lt;/section&gt; &lt;/sections&gt; </code></pre> <p><strong>Update:</strong><br> With Vincent's very good help, I've created a sorting that works well enough for our purposes. <a href="http://dl.dropbox.com/u/135090/XML-Sorter_v0.4public.zip" rel="nofollow noreferrer">Here it is.</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload