Author: Ma Xiaoning
xml (extensible markup language) may look like some kind of w3c standard - it has no practical impact now, and it will be a long time later, even if it can come in handy later. But in fact, it has now been applied. So, don't wait until the xml has been added to your favorite html editor before starting to use it. It can solve various internal problems and b2b system problems now.
In, we use XML to normalize data representations between different systems, including Java objects to HTML data display.
It is particularly important to note that we found that as long as standardization is implemented with a very basic xml structure, data can be more easily shared and manipulated. In this process, we have discovered many effective ways to use xml. The following is a detailed introduction to our current application situation.
standardization
Before using xml, create an XML data format that is different from the information you want to use.
Generate dynamic xml
Generating html from the database is not new, but generating xml is very new. Here we introduce the specific generation steps.
Use xsl as template language
xsl (extensible stylesheet language) is a good way to define the format of xml data display, and it will be more effective if written into several static templates.
Generate html
xml plus xsl is equivalent to html. This may sound wrong, but the html page that users see is actually the effect of xml and xsl.
1. Standardization
The ability of xml comes from its flexibility. Unfortunately, it is sometimes so flexible that you will face a blank page and worry about how to solve the problem.
In any XML project, the first step is to create a standard data format. For this purpose, you have to make the following decision:
•What data should be involved
• Do you want to use dtd (file type definition)
• Do you want to use dom (document object model) or sax (simplified API of xml) parsing
Confirm the data:
Because there is no standard XML format, developers are free to develop their own formats. However, if your format can only be recognized by one application, then you can only run the program to use the format. It would obviously be more helpful if there are other programs that can also read your xml format. If a certain XML format is modified, the system using it may also need to be modified, so you should create the complete format as possible. Because most systems ignore tags that they cannot recognize, the safest way to change an XML format is to add tags instead of modifying them.
Click here to view an instance of xml data format
In, we view all product data required for different product displays. Although not all pages use all data, we have developed a very complete XML data format for all data. For example, our product details page displays more data than product browsing pages. However, we still use the same data format in both cases, because each page's xsl template uses only the fields it needs.
Whether to use dtd
In, we use well-organized xml instead of just correct xml, because the former does not require dtd. dtd adds a processing layer between the user clicking and seeing the page. We found that this layer requires too much processing. Of course, it is still good to use dtd when communicating with other companies in XML format. Because dtd can ensure the correct data structure when sent and accepted.
Select a parsing engine
Now, there are several parsing engines available. Which one you choose is almost entirely up to your application needs. If you decide to use dtd, then this parsing engine must enable your xml to be verified by dtd. You can put verification into a process, but that will affect performance.
Sax and dom are two basic analytical models. Sax is based on events, so when the xml is parsed, the event is sent to the engine. Next, the event is 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, just as determined by the xsl style sheet. The debate on the sax model mainly focuses on excessive memory reduction of dom structures and speeding up the parsing time of xsl style sheets.
However, we found that many systems using sax do not fully utilize its capabilities. These systems use it to build a dom structure and send events through the dom structure. With this method, the dom must be built from the stylesheet before any xml processing, so the performance will be degraded.
2. Generate dynamic xml
Once the xml format is established, we need a way to port it dynamically from the database.
Generating XML documents is relatively simple, because it only requires a system that can handle strings. We have built a system that uses java servlet, enterprise javabean server, jdbc and rdbms (relational database management system).
• servlet handles product information requests by handing over the task of generating XML documents to enterprise javabean (ejb).
• ejb uses jdbc to query the required product details from the database.
•ejb generates an xml file and passes it to the servlet.
• servlet calls the parsing engine to create html output from xml files and static xsl stylesheets.
(For additional information about xsl applications, see Using xsl as template language.)
Example of generating xml
The real code for creating an XML document string in java can be divided into several methods and classes.
The code to start the XML generation process is placed in the ejb method. This instance immediately creates a stringbuffer to store the generated xml string.
stringbuffer xml = new stringbuffer();
(("/browse_find/", "browse", request));
(());
(("browse");
(());
The following three () arguments are themselves calls to other methods.
Generate file header
The first additional method calls the xmllutils 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 version=\"1.0\"?>\n")
.append("<?xml-stylesheet href=\"")
.append(stylesheet).append("\"")
.append(" type =\"text/xsl\"?>\n");
("<").append(page).append(">\n");
return ();
}
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. Finally, the item-level tag is included (<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/" type="text/xsl"?> <browse>
Fill in product information
After 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 produce its xml representation. The first method toxml() creates product nodes by generating <product> and </product> tags. Then it calls internalxml(), which can provide what the product xml needs. internalxml() is a series of() calls. The stringbuffer is also converted into a string and returned to the control method.
public string toxml()
{
stringbuffer xml = new stringbuffer("<product>\n");
(internalxml());
("</product>\n");
return ();
}
public string internalxml()
{
stringbuffer xml = new
stringbuffer("\t")
.append(producttype).append("\n");
("\t").append(())
.append("\n");
("\t").append(())
.append("\n");
("\t").append(())
.append("\n");
Lock?
("\t").append(amount).append("\n");
("\t").append(vendor).append("\n");
("\t\n");
("\t").append(pubdesc).append("\n");
("\t").append(vendesc).append("\n";
Lock?
return ();
}
Close the file
Finally, the () method is called. This call closes the xml tag (in this case), and finally completes the architecture xml file. The entire stringbuffer from the control method is also converted into a string and returned to the servlet that handles the initial http request.
3. Use xsl as template language
In order to obtain the html output, we combine the generated xml file with the xsl template that controls how the xml data is represented. Our xsl template consists of carefully organized xsl and html tags.
Start template creation
The beginning of our xsl template is similar to the following code. The first line of code is required, and this file is defined as an xsl style sheet. xmlns:xsl= attribute refers to the xml namespace used in this file, while 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 from <xsl:template> determines the pattern of the xsl template. The match attribute is required, pointing to the xml tag <basketpage> here. In our system, the <basketpage> tag contains the <product> tag, which allows the xsl template to access 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 the well-organized html. Since it will be processed by the xml parsing engine, it must comply with all the rules of a well-organized xml. Essentially, this means that all start tags must have corresponding end tags. For example, a <p> tag that is not normally ended must be closed with </p>.
<xsl:stylesheet xmlns:xsl="http:///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 representation. The following are two commonly used tags.
choose
The <xsl:choose> tag is similar to the beginning of the if-then-else structure in traditional programming languages. In xsl, the choice tag indicates that the assignment will trigger the occurrence of the action in the part entered by the code. The <xsl:when> tag with the assignment attribute follows the choice tag. If the assignment is correct, the content located between the start and end tags of <xsl:when> will be used. If the assignment is wrong, use the content between the start and end tags of <xsl:otherwise>. The whole part ends with </xsl:choose>.
In this example, the when tag will check the xml for the quantity tag. If the quantity tag contains the error attribute of the value true, the quantity tag will display the table cell listed below. If the value of the property is not true, xsl will display the contents between 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="'>/images/i-catalog/sparks_images/sparks_ui/"/></td>
<td valign="top" bgcolor="#ffffff" colspan="2"><font face="verdana, arial" size="1" color="#cc3300"><b>*not enough in stock. your quantity was adjusted accordingly.</b></font></td>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
for-each
The <xsl:for-each> tag can be used to apply the same style sheet to multiple situations of similar xml data. For us, we can take a series of product information from the database and format it uniformly on the web page. Here is an example:
<xsl:for-each select="package">
<xsl:apply-templates select="product"/>
</xsl:for-each>
The for-each loop starts when the program encounters a tag. This loop will end when the program encounters a tag. Once this loop runs, the template is applied every time the tag appears.
4. Generate html
At some point in the future, the browser will integrate the XML parsing engine. By then, you can send xml and xsl files directly to the browser, and the browser displays xml data according to the rules listed in the style sheet. However, before this, developers will have to create parsing functions on their server-side systems.
In, we have integrated an XML parser in the java servlet. This parser uses a mechanism called xslt (xsl transformation) to add xml data to the xsl template according to the xsl tag.
When our java servlet handles http request, the servlet retrieves the dynamically generated xml, and the xml is then passed to the parsing engine. According to the instructions in the xml file, the parsing engine looks for the appropriate xsl style sheet. The parser creates an html file through the dom structure, and then passes this file to the user who issued the http request.
If you choose to use the sax model, the parser will read through the xml source program and create an event for each xml tag. The event corresponds to the xml data, and finally inserts data into the style sheet according to the xsl tag.