If your browser supports it, XSLT can be used to transform the document to XHTML in your browser.
Èç¹ûÄãµÄä¯ÀÀÆ÷Ö§³ÖXSLT£¬Ëü¿ÉÒÔÔÚÄãµÄä¯ÀÀÆ÷ÖаÑÎļþת»»³ÉXHTML¡£
A JavaScript Solution
JavaScript½â¾ö·½°¸
In the previous chapters we have explained how XSLT can be used to transform a document from XML to XHTML. We did this by adding an XSL style sheet to the XML file and let the browser do the transformation.
ÔÚÏÈǰһÕ£¬ÎÒÃÇÒѾ½âÊÍÁËÔõôÓÃXSLT°ÑÒ»·ÝÎĵµ´ÓXMLת»»³ÉXHTML¡£ÎÒÃÇͨ¹ýÏòXMLÎļþÖÐÌí¼ÓÒ»¸öXSLÑùʽ±í£¬²¢ÈÃä¯ÀÀÆ÷¶ÔÆä½øÐÐת»»¡£
Even if this works fine, it is not always desirable to include a style sheet reference in an XML file (e.g. it will not work in a non XSLT aware browser.)
¼´Ê¹ÕâÑù×öÔËÐеò»´í£¬ÄãÒ²²»¿ÉÒÔ¾ø¶ÔµØ½«ÆäÊÓΪһ¸öXMLÎļþµÄÑùʽ²Î¿¼£¨±ÈÈ磬ÔÚ²»Ö§³ÖXSLTµÄä¯ÀÀÆ÷ÖУ¬Ëü½«²»»áÔËÐУ©¡£
A more versatile solution would be to use a JavaScript to do the transformation.
¸üͨÓõĽâ¾ö·½°¸ÊÇʹÓÃJavaScriptÀ´×öת»»¡£
By using a JavaScript, we can:
ͨ¹ýʹÓÃJavaScript£¬ÎÒÃÇ¿ÉÒÔ£º
- do browser-specific testing
×öä¯ÀÀÆ÷µÄϸ½Ú£¨browser-specific£©²âÊÔ - use different style sheets according to browser and user needs
¸ù¾Ýä¯ÀÀÆ÷ºÍʹÓÃÕßµÄÐèҪʹÓò»Í¬µÄÑùʽ±í¡£
That is the beauty of XSLT! One of the design goals for XSLT was to make it possible to transform data from one format to another, supporting different browsers and different user needs.
ÄÇÊÇXSLTµÄ÷ÈÁ¦¡£ÆäÖÐÒ»¸öXSLTµÄÉè¼ÆÄ¿±ê¾ÍÊÇΪÁËÔÚÖ§³Ö²»Í¬ä¯ÀÀÆ÷ºÍ²»Í¬Ê¹ÓÃÕßÐèÇóµÄÇé¿öÏ£¬Ê¹ËüʵÏÖ´ÓÒ»¸ö¸ñʽµ½ÁíÒ»¸ö¸ñʽµÄÊý¾Ýת»»¡£
XSLT transformation on the client side is bound to be a major part of the browsers work tasks in the future, as we will see a growth in the specialized browser market (Braille, aural browsers, Web printers, handheld devices, etc.)
ÔÚ¿Í»§¶Ë½øÐÐXSLTµÄת»»Ò»¶¨ÊÇδÀ´ä¯ÀÀÆ÷¹¤×÷ÈÎÎñµÄÖ÷Òª²¿·Ö£¬ÎÒÃǽ«»áÔÚרҵµÄä¯ÀÀÆ÷Êг¡¿´µ½ËüµÄ³É³¤(ÓëäÈ˶ÔÓ¦µÄÌý¾õä¯ÀÀÆ÷, ÍøÂç´òÓ¡»ú, ÊÖ¶¯Çý¶¯µÈµÈ)¡£
The XML File and the XSL File
XMLÎļþºÍXSLÎļþ
Look at the XML document that you have seen in the previous chapters:
À´¿´Ò»ÏÂÒѾÔÚǰһÕ¿´¹ýµÄXMLÎļþ¡£
<?xml version="1.0" encoding="ISO-8859-1"?> |
And the accompanying XSL style sheet:
¸½ËæµÄXSLÑùʽ±í£º
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:template match="/"> </xsl:stylesheet> |
View the XSL file.
²é¿´XSLÎļþ.
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 in the Browser
ÔÚä¯ÀÀÆ÷ÖаÑXMLÎļþת»»³ÉXHTML
Here is the source code needed to transform the XML file to XHTML on the client:
Õâ¶ùµÄÔ´´úÂëÐèÒªÔÚ¿Í»§¶Ë°ÑXMLÎļþת»»³ÉXHTML¡£
<html> <script type="text/javascript"> // Load XML // Load XSL // Transform </script> </body> |
Tip: If you don't know how to write JavaScript, you can study our JavaScript tutorial.
Ìáʾ: Èç¹ûÄã²»ÖªµÀÔõôдJavaScript£¬Äã¿ÉÒÔ¿´Ò»ÏÂÎÒÃǵÄJavaScript ½Ì³Ì.
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 displays the result as XHTML in your browser. Nice!
µÚÒ»¿é´úÂë´´½¨ÁËÒ»¸ö΢ÈíµÄXMLÎļþ½âÎöÆ÷£¨XMLDOM£©µÄʵÀý£¬²¢°ÑXMLÎļþ¼ÓÔØµ½´æ´¢Æ÷ÖС£µÚ¶þ¿é´úÂë´´½¨ÁËÁíÍâÒ»¸ö½âÎöÆ÷µÄʵÀý£¬²¢ÇÒ°ÑXMLÎļþ¼ÓÔØµ½´æ´¢Æ÷ÖС£×îºóÒ»ÐеĴúÂëͨ¹ýʹÓÃXSLÎļþÀ´×ª»»XMLÎļþ£¬²¢ÔÚä¯ÀÀÆ÷ÖÐÒÔXHTMLµÄÐÎʽÏÔʾ½á¹û¡£