Home > Software engineering >  How to sort only specific children in the parent node
How to sort only specific children in the parent node

Time:08-18

What would I have to do to sort only specific elements in the parent node? My input looks like this

<?xml version="1.0" encoding="UTF-8"?>
<Companies>
  <badCompany>badCompany</badCompany>
  <badCompany>badCompany2</badCompany>
   <company>Toyota</company>
   <company>Mercedes</company>
   <company>BMW</company>
   <company>Mazda</company>
   <badCompany>badCompany3</badCompany>
</Companies>

I would like to sort all Company nodes in Companies node leaving other children in their place so the output would look like this

<?xml version="1.0" encoding="UTF-8"?>
    <Companies>
      <badCompany>badCompany</badCompany>
      <badCompany>badCompany2</badCompany>
      <company>BMW</company>   
      <company>Mazda</company>
      <company>Mercedes</company>
      <company>Toyota</company> 
      <badCompany>badCompany3</badCompany>
</Companies>

This is my code but it sorts all children

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://sap.com/xi/SAPGlobal20/Global" xmlns:n1="http://sap.com/xi/EWM/Global" version="2.0">
    <xsl:output encoding="UTF-8" method="xml" version="1.0"/>
  <xsl:template match="Companies">
    <xsl:copy>
      <xsl:apply-templates>
        <xsl:sort/>
      </xsl:apply-templates>
        </xsl:copy>

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

</xsl:stylesheet>

CodePudding user response:

Here is one way to look at it:

XSLT 2.0

<xsl:stylesheet version="2.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="/Companies">
    <xsl:copy>
        <xsl:for-each-group select="*" group-adjacent="name()"> 
            <xsl:choose>
                <xsl:when test="self::company">
                    <xsl:apply-templates select="current-group()">
                        <xsl:sort/>
                    </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>
  • Related