最近项目遇到的 整合成了一个例子
先贴xml
代码就没必要全都贴上去了 大家明白那个意思就行
xsl代码:
在这里给大家解释下代码的意思
<xsl:attribute name="href">
<xsl:value-of select="channels/channel/link" />
</xsl:attribute>
这是给<a>标签加上href的属性
<xsl:attribute name="title">
<xsl:value-of select="channels/channel/description" />
</xsl:attribute>
<xsl:choose>
这是给<a>标签添加title的属性 鼠标移上去的时候显示所有的文章标题
<xsl:choose>
<xsl:when test="string-length(channels/channel/description) < 18">
<xsl:value-of select="channels/channel/description"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring(channels/channel/description,1,18)"/>...
</xsl:otherwise>
</xsl:choose>
这是对xml文本的判断 如果文本的长度大于18就进行截取然后加上...
前台代码:
调用:
效果如图:
参考教程:http://www.w3school.com.cn/xsl/index.asp
补充:
因为我用的是url来获取xml 而js不能跨域获取 所以后面只能改成C#来做 具体代码如下:
调用方法为: ReadXLST(eastweekly, "East_index.xsl", "http://192.168.9.22:9090/static/xml/eastday_hotnews_index_weekly.xml");