Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

XML assignment to create a page that displays times that various zoo animals are

ID: 3832553 • Letter: X

Question

XML assignment to create a page that displays times that various zoo animals are fed.

The Downtown Zoo has hired you to help work on their web site. They would like to create a page that displays the times that various animals are being fed. The Zoo has the information stored in an XML document, Zoo.xml. The web page should list the feeding times across the top as hyperlinks to the details of which animals are fed at that time. The page should look like this:

   11:00            14:00              15:00               16:00               9:00

11:00 Feeding Time For:

Lion Tortise

14:00 Feeding Time For:

Penguin Hippo

15:00 Feeding Time For:

Flamingo

16:00 Feeding Time For:

Ape

9:00 Feeding Time For:

Elephant

Rattle Snake

Zoo.xml

<?xml version='1.0'?>

<?xml-stylesheet href="Zoo.xsl" type="text/xsl"?>

<!-- XML -->

<zoo>

<animal name="Lion">

<feeding-time>11:00</feeding-time>

</animal>

<animal name="Penguin">

<feeding-time>14:00</feeding-time>

</animal>

<animal name="Elephant">

<feeding-time>9:00</feeding-time>

</animal>

<animal name="Tortise">

<feeding-time>11:00</feeding-time>

</animal>

<animal name="Ape">

<feeding-time>16:00</feeding-time>

</animal>

<animal name="Hippo">

<feeding-time>14:00</feeding-time>

</animal>

<animal name="Rattle Snake">

<feeding-time>9:00</feeding-time>

</animal>

<animal name="Flamingo">

<feeding-time>15:00</feeding-time>

</animal>

</zoo>

To complete this task you should do the following steps:

1. Create a new XSLT style sheet document Zoo.xsl with the basic structure of a stylesheet.

2. Create a stylesheet that displays the feeding times as links across the top of the page, and a list of the animals that are fed at each time within the page.

So far I have this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template match="/">
<html>
<body>
  
   <table>
       <tbody>
          <tr>
           <xsl:for-each select="zoo/animal">
                   <th>
                       <a href="#"><xsl:value-of select="feeding-time" /></a>
                   </th>
               </xsl:for-each>
           </tr>
       </tbody>
   </table>

       <xsl:for-each select="zoo/animal">
       <br />
       <table>
   <tbody>
       <tr>
           <td>
               <h4><xsl:value-of select="feeding-time"></xsl:value-of> Feeding Time For:</h4>
                          
               <span>
               <xsl:value-of select="@name"></xsl:value-of>
               </span>
           </td>
       </tr>      
       </tbody>
       <br />
      
</table>
       </xsl:for-each>
  
  
</body>
</html>
</xsl:template>

</xsl:stylesheet>

The only problem is that it is not grouping the animals based on the specific time. I am getting an output that reads throught the xml file and prints out each time, which means that I have repreated times. The figure in the directions above is what the output should look like. I have tried using group-by and muenchian grouping but can not figure out how to apply it to my current code. Can you please edit my code so that the times are grouped with the animals for that particular time?

Explanation / Answer

XSLT generate-id() Function:

The generate-id() method returns a string value that uniquely identifies a specified node for example feeding time. Here we used to use as anchors using keys.

If the node-set specified is empty, an empty string is returned. If you omit the node-set parameter,
it defaults to the current node

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

<xsl:key name="feedtime" match="feeding-time"
use="."/>

<xsl:key name="animalfeedtime" match="@name"
use="../feeding-time"/>

<xsl:template match="/">
<xsl:apply-templates/>

<xsl:apply-templates mode="group"/>
</xsl:template>

<xsl:template match=
"feeding-time[generate-id()
               =
                generate-id(key('feedtime',.)[1])
               ]
">

<a href="#{generate-id()}">
<xsl:value-of select="."/>
</a>
<xsl:text> </xsl:text>
</xsl:template>

<xsl:template mode="group" match=
"feeding-time[generate-id()
               =
                generate-id(key('feedtime',.)[1])
               ]
">

<br /><p id="{generate-id()}"><xsl:text/>

<b><xsl:value-of select="."/> Feeding Time for:</b></p>

<xsl:apply-templates select="key('animalfeedtime', .)"/>
</xsl:template>

<xsl:template match="@name">
<br /><xsl:value-of select="."/>
</xsl:template>

<xsl:template match="text()"/>
<xsl:template mode="group" match="text()"/>
</xsl:stylesheet>

Transformed xml:

Hope this helps.