Author: Ma Xiaoning
XML (Extensible Markup Language) may look like some kind of w3c standard - it has no practical impact now, and even if it comes in handy in the future, it will be a long time later. But in fact, it is already being used. So, don't wait until XML is added to your favorite HTML editor to start using it. It can solve various internal problems and b2b system problems now.
At sparks.com we use xml to standardize data representation between different systems, from java objects to html data displays.
In particular, we've found that data can be shared and manipulated more easily when it's standardized in a very basic XML structure. In this process, we discovered many effective ways to use xml. The following describes our current application in detail.
Standardize Before using XML, create an XML data format that is different from the information you want to use.
Generate dynamic xml
Generating html from a database is not new, but generating xml is. Here we introduce the specific generation steps.
Using xsl as template language
XSL (Extensible Stylesheet Language) is a good way to define the XML data display format, and it will be more effective if written as several static templates.
Generate html
xml plus xsl equals html. This may not sound right, but our html page that users see is actually the result of xml and xsl.
1.
The ability to standardize XML comes from its flexibility. But unfortunately, it's sometimes so flexible that you're left with a blank page wondering how to solve the problem.
In any XML project, the first step is to create a standard data format. To do this you will need to make the following decisions:
• What data will be involved?
• Whether to use dtd (file type definition)
• Whether you want to use DOM (Document Object Model) or SAX (Simplified API for XML) to parse
and determine the data:
Because there is no standard XML format, developers are free to develop their own formats. However, if your format is only recognized by one application, then you can only run that application to use the format. It would obviously be more helpful if there were other programs that could also read your xml format. If an XML format is modified, the system using it may also need to be modified, so you should build the format as complete as possible. Because most systems ignore tags they don't recognize, the safest way to change the format of an XML is to add tags, rather than modify them.
Click here to view xml data format example
At sparks.com, we looked at all the product data required for different product presentations. Although not all pages use all data, we have developed a very complete xml data format suitable for all data. For example, our product details page displays more data than our product browse page. However, we still use the same data format in both cases because each page's xsl template only uses the fields it needs.
Whether to use dtd
At sparks.com we use well-organized xml rather than just correct xml as the former does not require a dtd. DTD adds a layer of processing between the user clicking and seeing the page. We found this layer required too much processing. Of course, it's still nice to use DTDs when communicating with other companies in XML format. Because dtd can ensure that the data structure is correct when sending and receiving.
Choosing a Parsing Engine Now, there are several parsing engines that can be used. Which one you choose depends almost entirely on your application needs. If you decide to use DTD, then the parsing engine must be able to enable your XML to be verified by DTD. You could put the validation into a separate process, but that would impact performance.
Sax and dom are two basic parsing models. SAX is event based, so when the xml is parsed, events are sent to the engine. Next, the events are synchronized with the output file. The DOM parsing engine establishes a hierarchical tree structure for dynamic XML data and XSL style sheets. By randomly accessing the DOM tree, XML data can be provided as if determined by an XSL stylesheet. The debate on the SAX model mainly focuses on excessive memory reduction of the DOM structure and speeding up the parsing time of the XSL style sheet.
However, we found that many systems using sax did not use it to its full capabilities. These systems use it to build DOM structures and send events through DOM structures. With this approach, the DOM must be built from the stylesheet before any XML processing, so performance will suffer.
2. Generate dynamic xml
Once the xml format is established, we need a method to dynamically transplant it from the database.
Generating xml documents is relatively simple because it only requires a system that can handle strings. We built a system using java servlet, enterprise javabean server, jdbc and rdbms (relational database management system).
• The servlet handles product information requests by offloading the task of generating xml documents to the enterprise javabean (ejb).
• ejb uses jdbc to query the required product details from the database.
• ejb generates the xml file and passes it to the servlet.
The servlet calls the parsing engine to create html output from xml files and static xsl style sheets.
(For additional information on the application of XSL, see Using XSL as a Template Language.)
Example of Generating XML The actual code for creating an XML document string in Java can be divided into several methods and classes.
The code that starts the xml generation process is placed in the ejb method. This instance will immediately create a stringbuffer to store the generated xml string.
stringbuffer xml = new stringbuffer();
xml.append(xmlutils.begindocument("/browse_find/browse.xsl", "browse", request));
xml.append(product.toxml());
xml.append(xmlutils.enddocument("browse");
out.print(xml.tostring());
The following three xml.append() variables themselves are calls to other methods.
Generate file header The first additional method calls the xmlutils class to generate the xml file header. The code in our java servlet is as follows:
public static string begindocument(string stylesheet, string page)
{
stringbuffer xml = new stringbuffer();
xml.append("<?xml version="1.0"?>n")
.append("<?xml-stylesheet href="")
.append(stylesheet).append(""")
.append(" type ="text/xsl"?>n");
xml.append("<").append(page).append(">n");
return xml.tostring();
}
This code generates the xml file header. The <?xml> tag defines this file as an xml file that supports version 1.0. The second line of code points to the location of the correct style sheet to display the data. The last thing included is the item-level tag (<browse> in this example). At the end of the file, only the <browse> tag needs to be closed.
<?xml version="1.0"?> <?xml-stylesheet href="/browse_find/browse.xsl" type="text/xsl"?> <browse>
After filling in the product information and completing the file header, the control method will Call the java object to generate its xml. In this example, the product object is called. The product object uses two methods to generate its xml representation. The first method toxml() creates the product node by generating <product> and </product> tags. It then calls internalxml(), which provides the required content for the product xml. internalxml() is a series of stringbuffer.append() calls. The stringbuffer is also converted to a string and returned to the control method.
public string toxml()
{
stringbuffer xml = new stringbuffer("<product>n");
xml.append(internalxml());
xml.append("</product>n");
return xml.tostring();
}
public string internalxml()
{
stringbuffer xml = new
stringbuffer("t")
.append(producttype).append("n");
xml.append("t").append(idvalue.trim())
.append("n");
xml.append("t").append(idname.trim())
.append("n");
xml.append("t").append(page.trim())
.append("n");
Pang?
xml.append("t").append(amount).append("n");
xml.append("t").append(vendor).append("n");
xml.append("tn");
xml.append("t").append(pubdesc).append("n");
xml.append("t").append(vendesc).append("n";
Pang?
return xml.tostring();
}
Finally, the xmlutils.enddocument() method is called after closing the file. This call closes the xml tag (in this case) and finally completes the structured xml file. The entire stringbuffer from the control method is also converted to a string and returned to the servlet that handled the original http request.
3. Use xsl as the template language
. In order to get html output, we combine the generated xml file with the xsl template that controls how the xml data is represented. Our xsl templates consist of carefully organized xsl and html tags.
Start building the template The beginning of our xsl template is similar to the code below. The first line of code is required and defines this file as an xsl style sheet. The xmlns:xsl= attribute refers to the xml namespace used by this file, and the version= attribute defines the version number of the namespace. At the end of the file we close the tag.
The second line of code starting with <xsl:template> determines the mode of the xsl template. The match attribute is required and here points to the xml tag <basketpage>. In our system, the <basketpage> tag contains the <product> tag, which allows the xsl template to access the product information embedded in the <product> tag. Once again we have to close the <xsl:template> tag at the end of the file.
Next, let's take a look at well-organized html. Since it will be processed by the XML parsing engine, it must comply with all the rules of well-organized XML. Essentially, this means that all opening tags must have a corresponding closing tag. For example, a <p> tag that is not normally closed must be closed with </p>.
<xsl:stylesheet xmlns:xsl=" http://www.w3.org/1999/xsl/transform "
version="1.0">
<xsl:template match="basketpage">
<html>
<head>
<title>shopping bag / adjust quantity</title>
</head>
<body bgcolor="#cccc99" bgproperties="fixed" link="#990000" vlink="#990000">
<br>
?br> </xsl:template>
</xsl:stylesheet>
Within the body of the template, there are many xsl tags used to provide logic for data presentation. Two commonly used tags are explained below.
choose
The <xsl:choose> tag is similar to the beginning of an if-then-else structure in traditional programming languages. In XSL, the choose tag indicates that in the part where the code enters, the assignment will trigger the action. The <xsl:when> tag with assigned attributes follows the choose tag. If the assignment is correct, the content between the opening and closing tags of <xsl:when> will be used. If the assignment is wrong, the content between the opening and closing tags of <xsl:otherwise> is used. The entire section ends with </xsl:choose>.
In this example, the when tag checks the xml for the quantity tag. If the quantity tag contains an error attribute with a value of true, the quantity tag will display the table cells listed below. If the value of the attribute is not true, xsl will display the content between the otherwise tags. In the example below, if the error attribute is not true, nothing will be displayed.
<xsl:choose>
<xsl:when test="quantity[@error='true']">
<td bgcolor="#ffffff"><img height="1" width="1" src="