oleksii.sapov-erlinger
oleksii.sapov-erlinger

Reputation: 412

Sort XML-elements by their attributes

My question is very similar to those:

  1. List item
  2. List item
  3. List item
  4. List item
  5. List item

However, they don't give the answer to my particular case. Although eventually, I solved the problem but I don't feel this solution to be good and would appreciate if there are better ways to do this. I faced the sort-problem for the first time and would like to understand it better.

From this (modelled) input:

<root>
    <measure attribute="attr">
        <other n="234">-</other>
        <other n="345">-</other>
        <element n="2"/>
        <element n="1"/>
        <element n="3"/>
        <other attr="abc">-</other>
    </measure>
    <measure>
        <other n="234">-</other>
        <other n="345"><node/></other>
        <element n="3"/>
        <element n="1"/>
        <element n="2"/>
        <other attr="abc">-</other>
    </measure>
</root>

I want to get this result:

<root>
   <measure>
      <other n="234">-</other>
      <other n="345">-</other>
      <element n="1"/>
      <element n="2"/>
      <element n="3"/>
      <other attr="abc">-</other>
   </measure>
   <measure>
      <other n="234">-</other>
      <other n="345">
         <node/>
      </other>
      <node/>
      <element n="1"/>
      <element n="2"/>
      <element n="3"/>
      <other attr="abc">-</other>
   </measure>
</root>

So I want to get particular elements (<element/>) to be sorted in the relation to each other, but other elements should stay on their positions.

First I tried this: https://xsltfiddle.liberty-development.net/6r5Gh3h/3

<xsl:stylesheet  version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output encoding="UTF-8" indent="yes" method="xml"/>
    <xsl:strip-space elements="*"/>
    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="measure">  
        <xsl:copy>
            <xsl:apply-templates select="@*, node()[local-name()!='element']"/>
            <xsl:apply-templates  select="element">
                <xsl:sort order="ascending" select="@n"/>
            </xsl:apply-templates>
        </xsl:copy>     
    </xsl:template>
</xsl:stylesheet>

But it changed the order of the elements.

This solution makes the desired output but are there better ways to do this?

https://xsltfiddle.liberty-development.net/94rmq6j

<xsl:stylesheet exclude-result-prefixes="xs math map array" version="3.0" xmlns:array="http://www.w3.org/2005/xpath-functions/array" xmlns:map="http://www.w3.org/2005/xpath-functions/map" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output encoding="UTF-8" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="measure">
    <xsl:copy>
        <xsl:variable name="sortedEls">
            <xsl:perform-sort select="child::element">
                <xsl:sort data-type="number" order="ascending" select="@n"/>
            </xsl:perform-sort>
        </xsl:variable>

        <xsl:for-each select="descendant::*">
            <xsl:choose>
                <xsl:when test="local-name() = 'element' and not(following-sibling::element)">
                    <xsl:sequence select="$sortedEls"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:if test="local-name() != 'element'">
                        <xsl:apply-templates select="."/>
                    </xsl:if>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>

    </xsl:copy>
</xsl:template>

Upvotes: 1

Views: 7938

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

If you only want to sort adjacent element elements then I think processing elements with xsl:for-each-group select="*" group-adjacent="boolean(self::element) allows you to identify them and then inside of the for-each-group you can process the groups of element elements sorted based on the attribute and the other elements without sorting:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    exclude-result-prefixes="xs math map array"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output indent="yes"/>

  <xsl:template match="measure">
      <xsl:copy>
          <xsl:for-each-group select="*" group-adjacent="boolean(self::element)">
              <xsl:choose>
                  <xsl:when test="current-grouping-key()">
                      <xsl:apply-templates select="current-group()">
                          <xsl:sort select="xs:decimal(@n)"/>
                      </xsl:apply-templates>
                  </xsl:when>
                  <xsl:otherwise>
                      <xsl:apply-templates select="current-group()"/>
                  </xsl:otherwise>                  
              </xsl:choose>
          </xsl:for-each-group>
       </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bFN1y9m/

If you want to sort all element child elements and swap them around based on the original position of all element children then I think the following, which first computes the sorted sequence of attribute values and then gives each element to be sorted the position of the original input order, helps:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output indent="yes"/>

  <xsl:template match="measure">
      <xsl:copy>
          <xsl:variable name="original-order" as="xs:string*" select="node()!generate-id()"/>
          <xsl:variable name="elems" as="element(element)*" select="element"/>
          <xsl:variable name="sort-order" as="xs:decimal*" select="sort(element/xs:decimal(@n))"/>
          <xsl:apply-templates>
              <xsl:sort
                select="if (. instance of element(element)) 
                        then 
                          let $sort-pos := index-of($sort-order, xs:decimal(@n)),
                              $orig-el := $elems[$sort-pos]
                          return
                              index-of($original-order, $orig-el!generate-id())
                        else position()"/>
          </xsl:apply-templates>
       </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bFN1y9m/1

For XSLT 3 processors with higher-order function sort support (e.g. Saxon PE or EE or Altova) I think this can be improved to use a sequence of elements or element ids for the original sort order:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="measure">
      <xsl:copy>
          <xsl:variable name="original-order" as="xs:string*" select="node()!generate-id()"/>
          <xsl:variable name="elems" as="element(element)*" select="element"/>
          <xsl:variable name="sort-order" as="xs:decimal*" select="sort(element/xs:decimal(@n))"/>
          <xsl:apply-templates>
              <xsl:sort
                select="if (. instance of element(element)) 
                        then 
                          let $sort-pos := index-of($sort-order, xs:decimal(@n)),
                              $orig-el := $elems[$sort-pos]
                          return
                              index-of($original-order, $orig-el!generate-id())
                        else position()"/>
          </xsl:apply-templates>
       </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

That way I think the approach should work even if there are various elements with the same sort key value (e.g. @n value).

Upvotes: 0

Related Questions