abc d

XSLT conditional sort

I have the following XML:

<Users>
  <User Id="1">
    <Name>abc</Name>
    <LastName>d</LastName>
  </User>
  <User Id="2">
    <Name></Name>
    <LastName>ab</LastName>
  </User>
  <User Id="3">
    <Name>a</Name>
    <LastName>efg</LastName>
  </User>
</Users>

Now I am sorting users using the following template:

<xsl:template match="Users">
  <Users>
    <xsl:for-each select="User">
      <xsl:sort select="Name"/>
      <xsl:sort select="LastName"/>

      <User>
        <xsl:attribute name="Id">
          <xsl:value-of select="attribute::Id"/>
        </xsl:attribute>
        <Name>
          <xsl:value-of select="Name"/>
        </Name>
        <LastName>
          <xsl:value-of select="LastName"/>
        </LastName>
      </User>
    </xsl:for-each>
  </Users>
</xsl:template>

But I need a sort that satisfies the following condition: Sort by name. If Name is empty or null, I need to sort by LastName. Therefore, in the prepared XML, I need the following ordering: User3, User2, User1.

Any help is appreciated.

PS: I am using ASP.NET 3.5

+5
source share
2 answers

First, I would use an identity transformation, and then apply sorting to a union of elements (excluding those whose are Nameempty)

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

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

    <xsl:template match="Users">
        <xsl:copy>
            <xsl:apply-templates select="User">
                <xsl:sort select="Name[.!='']|LastName"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

When applied at the input specified in the question, we get:

<Users>
   <User Id="3">
      <Name>a</Name>
      <LastName>efg</LastName>
   </User>
   <User Id="2">
      <Name/>
      <LastName>ab</LastName>
   </User>
   <User Id="1">
      <Name>abc</Name>
      <LastName>d</LastName>
   </User>
</Users>
+3
source

Name LastName , normalize-space(), , |, ( XSLT/XPath 2.0). , .

, , <Users>, <xsl:copy-of>.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" />

<xsl:template match="Users">
  <Users>
    <xsl:for-each select="User">
      <xsl:sort select="(Name[normalize-space()]|LastName[normalize-space()])[1]"/>
        <xsl:copy-of select="."/>
    </xsl:for-each>
  </Users>
</xsl:template>

</xsl:stylesheet>
+1

All Articles