Melorian Astrel
Melorian Astrel

Reputation: 13

Sort specific xml tags by attribute value

Say I have the following xml structure:

<a>
    <t id="27"></t>
    <t id="23"></t>
    <b id="a"></b>
    <b id="t"></b>
    <b id="p"></b>
    <c id="er"></c>
</a>

And I want to create a new xml file with only the b tags sorted by attribute id, and leave the rest of the file unchanged:

<a>
    <t id="27"></t>
    <t id="23"></t>
    <b id="a"></b>
    <b id="p"></b>
    <b id="t"></b>
    <c id="er"></c>
</a>

How can I do this?

This is what I found in a post:

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:choose>
        <xsl:when test="*[local-name()='MEASV']">
          <xsl:apply-templates select="@* | node()">
            <xsl:sort select="@id" />
          </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="@* | node()" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

But it does not work for this example:

<a>
    <b id="7"></b>
    <b id="2"></b>
    <b id="5"></b>
    <b id="9"></b>
    <MEASV id="7"></MEASV>
    <MEASV id="10"></MEASV>
    <MEASV id="6"></MEASV>
    <MEASV id="3"></MEASV>
    <MEASV id="5"></MEASV>
</a>

Giving:

<?xml version="1.0" encoding="UTF-8"?>
<a>
<MEASV id="10"></MEASV>
<b id="2"></b>
<MEASV id="3"></MEASV>
<b id="5"></b>
<MEASV id="5"></MEASV>
<MEASV id="6"></MEASV>
<b id="7"></b>
<MEASV id="7"></MEASV>
<b id="9"></b>
</a>

Any help, please?

Upvotes: 1

Views: 1301

Answers (2)

Tobias Klevenz
Tobias Klevenz

Reputation: 1645

Assuming XSLT 2.0 is a possibility, since it was used in the povided example.

The following will keep the base structure and only sort adjacent b's:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 exclude-result-prefixes="xs"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:template match="a">
    <xsl:copy>
        <!-- group any adjacent b's together -->
        <xsl:for-each-group select="*" group-adjacent="local-name()='b'">
            <xsl:choose>
                <!-- if b sort by id -->
                <xsl:when test="current-grouping-key()">
                    <xsl:apply-templates select="current-group()">
                        <xsl:sort select="@id" data-type="number"/>
                    </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:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

This will produce the following for the first example:

<a>
  <t id="27"/>
  <t id="23"/>
  <b id="a"/>
  <b id="t"/>
  <b id="p"/>
  <c id="er"/>
</a>

and this for the second:

<a>
   <b id="2"/>
   <b id="5"/>
   <b id="7"/>
   <b id="9"/>
   <MEASV id="7"/>
   <MEASV id="10"/>
   <MEASV id="6"/>
   <MEASV id="3"/>
   <MEASV id="5"/>
</a>

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 116959

Perhaps you could use something like this:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

 <xsl:template match="a">
    <xsl:copy>
        <xsl:apply-templates select="*">
            <xsl:sort select="name()"/>
            <xsl:sort select="@id[parent::b]"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

resulting in:

<?xml version="1.0" encoding="UTF-8"?>
<a>
   <b id="a"/>
   <b id="p"/>
   <b id="t"/>
   <c id="er"/>
   <t id="27"/>
   <t id="23"/>
</a>

for your first example, and:

<?xml version="1.0" encoding="UTF-8"?>
<a>
   <b id="2"/>
   <b id="5"/>
   <b id="7"/>
   <b id="9"/>
   <MEASV id="7"/>
   <MEASV id="10"/>
   <MEASV id="6"/>
   <MEASV id="3"/>
   <MEASV id="5"/>
</a>

for the second one.

Note that sorting is different for text and for numbers, so one type cannot really fit both of your examples.

Upvotes: 1

Related Questions