Example study: How to transform XML into XHTML using XSLT.
ʵÀýѧϰ£ºÈçºÎͨ¹ýXSLT°ÑXMLת»»³ÉXHTML¡£
The details of this example will be explained in the next chapter.
´ËʵÀýµÄÏêϸÇé¿ö½«ÔÚÏÂÒ»ÕÂÖÐ×÷Ïêϸ˵Ã÷¡£
Correct Style Sheet Declaration
ÕýÈ·µÄÑùʽ±íÉùÃ÷
The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>.
½«ÎļþÒÔXSLÑùʽ±í½øÐÐÉùÃ÷µÄ¸ùÔªËØ£¨root element£©ÊÇ<xsl:stylesheet> »ò<xsl:transform>¡£
Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used!
×¢Òâ: <xsl:stylesheet> ºÍ<xsl:transform>ÊÇÍêȫͬÒåµÄ£¬ÈκÎÒ»¸ö¶¼Äܱ»Ê¹Óá£
The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is:
ÉùÃ÷¸ù¾ÝW3C XSLT²Î¿¼±ê×¼µÄXSLÑùʽ±íµÄÕýÈ·µÄ;¾¶ÊÇ£º
<xsl:stylesheet version="1.0" |
or:
»ò:
<xsl:transform version="1.0" |
To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document.
ÏëÒªÓÐȨʹÓÃXSLTÔªËØ£¬ÎÒÃDZØÐëÔÚÎļþµÄ¶¥¶ËÊÂÏÈÉùÃ÷XSLTÃüÃû¿Õ¼äµÄÊôÐÔºÍÌØÕ÷¡£
The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version="1.0".
xmlns:xsl="http://www.w3.org/1999/XSL/TransformÖ¸³öÁ˹ٷ½W3C XSLTµÄÃüÃû¿Õ¼ä£¨namespace£©¡£Èç¹ûÄãʹÓÃÁËÕâ¸öÃüÃû¿Õ¼ä£¬ÄãÒ²±ØÐë×¢Ã÷ÊôÐÔ°æ±¾£¨version£©=1.0¡£
Start with a Raw XML Document
ÒÔXMLÔ´Îļþ¿ªÊ¼
We want to transform the following XML document ("cdcatalog.xml") into XHTML:
ÈÃÎÒÃǰÑÏÂÃæµÄXMLÎļþ("cdcatalog.xml")ת»»³ÉXHTML°É¡£
<?xml version="1.0" encoding="ISO-8859-1"?> |
Viewing XML Files in Firefox and Internet Explorer: Open the XML file (typically by clicking on a link) - The XML document will be displayed with color-coded root and child elements. A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure. To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source" from the browser menu.
ÔÚ»ðºüºÍIEä¯ÀÀÆ÷Öв鿴XMLÎļþµÄ·½·¨£º ´ò¿ªXMLÎļþ£¨Í¨¹ýÁ´½Ó£©-XMLÎļþ½«»áÏÔʾº¬ÓÐÑÕÉ«´úÂëµÄ¸ùÔªËØºÍ×ÓÔªËØ¡£µ¥»÷×ó±ßÔªËØµÄ“+”ºÍ“-”£¬Õ¹¿ª»òÕßËõ»ØÔªËØÁÐ±í¡£²é¿´XMLÔ´ÎļþµÄÔ´´úÂ루ûÓмӺźͼõºÅ£©£¬ÔÚä¯ÀÀÆ÷²Ëµ¥ÀïÑ¡Ôñ“²é¿´Ò³ÃæÔ´´úÂ루View Page Source£©”»òÕß“²é¿´Ô´´úÂ루View Source£©”¡£
Viewing XML Files in Netscape 6: Open the XML file, then right-click in XML file and select "View Page Source". The XML document will then be displayed with color-coded root and child elements.
ÔÚNetscape 6Öв鿴XMLÎļþµÄ·½·¨£º´ò¿ªXMLÎļþ£¬ÔÚXMLÎļþÀïÓÒ¼üµ¥»÷Ñ¡Ôñ“²é¿´Ò³ÃæÔ´´úÂ루View Page Source£©”¡£XMLÎļþ½«»áͨ¹ý±êÓÐÑÕÉ«´úÂëµÄ¸ùÔªËØºÍ×ÓÔªËØÀ´ÏÔʾ¡£
Viewing XML Files in Opera 7: Open the XML file, then right-click in XML file and select "Frame" / "View Source". The XML document will be displayed as plain text.
ÔÚOpera 7Öв鿴XMLÎļþµÄ·½·¨£º ´ò¿ªXMLÎļþ£¬ÔÚXMLÎļþÀïÓÒ¼üµ¥»÷Ñ¡Ôñ“¿ò¼Ü/²é¿´Ô´´úÂ루"Frame" / "View Source"£©”¡£XMLÎļþ½«»áͨ¹ý´¿Îı¾ÏÔʾ¡£
View "cdcatalog.xml"
²é¿´ "cdcatalog.xml"
Create an XSL Style Sheet
´´½¨Ò»¸öXSLÑùʽ±í
Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template:
Äãͨ¹ýÒ»¸öת»»Ä£°å´´½¨ÁËÒ»¸öXSLÑùʽ±í£¨"cdcatalog.xsl"£©¡£
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" <xsl:template match="/"> </xsl:stylesheet> |
View "cdcatalog.xsl"
²é¿´"cdcatalog.xsl"
Link the XSL Style Sheet to the XML Document
½«XSLÑùʽ±íÁ¬½Óµ½XMLÎĵµÖÐ
Add the XSL style sheet reference to your XML document ("cdcatalog.xml"):
°ÑXSLÑùʽ±í²ÎÊýÌí¼Óµ½XMLÎļþ"cdcatalog.xml")£º
<?xml version="1.0" encoding="ISO-8859-1"?> |
If you have an XSLT compliant browser it will nicely transform your XML into XHTML.
Èç¹ûÄãÓÐÁËÒ»¸öºÏÊʵÄä¯ÀÀÆ÷£¬Ëü½«»á׼ȷµØ°ïÄã°ÑXMLÎļþת»»³ÉXHTML¡£
The details of the example above will be explained in the next chapters.
ÒÔÉÏʵÀýµÄϸ½Ú²¿·Ö½«»áÔÚÏÂÒ»ÕÂÖÐ×÷Ïêϸ˵Ã÷¡£