Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6160833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:23:31+00:00 2026-05-23T21:23:31+00:00

I have following XML: <Users> <User Id="1"> <Name>abc</Name> <LastName>d</LastName> </User> <User Id="2"> <Name></Name> <LastName>ab</LastName>

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:23:31+00:00Added an answer on May 23, 2026 at 9:23 pm

    I would use first the identity transformation, and then apply the sorting to the union of the elements (excluding those whose Name is empty)

     <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 on the input shown in the question, we obtain:

    <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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following XML: <Users> <User Id=1> <Name>Name1</Name> <LastName>LastName1</LastName> </User> <User Id=5> <Name>Name5</Name> <LastName>LastName5</LastName>
In Powershell, suppose I have the following xml: <Users> <User Name=Foo> <Friends> <Friend Name=Bar/>
I have a XMLTYPE with the following content: <?xml version=1.0?> <users> <user> <name>user1</name> </user>
i have the following xml file <?xml version=1.0 encoding=utf-8?> <Users> <User> <Name>John Smith</Name> <test>
i have following xml <?xml version=1.0 encoding=UTF-8 ?> <Users> <User> <ID>SMSUser(63)</ID> <Email>a@example.com</Email> </User> <User>
I have the following XML document: <database name=pressPlay> <table name=users> <column name=userID type=INT constraints=tableID_c
I have the following in the tomcat-users.xml : <tomcat-users> <role rolename=admin/> <role rolename=manager/> <user
I have an xml document as follows <users> <user test=oldvalue> <userid>sony</userid> <name>vijay</name> </user> </users>
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I have the following filters configured in my pom.xml: <build> ... <filters> <filter>build.properties</filter> <filter>user.properties</filter>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.