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 9011397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:43:06+00:00 2026-06-16T02:43:06+00:00

Would like to transform a price into a an expressed price range , so

  • 0

Would like to transform a price into a an expressed price range , so that I can say, for example, “Under $20” instead of $ 17.95. Using xml:choose is working for me, but when I try to put the result into a class attribute, I get an error:

Error during XSLT transformation: An unknown error has occurred ()

I’ve been reading up on xsl:variable but cannot seem to find the proper way to set the variable in this case.

The XML

    <?xml version="1.0" encoding="UTF-8"?>
    <productSearchResponse>
        <status>SUCCESS</status>
        <products found="20">
            <product>
                <itemNumber>50575</itemNumber>
                <itemName>Example 1</itemName>
                <price>$ 17.95</price>
            </product>

            ...

            <product>
                <itemNumber>81588</itemNumber>
                <itemName>Example 2</itemName>
                <price>$ 25.95</price>
            </product>
        </products>
    </productSearchResponse>

The stylesheet

<?xml version="1.0" encoding="UTF-8"?>          
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
    <html>
        <head>
        </head>
        <body>
            <xsl:for-each select="/essentials/webservice">
                <xsl:for-each select="document(@filename)/productSearchResponse/products/product">                  
                    <xsl:variable name="producingCountry" select="producingCountry"/>
                    <xsl:variable name="price" select="price"/>
                    <xsl:variable name="priceNoSymbol" select="translate($price,'&#36;', '')"/> 

                    <!-- Assign pricing bands -->                       
                        <xsl:choose>                            
                            <xsl:when test="$priceNoSymbol &lt; '20'">  
                                <xsl:variable name="priceBand" select="'under20'" />
                            </xsl:when>                             
                            <xsl:when test="$priceNoSymbol &gt; '39.99'">   
                                <xsl:variable name="priceBand" select="'20to40'" />
                            </xsl:when>                             
                            <xsl:otherwise>
                                <xsl:variable name="priceBand" select="'over40'" /> 
                            </xsl:otherwise>                                
                        </xsl:choose>

                    <!-- Make the product div -->
                        <div class="product {$producingCountry} {$price} {$priceNoSymbol}     ">    
                        <!--           How do I insert priceBand into the class attribute? ^ -->    
                            <xsl:apply-templates select="itemName"/><br/>                           
                            <xsl:apply-templates select="producingCountry"/><br/>                       
                            <xsl:apply-templates select="price"/><br/><br/>                                                 
                        </div>

                </xsl:for-each>
            </xsl:for-each>
        </body>         
    </html>
</xsl:template>
</xsl:stylesheet>

The resulting html

    <html>
    <head>
        <body>

            <div class="product France $ 17.95 17.95 ">
                Example 1
                <br>
                France
                <br>
                $ 17.95
                <br>
                <br>
            </div>

            ...

            <div class="product Portugal $ 25.95 25.95 ">
                Example 2
                <br>
                Portugal
                <br>
                $ 25.95
                <br>
                <br>
            </div>

        </body>
    </html>

Desired html

    <html>
    <head>
        <body>

            <div class="product France $ 17.95 17.95 under20 ">
                Example 1
                <br>
                France
                <br>
                $ 17.95
                <br>
                <br>
            </div>

            ...

            <div class="product Portugal $ 25.95 25.95 20to40">
                Example 2
                <br>
                Portugal
                <br>
                $ 25.95
                <br>
                <br>
            </div>

        </body>
    </html>
  • 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-06-16T02:43:07+00:00Added an answer on June 16, 2026 at 2:43 am

    I haven’t studied the entire question in detail, but you should replace

    <xsl:choose>                            
      <xsl:when test="$priceNoSymbol &lt; '20'">  
        <xsl:variable name="priceBand" select="'under20'" />
      </xsl:when>                             
      <xsl:when test="$priceNoSymbol &gt; '39.99'">   
        <xsl:variable name="priceBand" select="'20to40'" />
      </xsl:when>                             
      <xsl:otherwise>
        <xsl:variable name="priceBand" select="'over40'" /> 
      </xsl:otherwise>                                
    </xsl:choose>
    

    with

    <xsl:variable name="priceband">
      <xsl:choose>                            
        <xsl:when test="$priceNoSymbol &lt; 20">under20</xsl:when>                             
        <xsl:when test="$priceNoSymbol &lt; 40">20to40</xsl:when>                             
        <xsl:otherwise>over40</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    

    and

    <xsl:variable name="priceNoSymbol" select="translate($price,'&#36;', '')"/> 
    

    with

    <xsl:variable name="priceNoSymbol" select="number(translate($price,'&#36;', ''))"/> 
    

    When you define a variable inside an xsl:when element like that, it goes out of scope almost immediately and isn’t available outside the xsl:choose.

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

Sidebar

Related Questions

I would like to transform an XML document using xslt with a set of
I have some XML which I would like to transform into a report showing
MSH.1:MSH.2:MSH.3:PID.1:PID.2:ORC.1:ORC.3 above string pattern I would like to transform into below XML format <filters>
I have a generated JSON file that I would like to transform. Is there
I have an URL like that : /eng/myfolder/mycategory.aspx I would like to transform it
I have the following Python snippet that I would like to reproduce using C++:
I would like to transform the following lines of data into a structured table
I would like to transform 42 (Base 10) into 000002A (Base 16) in Erlang...
I have two classes that I would like to transform to protobuf messages: [ProtoContract]
How to transform XML for one XSD into another XML format that is very

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.