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中