XSLTÇáËɽ«XMLÊý¾Ýת»»³ÉHTML
ʱ¼ä:2008-12-03 19:33À´Ô´:ÍøÂçËѼ¯ ×÷Õß: µã»÷:
´Î
XSLTÇáËɽ«XMLÊý¾Ýת»»³ÉHTML ,ʹÓÃÒ»¸ö¼òµ¥µÄ XSL Ñùʽ±í¾Í¿ÉÒÔ½« XML Êý¾Ýת»»³É HTML¡£Õâ¸öÀý×ÓΪÔÚ²¿Êð Web Êý¾Ý½â¾ö·½°¸ÖÐÓõ½µÄ File-Folder-Field ÊÓͼ´´½¨ÁËÒ»¸ö»ù´¡¡£
¡¾Õª Òª¡¿
ʹÓÃÒ»¸ö¼òµ¥µÄ XSL Ñùʽ±í¾Í¿ÉÒÔ½« XML Êý¾Ýת»»³É HTML¡£Ëæ×Å XML ¹æ·¶µÄ²»¶ÏÑݽø£¬ÔÚеİ汾ÖÐÂú×ãÿ¸öÈ˵ÄÐèÒªËƺõÒѾ³ÉΪ±ØÒª£»²»ÐÒµÄÊÇ£¬½øÐмòµ¥µÄת»»Ò»Ö±¶¼À§ÈÅ׏淶¡£
¼ÙÉèÎÒÓÐÒ»¸ö±íʾһ¸öÒ³ÃæÄÚÈÝµÄ XML¡¡Êý¾Ý£¬ÏÖÔÚÎÒÏ뽫ÆäÄÚÈÝת»»³É²¼¾Ö¡£ÏÂÃæÊÇÎÒÏëҪת»»µÄ XML£º
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="article.xsl"?>
<xml>
<folders>
<folder>
<text>Folder 1</text>
<files>
<file>
<text>File 1</text>
<fields>
<field>
<data>
<type>string</type>
<length>50</length>
<value>some data</value>
</data>
</field>
</fields>
</file>
</files>
</folder>
</folders>
</xml>
|
Õâ¸öÄÚÈݱíʾһ×éÎļþ¼Ð¡¢ÎļþºÍÓò¡£Ã¿¸öÎļþ¼Ð°üº¬Îļþ£¬Ã¿¸öÎļþ°üº¬ÓÃÓÚÊäÈëÊý¾ÝµÄÓò¡£Îļþ¼Ð×éÖеÄÿ¸öÎļþ¼Ð¶¼½«±íÏÖΪһ¸ö TABLE µÄµÚÒ»ÐеÄÒ»¸ö TR ÔªËغÍÒ»¸ö TD ÔªËØ¡£Îļþ×éÖеÄÿ¸öÎļþ¶¼½«±íʾΪǶÌ×ÔÚÎļþ¼Ð TR ÔªËØÖеÄÒ»¸ö TABLE ÔªËصĵÚÒ»ÐеÄÒ»¸ö TR ÔªËغÍÒ»¸ö TD ÔªËØ¡£Óò×éÖеÄÿ¸öÓò¶¼½«ÔÚÏà¹ØµÄÎļþÖбíÏÖΪһ¸ö INPUT¡£
ΪÁËʵÏÖÕâÒ»Ïë·¨£¬ÎÒÃÇÐèÒª½«±éÀú XML È»ºó¸ù¾ÝXSL ¹¹½¨Ò»¸ö±í¡£
ÏÂÃæÊÇÓÃÓÚÕâ¸öת»»µÄ XSL£º
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:fn="http://www.mycompany.com/mynamespace">
<xsl:output method="html"/>
<msxsl:script language="JScript" implements-prefix="fn">
function getElementCount(nodelist, what) {
var rtrn = 0;
rtrn = nodelist[0].parentNode.selectNodes(what).length;
return (rtrn + 1); //1 is added for filler TD
}
</msxsl:script>
<xsl:template match="/">
<TABLE CELLSPACING="0" CELLPADDING="0"
WIDTH="100%" BORDER="0" ID="tblRoot" NAME="tblRoot"
style="table-layout:fixed;">
<TR>
<xsl:for-each select="xml/folders/folder">
<xsl:element name="TD">
<xsl:attribute name="style">width:55px</xsl:attribute>
<xsl:value-of select="text"/>
</xsl:element>
</xsl:for-each>
<TD> </TD>
</TR>
<xsl:for-each select="xml/folders/folder">
<TR>
<xsl:element name="TD">
<xsl:attribute name="colspan">
<xsl:value-of select="fn:getElementCount(., 'folder')"/>
</xsl:attribute>
<TABLE CELLSPACING="0" CELLPADDING="0"
WIDTH="100%" BORDER="0" style="table-layout:fixed;">
<TR>
<xsl:for-each select="files/file">
<xsl:element name="TD">
<xsl:attribute name="style">width:55px;</xsl:attribute>
<xsl:value-of select="text"/>
</xsl:element>
</xsl:for-each>
<TD> </TD>
</TR>
<xsl:for-each select="files/file">
<TR>
<xsl:element name="TD">
<xsl:attribute name="colspan">
<xsl:value-of select="fn:getElementCount(., 'file')"/>
</xsl:attribute>
<xsl:for-each select="fields/field">
<xsl:element name="INPUT">
<xsl:attribute name="type">text</xsl:attribute>
<xsl:attribute name="maxlength">
<xsl:value-of select="data/length"/>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="data/value"/>
</xsl:attribute>
</xsl:element><BR/>
</xsl:for-each>
</xsl:element>
</TR>
</xsl:for-each>
</TABLE>
</xsl:element>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
|
ÔÚ stylesheet ±ê¼ÇÖУ¬ÎÒÃÇÉèÖÃÁ˼¸¸öÃüÃû¿Õ¼ä£¬°üÀ¨¶¨ÒåËùÓÐ xsl ת»»±ê¼ÇµÄ xsl ÃüÃû¿Õ¼ä¡£Äܹ»ÈÃÎÒÃÇ´´½¨¿ÉÒÔÔÚÑùʽ±íÖÐʹÓõÄÓû§º¯ÊýµÄmsxml ÃüÃû¿Õ¼ä¡£ÎÒʹÓÃËüµÃµ½ËùÓеÄ×ÓÔªËØ£¬ÒÔ±ãµÃµ½Ò»¸ö TD ±ê¼ÇµÄÒ»¸ö COLSPAN ÊôÐÔ¼¯¡£ÓÃÓÚ¼ÓÈëÒ»×éÓû§¶¨ÒåµÄº¯ÊýµÄfn ÃüÃû¿Õ¼ä£¬¸ÃÃüÃû¿Õ¼äÓÉmsxml:script element ´´½¨¡£
È»ºó£¬ÎÒÃÇ´´½¨Íⲿ TABLE ºÍµÚÒ»¸ö TR¡£ÔÚ TR ÖУ¬ÎÒΪ XML ÖÐÖ¸¶¨µÄÿ¸öÎļþ¼Ð´´½¨Ò»¸ö TD¡£ÎÒʹÓÃÁË xsl:element ±ê¼Ç£¬ÒòΪËüÔÊÐíÎÒÌí¼Ó×Ô¶¨ÒåÊôÐÔ»òÕßÖ´ÐÐÒ»¸öº¯ÊýÀ´ÎªÁíÒ»¸ö TD ÔªËØÖÐµÄ COLSPAN ÊôÐÔÉèÖÃÒ»¸öÊôÐÔ¡£
ÔÚΪÿ¸öÎļþ¼Ð´´½¨ÁËËùÐèµÄ TD Ö®ºó£¬ÎÒ¿ªÊ¼ÎªÃ¿¸öÎļþ¼Ð´´½¨ TR¡£ÎÒÖ»ÏòÕâ¸ö TR Ìí¼ÓÒ»¸ö TD£¬µ«ÊÇÎÒ½«ËüµÄ COLSPAN ÊôÐÔÉèΪµÈÓÚÎļþ¼Ð×éÖÐÎļþ¼Ð±ê¼ÇµÄÊýÁ¿ÔÙ¼ÓÒ»¡£¶à³öµÄÒ»ÊÇÓÃÀ´ÔÚÒ»¸ö¹Ì¶¨²¼¾ÖÑùʽµÄ TABLE ÖÐÌî³ä¿Õ¸ñÓõġ£
ΪÁ˵õ½ COLSPAN£¬ÎÒ´«È뵱ǰµÄÉÏÏÂÎÄ£¨ÔÚÕâÀïÓÉ“.”Ö¸¶¨£©ºÍÎÒÏë¼ÆËãµÄ½ÚµãµÄÃû×Ö¡£ÔÚÎҵĺ¯ÊýÖУ¬Îҵõ½µ±Ç°µÄÉÏÏÂÎÄ£¬paraentNode£¬ºÍ XPath ²éѯÖÐÖ¸¶¨µÄ½ÚµãµÄÊýÁ¿¡£È»ºóº¯Êý·µ»ØÕâ¸öÊýÁ¿¼ÓÉÏÒ»ÒÔÌî³ä TD¡£
ÓÐÁËÕâ¸ö TD£¬ÎÒÔÚÆäÖÐǶÈëÁíÍâÒ»¸ö TABLE£¬¸Ã TABLE °üº¬Îļþ×éÖеÄÿ¸öÎļþ¡£´ÓÕâÒ»µã¿ªÊ¼£¬Æä¹ý³Ì¾ÍÓëÍⲿ TABLE ת»»Ò»Ñù¡£×îºóµÄ²½ÖèÊÇÌí¼Óÿ¸öÎļþÖеÄÓò¡£Õâ´ÎÎÒûÓÐÔÙ´´½¨Ç¶ÈëµÄ TABLE£¬Ö»ÊÇ°ÑÕâЩÓòÌí¼Óµ½µ±Ç°µÄ TD ÖС£
Ò»µ©ÎÒÍê³ÉÁËͨÓò¼¾Ö£¬ÎҾͿÉÒÔ¿ªÊ¼Ìí¼ÓÓû§½çÃ湦ÄÜÁË£¬±ÈÈç˵Òþ²ØµôÆäËüµÄÎļþ¼ÐºÍÎļþÐУ¬Ö±µ½Óû§µã»÷ÁËÏà¹ØµÄÑ¡Ï¡£ÕâÒ»¹¦ÄÜ¿ÉÒÔͨ¹ý±àд֧³ÖÕâÒ»¹¦ÄܵĽű¾À´ÊµÏÖ£¬Ìí¼ÓÒ»¸ö onclick xsl:attribute ÔªËص½Îļþ¼ÐºÍÎļþ TD ÔªËØ£¬È»ºó½«ËüµÄÖµÉèÖÃΪ½Å±¾º¯ÊýµÄÃû×Ö¡£
×îºó£¬ÔÚͨÓù¦ÄÜÍê³ÉÖ®ºó£¬Äã¿ÉÒÔÌí¼Óclass xsl:attributes ²¢ÔÚ STYLE »ò CSS ÖÐÌí¼ÓÏà¹ØµÄclassNames µÃµ½ÄãÏëÒªµÄÍâ¹Û¡£
Õâ¸öÀý×ÓΪÔÚ²¿Êð Web Êý¾Ý½â¾ö·½°¸ÖÐÓõ½µÄ File-Folder-Field ÊÓͼ´´½¨ÁËÒ»¸ö»ù´¡¡£·ÃÎÊ MSDN ÕÒµ½¸ü¶àÓйØMicrosoft µÄ XML ¹æ·¶µÄÐÅÏ¢¡£
(ÔðÈα༣ºadmin) |
Ö¯ÃζþάÂëÉú³ÉÆ÷
------·Ö¸ôÏß----------------------------