XSL to display the maximum number of copies in catalog.xml

In this file catalog.xml. I have two books that have the same inventory (i.e. 20). I want to write an XSL file that displays the largest number of book copies in a directory. If there are two or more books of the same inventory, they should 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 file catalog3.xsl, which can display only one of 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>

Can someone correct me in order to achieve my goal - to display all copies with the same maximum inventory in the directory. Thank.

+5
source share
4 answers

The maximum current_inventorycan be calculated as follows:

<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:if current_inventory node for-each $max .

position()=1 for-each, .

current_inventory, $max:

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

:

<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>

+2

, Muenchian:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kBookByNums"
    match="Book" use="number(current_inventory)"/>

 <xsl:template match="/*">
   <xsl:for-each select=
    "Book
      [generate-id()
      =
       generate-id(key('kBookByNums',
                        number(current_inventory)
                       )[1]
                       )
       ]
    ">
     <xsl:sort select="current_inventory"
      data-type="number" order="descending"/>

      <xsl:if test="position()=1">
        <xsl:copy-of select=
          "key('kBookByNums',
                number(current_inventory)
               )"/>
      </xsl:if>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

XML-, :

<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>

, xsl-key " " " (.. )", -. .

, , . , ( ) , 10-20 - , .

.

+2

XSLT 1.0

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
>
  <xsl:template match="catalog">
    <!-- find the maximum <current_inventory> of all books -->
    <xsl:variable name="MaxInventory">
      <xsl:for-each select="Book">
        <xsl:sort select="current_inventory" data-type="number" order="descending" />
        <xsl:if test="position() = 1">
          <xsl:value-of select="current_inventory" />
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>

    <!-- output all matching <Book>s, sorted by title -->
    <table>
      <xsl:apply-templates select="Book[current_inventory = $MaxInventory]">
        <xsl:sort select="title" data-type="text" order="ascending" />
      </xsl:apply-templates>
    </table>
  </xsl:template>

  <xsl:template match="Book">
    <tr>
      <td><xsl:value-of select="title" /></td>
      <td><xsl:value-of select="current_inventory" /></td>
    </tr>
  </xsl:template>

</xsl:stylesheet>

<table>
  <tr>
    <td>Beauty Secrets</td>
    <td>20</td>
  </tr>
  <tr>
    <td>Picturescapes</td>
    <td>20</td>
  </tr>
</table>

, , XSL ( Dimitre Novatchev). , XSL- " " (.. ). .

+2

These kinds of things are not interesting with XSL. To find out β€œif there are two or more books of the same inventory” in the markup, when the quantity of the inventory can be arbitrary, you need a data structure in which you can store the number of stocks encountered. I would suggest traversing the XML DOM before conversion and making your score on it (much easier), and then create a new document of the nodes you want to convert and apply XSL to it. This will be more direct and less messy than trying to achieve everything in the stylesheet.

-2
source

All Articles