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 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 optimize this piece of code to work faster? I'd
Is there any way to optimize this query? It takes more than 2.5 secs.
Is there any way I can avoid using array_flip to optimize performance. I am
Is there any way to update cookies across folders in PHP? For example, consider
Is there any way to trigger a function that's called when the user types
:) Is there any way to create an index, and incrementing with a given
Is there any way to hover over an element that's already hidden. I am
Is there any way to simulate a try-finally or try-except in a language that
is there any way to debug a J2ME application in a Windows Mobile emulator
Is there any way to listen for system-side open and save panel events and

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.