2008 2008年 5月 4

包括XSL內的XSL

如果XML / XSL轉換是你的結構域,然後還有時候,我們需要一個動態代碼peice要使用庫項目(可重複使用的要作出)。 我的意思,可能與這個示例場景中更明確。

想像你正在創建一個網站(使用的XML,XSL transfroms當然)和大多數的網頁將有一個評論模塊 好吧! 然後,複製或粘貼此代碼到每一頁的模板(我不說,但維修和返工一場噩夢),甚至更好,你創建包含文件可以在任何你想它在您的網頁拉( S)...
那麼,如何創造一個XSL INCLUDE文件,它包括在另一個XSL文件 這裡是如何...

只是把事情說清楚......這裡是快速的文件列表,您將創建......在這裡,我們將納入食品父頁左右的水果和蔬菜的信息。

1。 food.xml - XML數據文件的轉換將應用於
2。 food.xsl - 主要XSL文件,這將改變我們的food.xml
3。 inc_fruits.xsl - 的XSL包括會使水果的數據文件
4。 inc_vegtables.xsl - XSL的文件,將呈現vetetables數據

我不認為我有解釋太多,以上元素的代碼,將自明...

FOOD.XML


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="food.xsl"?>
<food>
<date>July 2008</date>
<description>All about things we eat everyday</description>
<fruits type="tropical">
<item name="mango" moreinfo="http://www.mango.com">Mango is the king of fruits</item>
<item name="banana" moreinfo="http://www.banana.com">Banana once a day , keeps the doctor away</item>
<item name="orange" moreinfo="http://www.orange.com">Orange is the color and rich in vitamin C</item>
<item name="Papaya" moreinfo="http://www.papaya.com">Papaya - Hot when raw, cold when ripe</item>
</fruits>

<vegetables>
<item name="spinach" moreinfo="http://www.spinach.com">Spinach is full of iron</item>
<item name="asparagus" moreinfo="http://www.asparagus.com">Asparagus contains loads of vitamin D </item>
<item name="fenugreek" moreinfo="http://www.fenugreek.com">Fenugreek is rich in vitamin C</item>
</vegetables>
</food>


FOOD.XSL


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="inc_fruits.xsl" />
<xsl:include href="inc_vegetables.xsl" />
<xsl:template match="/">
<html>
<head>
<title>Title</title>
</head>
<body>
<h3><xsl:value-of select="/food/description" /></h3>
Modification Date : <xsl:value-of select="/food/date" />
<hr/>
<h5> About Fruits</h5>
<xsl:call-template name="about_fruits"/>

<hr/>
<h5> About Vegetables</h5>
<xsl:call-template name="about_vegetables"/>

<hr/>
</ BODY>
</ HTML>
</ XSL模板>
</ XSL樣式表>


INC_FRUITS.XSL

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" />
<xsl:template name="about_fruits">
<xsl:for-each select="/food/fruits/item/@*">
attribute name : <xsl:value-of select="name()"/>
attribute value : <xsl:value-of select="."/> <br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


INC_VEGETABLES.XSL

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" />
<xsl:template name="about_vegetables">
<xsl:for-each select="/food/vegetables/item/@*">
attribute name : <xsl:value-of select="name()"/>
attribute value : <xsl:value-of select="."/> <br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

下載上述所有文件(252下載)


NDK的家 | 表達它 表示唇齶裂 表示Penmenship 表示敬畏 表達自己