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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:07:54+00:00 2026-05-14T01:07:54+00:00

In this the catalog.xml file. I have two books who have the same inventory

  • 0

In this the catalog.xml file. I have two books who have the same inventory (i.e. 20). I want to write an XSL file that will display the highest number of copies of a book in a catalog. If there are two or more books of the same inventory then they have to be displayed.

<catalog>
  <Book>
    <sku>12345</sku>
    <title>Beauty Secrets</title>
    <condition>New</condition>
    <current_inventory>20</current_inventory>
    <price>99.99</price>
  </Book>
  <Book>
    <sku>54321</sku>
    <title>Picturescapes</title>
    <current_inventory>20</current_inventory>
    <condition>New</condition>
    <price>50.00</price>
  </Book> 
  <Book>
    <sku>33333</sku>
    <title>Tourist Perspectives</title>
    <condition>New</condition>
    <current_inventory>0</current_inventory>
    <price>75.00</price>
  </Book>
  <Book>
    <sku>10001</sku>
    <title>Fire in the Sky</title>
    <condition>Used</condition>
    <current_inventory>0</current_inventory>
    <price>10.00</price>
  </Book>
</catalog>

Below is my catalog3.xsl file which is able to display only one out of the two books:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:variable name="max"/>

  <xsl:template match="/">
    <html>
      <body>
        <h2>Titles of Books for which Most Copies are Available</h2>
        <table border="2">
          <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>No of Copies</th>
          </tr>
          <xsl:apply-templates/>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="catalog">
    <xsl:for-each select="Book">
      <xsl:sort select="current_inventory" data-type="number" order="descending"/>
      <tr>
        <xsl:if test="position()= 1">
          <p><xsl:value-of select="$max = "/></p>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="current_inventory"/></td>
        </xsl:if>
      </tr>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Could anybody correct me to achieve my goal of displaying all the copies having the same maximum inventory in the catalog. Thanks.

  • 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-14T01:07:55+00:00Added an answer on May 14, 2026 at 1:07 am

    The maximum current_inventory can be calculated in the following manner:

    <xsl:variable name="max">
      <xsl:for-each select="/catalog/Book/current_inventory">
        <xsl:sort data-type="number" order="descending"/>
        <xsl:if test="position()=1"><xsl:value-of select="."/></xsl:if>
      </xsl:for-each>
    </xsl:variable>
    

    Adjusting the criteria for the xsl:if to compare the current_inventory of the current node in the for-each to the $max variable achieves the desired result.

    You were evaluating position()=1 inside of the for-each, which will only be true for the first item in the sorted collection.

    I set it to look for current_inventory that is equal to the $max:

    <xsl:if test="current_inventory = $max">
    

    Applying these changes to your stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <!--Determine the maximum current_inventory -->
    <xsl:variable name="max">
        <xsl:for-each select="/catalog/Book/current_inventory">
            <xsl:sort data-type="number" order="descending"/>
            <xsl:if test="position()=1"><xsl:value-of select="."/></xsl:if>
        </xsl:for-each>
    </xsl:variable>
    
    <xsl:template match="/">
        <html>
            <body>
                <h2>Titles of Books for which Most Copies are Available</h2>
                <table border="2">
                    <tr bgcolor="#9acd32">
                        <th>Title</th>
                        <th>No of Copies</th>
                    </tr>
                    <xsl:apply-templates/>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="catalog">
        <xsl:for-each select="Book">
            <xsl:sort select="current_inventory" data-type="number" order="descending"/>
    
            <xsl:if test="current_inventory = $max">
                                <tr>
                    <td>
                        <xsl:value-of select="title"/>
                    </td>
                    <td>
                        <xsl:value-of select="current_inventory"/>
                    </td>
                                </tr>
            </xsl:if>
    
        </xsl:for-each>
    </xsl:template>
    

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

Sidebar

Related Questions

I have an XML catalog data and an XSL file to visualize this catalog
I have an XML file that contains price value.I want to sort my table
this is my code that I write it but I want to use LINQ
I have many xml file's, I do not want to get the value, but
I have a xml like this and I want the attribute which is defined
Is there a way to have Artifactory automatically generate the archetype-catalog.xml file for a
I have an XML file with this format: <?xml version=1.0 encoding=utf-8 ?> <OpacResult> <valueObjects
I have an XML file with this format: <?xml version=1.0 encoding=utf-8 ?> <OpacResult> <valueObjects
I have an XML file. <?xml version=1.0?> <catalog> <book id=bk101> </book> <catalog> I read
Taking the XSLT and XML from this page as an example: http://www.w3schools.com/xsl/xsl_transformation.asp I have

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.