Jos XML / XSL muunnokset on sinun domian, niin on hetkiä, jolloin haluamme peice Dynamic koodia käytetään kirjastokohdetta (tehtävä uudelleen käytettävissä). Mitä tarkoitan, ehkä voitaisiin tehdä selväksi tämän esimerkin tilanteessa.
Kuvittele luot sivuston (ja XML, XSL transfroms tietysti) ja useimmat sivuilla olisi Kommentit Module. No! sitten joko kopioit tai liitä tämä koodi jokaiselle sivulle malliin (joka minulla ei ole sanottavaa, mutta elatusapua ja työstää painajainen) tai jopa parempi, luot include-tiedostoon, joka voidaan vetää sisään minne haluat sen sivun ( s) ...
Joten Miten luoda XSL ovat tiedostojen ja sisällyttää se sisällä toista XSL? Tässä on, miten ...
Vain tehdä asiat selväksi ... tässä on nopea lista tiedostoista syntyisi ... täällä, tulemme myös tietoa hedelmien ja vihannesten pääsivun nimeltään ruoka.
1. food.xml - xml tiedosto, johon muutosta haetaan
2. food.xsl - tärkeimmät XSL-tiedoston, joka muuttaa meidän food.xml
3. inc_fruits.xsl - XSL sisältää tiedoston, joka tekevät hedelmiä tietoa
4. inc_vegtables.xsl - XSL sisältää tiedoston, joka tekevät vetetables tietoa
I dont ajatella minulla on selittää paljon, koodit edellä mainitut seikat, on itsestään selvä ...
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: template>
</ Xsl: stylesheet>
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>
Lataa kaikki edellä tiedostot täältä (251 lataukset)