I have 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 sort users using 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 sorting, which satisfies following condition: Sort by Name. If Name is empty or null, I need to sort by LastName. So in produced XML I need following ordering: User3, User2, User1.
Any help is appreciated.
P.S.: I use ASP.NET 3.5
I would use first the identity transformation, and then apply the sorting to the union of the elements (excluding those whose
Nameis empty)When applied on the input shown in the question, we obtain: