Since not all browsers support XSLT, one solution is to transform the XML to XHTML on the server.
ÒòΪ²»ÊÇËùÓеÄä¯ÀÀÆ÷¶¼Ö§³ÖXSLT£¬ËùÒÔÈËÃÇÌá³öÁËÒ»¸öÔÚ·þÎñÆ÷¶Ë°ÑXMLת»»³ÉXHTMLµÄ½â¾ö·½°¸¡£
A Cross Browser Solution
Ò»¸ö½»²æä¯ÀÀÆ÷½â¾ö·½°¸
In the previous chapter we explained how XSLT can be used to transform a document from XML to XHTML in the browser. We created a JavaScript that used an XML parser to do the transformation. The JavaScript solution will not work in a browser that doesn't have an XML parser.
ÔÚǰһÕÂÎÒÃǽâÊÍÁËÔõÑùÔÚä¯ÀÀÆ÷ÖÐÓÃXSLT°ÑÎļþ´ÓXMLת»»³ÉXHTML¡£ÎÒÃÇ´´½¨ÁËÒ»¸öÓÃXML½âÎöÆ÷À´×öת»»µÄJavaScript¡£JavaScript½â¾ö³ÌÐò½«²»»áÔÚûÓÐXML½âÎöÆ÷µÄä¯ÀÀÆ÷ÉÏÔËÐС£
To make XML data available to all kind of browsers, we must transform the XML document on the SERVER and send it as XHTML back to the browser.
ÏëʹXMLÊý¾Ý¶Ô¸÷ÖÖä¯ÀÀÆ÷¶¼ÊÊÓã¬ÎÒÃDZØÐëÔÚ·þÎñÆ÷ÉÏת»»XMLÎļþ£¬²¢°ÑËüÒÔXHTMLµÄÐÎʽ·¢ËÍ»Øä¯ÀÀÆ÷¡£
That's another beauty of XSLT. One of the design goals for XSLT was to make it possible to transform data from one format to another on a server, returning readable data to all kinds of browsers.
ÕâÊÇXSLTµÄÁíÒ»¸ö÷ÈÁ¦¡£XSLTµÄÆäÖÐÒ»¸öÉè¼ÆÄ¿±ê¾ÍÊÇÄܹ»ÈÃËüÔÚ·þÎñÆ÷¶Ë½«Êý¾Ý´ÓÒ»ÖÖ¸ñʽת»¯ÎªÁíÒ»ÖÖ¸ñʽ£¬²¢°ÑËùÓпɶÁµÄÊý¾Ý·µ»Øµ½¸÷ÖÖä¯ÀÀÆ÷ÉÏ¡£
The XML File and the XSLT File
XMLÎļþºÍXSLTÎļþ
Look at the XML document that you have seen in the previous chapters:
ÏÂÃæÎÒÃÇÀ´»Ø¹ËÒ»ÏÂǰһÕÂʹÓùýµÄXMLÎļþ£º
<?xml version="1.0" encoding="ISO-8859-1"?> |
View the XML file.
²é¿´XMLÎļþ¡£.
And the accompanying XSL style sheet:
¸½ËæµÄXSLÑùʽ±í£º
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:template match="/"> </xsl:stylesheet> |
Notice that the XML file does not have a reference to the XSL file.
×¢ÒâXMLÎļþºÍXSLÎļþ²¢ÎÞ¹ØÁª
IMPORTANT: The above sentence indicates that an XML file could be transformed using many different XSL style sheets.
ÖØµã£ºÉÏÃæµÄ¾ä×ÓÖ¸³ö£ºXMLÎļþ²»Ó¦¸Ãͨ¹ý²»Í¬µÄXSLÑùʽ±í½øÐÐת»»¡£
Transforming XML to XHTML on the Server
ÔÚ·þÎñÆ÷¶Ë°ÑXMLת»»³ÉXHTML
Here is the ASP source code needed to transform the XML file to XHTML on the server:
Õâ¶ùµÄ¶¯Ì¬·þÎñÆ÷Ö÷Ò³£¨ASP£©Ô´´úÂëÐèÒªÔÚ·þÎñÆ÷¶Ë°ÑXMLÎļþת»»³ÉXHTML¡£
<% |
Tip: If you don't know how to write ASP, you can study our ASP tutorial.
Ìáʾ: Èç¹ûÄã²»ÖªµÀÔõÑùÊéдASP£¬Äã¿ÉÒÔÁ˽âÒ»ÏÂASP ½Ì³Ì
The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML file into memory. The second block of code creates another instance of the parser and loads the XSL file into memory. The last line of code transforms the XML document using the XSL document, and sends the result as XHTML to your browser. Nice!
µÚÒ»¿é´úÂë´´½¨ÁËÒ»¸ö΢ÈíXML½âÎöÆ÷£¨XMLDOM£©µÄʵÀý£¬²¢°Ñ´ËÎļþ¼ÓÔØµ½´æ´¢Æ÷Àï¡£µÚ¶þ¿é´úÂë´´½¨ÁËÁíÍâÒ»¸ö½âÎöÆ÷µÄʵÀý£¬²¢°ÑXSLÎļþ¼ÓÔØµ½´æ´¢Æ÷ÖС£×îºóÒ»ÐдúÂëͨ¹ýʹÓÃXSLÎļþÀ´×ª»»XMLÎļþ£¬²¢½«ÆäÒÔXHTMLµÄÐÎʽ°Ñ½á¹û·¢µ½ä¯ÀÀÆ÷ÉÏ¡£Õæ°ô£¡