×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

见内, 是你想要的吗?

本文发表在 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
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / XML问题: 我有一个XML资源文件, 想通过不同的XSLT显示, 但在XML里面只能指定唯一的XSLT. 这怎么实现?
    • XML 和 XSLT 是可以分开的. 用程序指定不同的XSLT 解释 XML
      • Detail?
        • 有MSDN吗? 如果是用MS技术开发, 检索XML , XSLT 能得出无数的例子....
          • 有MSDN, 就是因为查不到才问的. 唯一一个有点像的例子, 同一个XML在不同目录做了COPY, 并不是我需要的
            • 我帮你找找....
            • 见内, 是你想要的吗?
              本文发表在 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
              • 谢谢, 修改一下应该可以. 能给我URL吗? 想看看例子
                • 对不起,我是从本地MSDN上找出来的, 我去MS网站上找找看....
                • 这个简洁, 不错
                  • Great! That's really what I want!