XML assignment to create a page that displays times that various zoo animals are
ID: 3831505 • 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:
Create a new XSLT style sheet document Zoo.xsl with the basic structure of a stylesheet.
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.
The assignment should be done in Altova XML Spy.
Explanation / Answer
<?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>
That’s how you start an XSLT style sheet (in fact, if you’re using a standalone program that requires you to give the name of the style sheet you’re using, you can usually omit the <xsl:stylesheet> element). To work with specific nodes in an XML document, XSLT uses templates. When you match or select nodes, a template tells the XSLT processor how to transform the node for output. In this example, I want to replace the root node with a whole new HTML document, so I start by creating a template with the <xsl:template> element, setting the match attribute to the node to match, "/":
When the root node is matched, the template is applied to that node. In this case, I want to replace the root node with an HTML document, so I just include that HTML document directly as the content of the <xsl:template> element:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.