In: Computer Science
While HTML is a markup language with a fixed set of tags that allows users to specify the appearance of a document, XML (extensible markup language) allows the user to create new tags to provide a document structure appropriate to the task at hand. A document has three aspects: structure, appearance, and content. In the case of XML, theseaspectsareseparated,withdifferent meansemployedfordefiningthestructure, the appearance, and the content. Usually, the document type definition (DTD) file describes the structure of a document; the appearance is specified by an extensible style sheet (XSL); and the content is provided in the XML document. In the case of XML, the Web server does not send an HTML document but rather sends the XML and the XSL documents, and the client must have XML/XSL processors to display the document. In this assignment, you will learn to create a simple XML documentfirst.Youwillthenlearnto specifythestructureoftheXMLfileandformat your XML document using XSL. A. Creating a Simple XML Document (20 points) As a programmer, you are asked to provide a cafe comparison table for web surfers. In ordertodo that,youfirstneedtocreatealltherelatedcafeinformationusingasimple XML document. Open Notepad or any XML Editor on your computer system, create a file and name it CAFE.xml. First define the XML version by writing “ ” in the first line. The next line, “”, refers to the type of style sheet used and the name of the file. The structure of the XML file is simple. At the root is a structure called CAFES. This structure at the next level has a repeated number of child structures called CAFE.Inturn, thestructureCAFEhasoneattribute,CAFE_TYPE,andithas five elements: CAFE_NAME FOOD_QUALITY ENVIRON COST_RATING COMMENTS The code for the entire file CAFE.xml is in the following image: B. Formatting XML Using XSL (20 points) 6. Now consider the XSL file, CAFE.xsl. The file accomplishes several tasks: (1) the XSL filegeneratestheHTMLcodethateventuallyrunsonthebrowser.The XSL file has to create the necessary HTML tags for that purpose. (2) As part of the above, the XSL code creates a table that has the tabular headings in the first row.(3)Loopingconstructs allowedinXSLareusedtoreadindividualelements of CAFE in the associated XML file and then to output the values in the HTML table. Preamble. The file begins with a preamble that defines the location of the XSL specification by the following line: “”. If it is not working, please try this line: “”. And, remember to add “” as the first line. Template Match. The line: instructs the system to match the XSL template right from the root stage of the structure described in the XML file (i.e., the entire structure of CAFES/CAFE is to be processed as per the XSL file). 9. The HTML tags for the file and for defining the table are achieved by the following code: 10. The header row of the table is defined by the code: 11. The XML file defines the overall structure of CAFES, which consists of multiple repeated child structure elements CAFE. The file CAFE.xml has five CAFE child structures. XSL provides the necessary language constructs to extractthesevaluesand putstheminanHTMLformatwiththeappropriate formatting.The“for-eachselect” loopingconstructofXSLisusedtoloop through the CAFES structure, and the XSL construct “value-of select” is used to fetch the values of the individual CAFE elements, such as CAFE_NAME and FOOD_QUALITY, and includes them in the table. This “for-each-select” looping constructofXSLactsthesameasaWHILEloopinacomputer program.The looping construct, such as a WHILE loop, is fixed in length, irrespective of how many CAFE child structures are involved. The looping code is as follows: Now you should have keyed in the necessary code to display the comparison table. The last thing is to close up all the open tags: Please remember that XML code is case sensitive. An upper cased letter is never the same as the lower cased same letter. And the select element name should be exactly the same as mentioned in your xml document. Please check both documents carefully. 14. Double-click on your xml document, and you shall see the following image on your computer screen:
XML CODE:
<?xml version="1.0">
<?xml-stylesheet type="text/xsl" href="CAFE.xsl"?>
<CAFES>
<CAFE CAFE_TYPE="Primarily Take-Out">
<CAFE_NAME>New
Mexico</CAFE_NAME>
<FOOD_QUALITY>Average</FOOD_QUALITY>
<ENVIRON>Great!</ENVIRON>
<COST_RATING>$3.50 per
meal</COST_RATING>
<COMMENTS>Should order well
in time.</COMMENTS>
</CAFE>
<CAFE CAFE_TYPE="Brunch">
<CAFE_NAME>Moscow</CAFE_NAME>
<FOOD_QUALITY>Outstanding</FOOD_QUALITY>
<ENVIRON>Average</ENVIRON>
<COST_RATING>$8 per
person</COST_RATING>
<COMMENTS>A good place for
weekend brunch.</COMMENTS>
</CAFE>
<CAFE CAFE_TYPE="Formal Dinner">
<CAFE_NAME>Spring in
Paris</CAFE_NAME>
<FOOD_QUALITY>Excellent</FOOD_QUALITY>
<ENVIRON>Fantastic</ENVIRON>
<COST_RATING>$19 per
plate</COST_RATING>
<COMMENTS>Got to be rich to
go there.</COMMENTS>
</CAFE>
<CAFE CAFE_TYPE="Asian Grill">
<CAFE_NAME>Big
Saigon</CAFE_NAME>
<FOOD_QUALITY>Good</FOOD_QUALITY>
<ENVIRON>OK</ENVIRON>
<COST_RATING>$7 per
plate</COST_RATING>
<COMMENTS>Good for Quick
Lunch</COMMENTS>
</CAFE>
<CAFE CAFE_TYPE="Take-Out">
<CAFE_NAME>Aby's
Diner</CAFE_NAME>
<FOOD_QUALITY>Fantastic</FOOD_QUALITY>
<ENVIRON>Splendid</ENVIRON>
<COST_RATING>$7 per
plate</COST_RATING>
<COMMENTS>Always eat out at
Aby's Diner.</COMMENTS>
</CAFE>
</CAFES>
Save it as CAFE.xml.
The file begins with a preamble that defines the location of the
XSL specification by the following line: <xsl:stylesheet
xmlns:xsl=“http://www.w3.org/TR/WD-xsl”>”. If it is not working,
please try this line:
“<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">”.
And, remember to add “<?xml version=“1.0” ?>” as the first
line.
Template Match. The line: <xsl:template match=“/”> instructs the system to match the XSL template right from the root stage of the structure described in the XML file (i.e.,the entire structure of CAFES/CAFE is to be processed as per the XSL file).
The HTML tags for the file and for defining the table are achieved by the following code:
<?xml version="1.0">
<!-- XML Declaration by June Lu-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" version"1.1">
<!--Declaration that the document is a stylesheet -->
<xsl:template match"/">
<!-- Apply template from the root node -->
<HTML>
<BODY>
<P><B>This is an XML
Document on View! </B></P>
<P><EM>It uses XSL
Sheet </EM></P>
<P>These XML Docuements have
been produced</P>
</BODY>
</HTML>
The header row of the table is defined by the code:
<!-- Set up the first, that is, the header row -->
<TR>
<TD><B>Cafe
Type</B></TD>
<TD><B>Cafe
Name</B></TD>
<TD><B>Food</B></TD>
<TD><B>Environment</B></TD>
<TD><B>Meal
Cost</B></TD>
<TD><B>Comments</B></TD>
</TR>