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

  • Home
  • SEARCH
  • 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 803523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:46:29+00:00 2026-05-14T23:46:29+00:00

Is there any way to optimize this code. <xsl:choose> <xsl:when test=val1 = val2> <xsl:apply-templates

  • 0

Is there any way to optimize this code.

<xsl:choose>
    <xsl:when test="val1 = val2">
        <xsl:apply-templates select="node"/>
    </xsl:when>
    <xsl:otherwise>
        <div>
            <xsl:apply-templates select="node"/>
        </div>
    </xsl:otherwise>
</xsl:choose>

I do not like having to write twice the same <xsl:apply-templates select="node"/>.


Update:

The idea is that depending on the result of comparison to do one of two things:

  1. Just print some information (which we obtain after applying the template <xsl:apply-templates select="node"/>).

  2. Print the same information, but in advance “wrapping” it in the container (div for example).

  • 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-14T23:46:30+00:00Added an answer on May 14, 2026 at 11:46 pm

    Use:

    <xsl:apply-templates select="node">
      <xsl:sort select="not(val1 = val2)"/>
    </xsl:apply-templates>
    

    Here is a complete example. This transformation:

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
        <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*">
      <t>
            <xsl:apply-templates select="node">
              <xsl:sort select="not(val1 = val2)"/>
            </xsl:apply-templates>
      </t>
     </xsl:template>
    
     <xsl:template match="node[not(val1 = val2)]">
      <div>
         <node>
           <xsl:apply-templates/>
         </node>
      </div>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on this XML document:

    <t>
      <node>
        <val1>1</val1>
        <val2>2</val2>
      </node>
      <node>
        <val1>3</val1>
        <val2>3</val2>
      </node>
    </t>
    

    produces the wanted, correct result:

    <t>
       <node>
          <val1>3</val1>
          <val2>3</val2>
       </node>
       <div>
          <node>
             <val1>1</val1>
             <val2>2</val2>
          </node>
       </div>
    </t>
    

    Explanation of the solution:

    Whenever an <xsl:apply-templates> has an <xsl:sort> child, the nodes that are selected are sorted according the data provided in the <xsl:sort> child(ren) and the results of applying templates on each selected node appear in the output in that (sort) order — not in document order.

    In the transformation above we have:

    <xsl:apply-templates select="node"> 
      <xsl:sort select="not(val1 = val2)"/> 
    </xsl:apply-templates> 
    

    This means that the results of applying templates to the elements named node for which it is true that val1=val2 will appear before the results of applying templates to the elements named node for which val1=val2 is not true. This is because false sorts before true.

    If this explanation is not clear, the reader is directed to read more about <xsl:sort>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to apply an attribute to a model file in ASP.NET
Is there any way to check whether a file is locked without using a
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox
Is there any way, in any language, to hook my program when a user
Is there any way to have something that looks just like a file on
Is there any way to include the SVN repository revision number in the version
Is there any way to use inheritance in database (Specifically in SQL Server 2005)?
Is there any way to force a listview control to treat all clicks as
Is there any way to listen to an Oracle AQ using a Perl process
Is there any way to Write files to App_Data under medium trust hack? Im

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.