本文发表在 rolia.net 枫下论坛Generating HTML: how it works
The generation of the HTML from the canonical XML on the server side is easy. It's done in the ASP page by the XML DOM object. These snippets are from the ASP code in det.asp, which handles requests for item detail pages.
First, we see if the HTML page is in the cache we've created. If it is, we can just send it out.
' DetKey is the cache key for this particular URL
' Attempt to retrieve HTML from cache
DetHTML = Application("Cache")(DetKey)
' If HTML was not cached, generate HTML and add to cache
If DetHTML = "" Then
' generate HTML from XML storing in DetHTML; shown next
' ...
End If ' this section will be repeated below
' Write HTML detail to client
Response.BinaryWrite(DetHTML)
We use Response.BinaryWrite rather than just Response.Write because the text, stored in DetHTML, is in UTF-8 character format, which allows us to use extended character sets (as for Asian languages). If we used Response.Write, the results would be mangled because it would be assumed that the results where ANSI.
The code to generate the HTML from XML, if it's not already in the cache, is relatively simple. (The following four code snippets go inside the if statement in the preceding code example.)
First, we create objects to hold the XML and XSL documents.
' Instantiate XMLDOM objects for XML and XSL
Set XMLDoc = Server.CreateObject("Microsoft.XMLDOM")
Set XSLDoc = Server.CreateObject("Microsoft.XMLDOM")
Next, we create a Workflow object and use it to obtain the XML data. We get the XSL style sheet directly from the server.
' Instantiate Workflow component
Set WFL = Server.CreateObject("d4Wflow.cWorkflow")
' Load XMLDoc with XML representation of item from workflow
XMLDoc.async = false
XMLDoc.loadXML(WFL.GetItemByItemId(DetId))
' Load XSLDoc with style sheet
XSLDoc.async = false
XSLDoc.load(Server.MapPath("det.xsl"))
Next, we call the magical transformNode method to cause the object containing the XML data to use the XSL style sheet to generate HTML. And, having generated this HTML, we add it to the cache so we won't have to generate it again.
' Generate HTML from XML and XSL
DetHTML = XMLDoc.documentElement.transformNode(
XSLDoc.documentElement)
' Add HTML to cache
Application("Cache").Add DetKey, DetHTML
Finally, we rejoin the other fork of the if statement (we showed this before) and write the HTML code we generated into the document that will be sent to the client.
End If ' this section is a repeat of the code above
' Write HTML detail to client
Response.BinaryWrite(DetHTML)更多精彩文章及讨论,请光临枫下论坛 rolia.net
The generation of the HTML from the canonical XML on the server side is easy. It's done in the ASP page by the XML DOM object. These snippets are from the ASP code in det.asp, which handles requests for item detail pages.
First, we see if the HTML page is in the cache we've created. If it is, we can just send it out.
' DetKey is the cache key for this particular URL
' Attempt to retrieve HTML from cache
DetHTML = Application("Cache")(DetKey)
' If HTML was not cached, generate HTML and add to cache
If DetHTML = "" Then
' generate HTML from XML storing in DetHTML; shown next
' ...
End If ' this section will be repeated below
' Write HTML detail to client
Response.BinaryWrite(DetHTML)
We use Response.BinaryWrite rather than just Response.Write because the text, stored in DetHTML, is in UTF-8 character format, which allows us to use extended character sets (as for Asian languages). If we used Response.Write, the results would be mangled because it would be assumed that the results where ANSI.
The code to generate the HTML from XML, if it's not already in the cache, is relatively simple. (The following four code snippets go inside the if statement in the preceding code example.)
First, we create objects to hold the XML and XSL documents.
' Instantiate XMLDOM objects for XML and XSL
Set XMLDoc = Server.CreateObject("Microsoft.XMLDOM")
Set XSLDoc = Server.CreateObject("Microsoft.XMLDOM")
Next, we create a Workflow object and use it to obtain the XML data. We get the XSL style sheet directly from the server.
' Instantiate Workflow component
Set WFL = Server.CreateObject("d4Wflow.cWorkflow")
' Load XMLDoc with XML representation of item from workflow
XMLDoc.async = false
XMLDoc.loadXML(WFL.GetItemByItemId(DetId))
' Load XSLDoc with style sheet
XSLDoc.async = false
XSLDoc.load(Server.MapPath("det.xsl"))
Next, we call the magical transformNode method to cause the object containing the XML data to use the XSL style sheet to generate HTML. And, having generated this HTML, we add it to the cache so we won't have to generate it again.
' Generate HTML from XML and XSL
DetHTML = XMLDoc.documentElement.transformNode(
XSLDoc.documentElement)
' Add HTML to cache
Application("Cache").Add DetKey, DetHTML
Finally, we rejoin the other fork of the if statement (we showed this before) and write the HTML code we generated into the document that will be sent to the client.
End If ' this section is a repeat of the code above
' Write HTML detail to client
Response.BinaryWrite(DetHTML)更多精彩文章及讨论,请光临枫下论坛 rolia.net