2008 2008年 8月 13
如果你的要求是分裂一個XML節點的值,包含分隔的字符串值,將個別項目,那麼你已經達到了正確的地方......看看下面的例子。 如果你熟悉XML和XSL的一點點......我不認為你需要任何解釋。
另外,這個例子包括使用XSL:XSL函數一樣調用模板,XSL:子串以前,XSL:子串後,如果是你是什麼。
XML進行轉換(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>
<keywords>Fruits, Vegetables, Pulses, Meat, Cereals </keywords>
</food>
XSL(food.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="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>XSL 1.0 Delimited String Tokeniser</title>
</head>
<body>
<xsl:value-of select="food/meta"/>
<div >
<xsl:call-template name="tokenize">
<xsl:with-param name="string" select="food/keywords" />
<xsl:with-param name="delimitr" select="','" />
</xsl:call-template>
</div>
</body>
</html>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="string" />
<xsl:param name="delimitr" />
<xsl:choose>
<xsl:when test="contains($string, $delimitr)">
<div style="border:1px solid red;">
<h3><xsl:value-of select="substring-before($string,$delimitr)" /></h3>
<xsl:variable name="data" select="substring-before($string,$delimitr)"/>
</div>
<xsl:call-template name="tokenize">
<xsl:with-param name="string" select="substring-after($string, $delimitr)" /><xsl:with-param name="delimitr" select="$delimitr" /></xsl:call-template>
</xsl:when>
<xsl:otherwise>
<div style="border:1px solid red;">
<h3><xsl:value-of select="$string" /></h3>
</div>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
結果輸出的HTML: -
<div>
<div style="border: 1px solid red;">
<h3>Fruits</h3>
</div>
<div style="border: 1px solid red;">
<h3> Vegetables</h3>
</div>
<div style="border: 1px solid red;">
<h3> Pulses</h3>
</div>
<div style="border: 1px solid red;">
<h3> Meat</h3>
</div>
<div style="border: 1px solid red;">
<h3> Cereals </h3>
</div>
</div>
不用說了......只是改變參數“delimitr”,您選擇的分隔符
沒有評論 |標籤: 教程 , XSLT |張貼在XSL中
2008 2008年 8月 4
我有這個strage問題,同時創造一個XSL的今天。 看著整個淨的答案,但couldnt找到任何.... 其實我猜! DINT我知道我的問題是什麼,所以我看的呢?
無論如何! 以防萬一你有這同樣的問題也莫名其妙的力量,你成功地降落在此頁的權力......你會感謝我萬噸......如果你不小心降落此頁面上的一些奇怪的命運和你剛好是一個XSL開發, 會盡此注意現在,未來的事業,如果有一天,你面對這個問題,那麼該部隊可能不會與你同在。
問題陳述:
我有這樣的XML,其中有兩個節點與多個項目中,這兩個節點之間的項目有一一對應。 在下面的例子中,每個在節點<array項目name="PLAYURL" />相關(相應的位置)節點<array的一個項目name="SITENAME"
<?xml version="1.0" encoding="utf-8"?>
<myplaylists>
<playlist>
<title>Best of Rest </title>
<array name="SITENAME">
<str>www.musicindiaonline.com</str>
<str>www.dhingana.com</str>
<str>www.raaga.com</str>
<str>www.smashits.com</str>
<str>www.desimusic.com</str>
<str>www.musicplug.in</str>
</array>
<array name="PLAYURL">
<str>http://www.musicindiaonline.com/123/</str>
<str>http://www.dhingana.com/play/123</str>
<str>http://www.raaga.com/123</str>
<str>http://ww.smashits.com/123</str>
<str>http://www.desimusic.com/123</str>
<str>http://www.musicplug.in/123</str>
</array>
</playlist>
</myplaylists>
現在,我有一個循環,我通過<array name="SITENAME">循環,我想以獲得在<array name="PLAYURL">相關項目(在相應的位置),
你會normaly嘗試做的是...
<xsl:for-each select="myplaylists/playlist/array[@name='SITENAME']/str">
play from : <a href="{../../array[@name='PLAY']/str[position()]}"/> <xsl:value-of select="." /></a>
</xsl:for-each>
對不起!! 這不會工作......啊!! Surpised ...
現在,你可能會想嘗試一下,像這樣...
<xsl:for-each select="myplaylists/playlist/array[@name='SITENAME']/str">
<xsl:variable name="pos"> <xsl:value-of select="position()"/></xsl:variable>
play from : <a href="{../../array[@name='PLAY']/str[$pos]}"/> <xsl:value-of select="." /></a>
</xsl:for-each>
並再次慣常的工作......現在,你抓你的頭,試圖聯繫大家,你認為誰知道一些的XSL,可能是有幫助......你盡一切其他的選項,它仍然不會工作嘛! 那是什麼發生在我ATLEAST ...
回复DONT絕望!! 試試這個
<xsl:for-each select="myplaylists/playlist/array[@name='SITENAME']/str">
<xsl:variable name="pos-int" select="position()" />
play from : <a href="{../../array[@name='PLAY']/str[$ <xsl:for-each select="myplaylists/playlist/array[@name='SITENAME']/str">
<xsl:variable name="pos-int" select="position()" />
play from : <a href="{../../array[@name='PLAY']/str[$ pos-int ]}"/> <xsl:value-of select="." /></a>
</xsl:for-each>
]}"/> <xsl:value-of select="." /></a>
</xsl:for-each>
不要問我為什麼工作。 因為我沒有權利的原因......如果你這樣做! 請不要刪除評論。 這可能是非常有用的。 還可以! 如果你有更好的這篇文章的標題,請給我一條線 
沒有評論 |標籤: XSL , XSL怪癖 , XSLT |張貼在XSL中
2008 2008年 5月 4 日
如果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下載)
沒有評論 |標籤: 教程 , XML , XSL , XSL包括 , XSLT |張貼在XSL中
2008 2008年 4月 4日
如果您使用XML和XSL,那麼你有可能遇到的時間時,你必須發揮你的XSL XML節點的屬性和值。 它們負載對此嗦信息的網站,但我發現沒有一個簡短和精確的......這是沒有XML / XSL的教程,但我試圖把一些金手指列表的排序...
看起來像這樣的示例XML,我們將與...
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="food.xsl"?>
<food>
<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>
</food>
所以讓我們開始改變我們的上面的XML使用XSL
例1:顯示在選定的屬性值
<xsl:value-of select="/food/fruits/item[@name='orange']" /><br />
to get more information about <a href="{/food/fruits/item[@name='orange']/@moreinfo}" target="new" ><xsl:value-of select="/food/fruits/item[@name='orange']/@name" /> </a>
HTML結果會看起來像
Orange is the color and rich in vitamin C,
to get more information about <a href="http://www.orange.com" target="new">
例2:遍歷並顯示所有XML屬性名稱和值
<xsl:for-each select="/food/fruits/item/@*">
attribute name : <xsl:value-of select="name()"/>,
attribute value : <xsl:value-of select="."/> <br />
</xsl:for-each>
HTML結果會看起來像
attribute name : name, attribute value : mango
attribute name : moreinfo, attribute value : http://www.mango.com
attribute name : name, attribute value : banana
attribute name : moreinfo, attribute value : http://www.banana.com
attribute name : name, attribute value : orange
attribute name : moreinfo, attribute value : http://www.orange.com
attribute name : name, attribute value : Papaya
attribute name : moreinfo, attribute value : http://www.papaya.com
下載上述所有文件(880下載)
觀看這個空間,我會不斷更新這一新的發現......
沒有評論 |標籤: 教程 , XML , XML屬性 , XSL , XSLT |張貼在XSL中