XML学习之XSLT:四、创建结果树

         属性值模版

        在一对花括号({})中使用任意的XPath表达式,作为元素的属性值,这将被XSLT处理器解释为属性值模板。在转换时,表达式计算的结果将别转为字符串。

<photo>
	<uri>image/banner.gif</uri>
	<width>500</width>
	<height>60</height>
</photo>

源文件数据

<xsl:template match="photo">
	<img src="{uri}" width="{width}" height="{height}"/>
</xsl:template>

xsl文件数据

<img src="image/banner.gif" width="500" height="60" />

最终显示格式

        注意:属性模版不能嵌套,也就是只能有一对花括号。以下方式是错误的。

<a href="#{id({@ref})/title}">错误格式</a>

错误格式

         创建属性

         属性将会直接输出到结果文档中,如需要根据源文件的内容,动态设置元素的属性,可可以使用XSLT的<xsl:attribute>元素来创建属性。语法如下,name是必需的。

<xsl:attribute name="name" namespace="namespace">
	<!--Content:template-->
</xsl:attribute>

        示例如下:

<xsl:template match="employee">
	<a>
		<xsl:attribute name="href">empinfo/<xsl:value-of select="name"/>.html</xsl:attribute>
		<xsl:value-of select="name"/>
	</a>
</xsl:template>

xsl文件

<a href="empinfo/zhangsan.html">zhangsan</a>

最终显示文件

        注意:如果在<xsl:attribute>中包含了换行字符,那么在输出文档中换行符会被转化为&#10;。

        命名属性集

       多个元素可能需要用到一些相同的属性,例如:尺寸相同的照片,需要将相同的width和height应用到img上。可以将多个属性作为属性集合中的成员,然后使用<xsl:sttribute-set>包含到元素中。语法如下。该元素必须作为I顶层元素使用,属性name是必需的,指定属性集的名字。

<xsl:attribute-set name="name" use-attribute-sets="qnames">
	<!--Content:-->
</xsl:attribute-set>

       示例如下:

<xsl:attribute-set name="columnstyle">
	<xsl:attribute name="align">center</xsl:attribute>
	<xsl:attribute name="valign">middle</xsl:attribute>
	<xsl:attribute name="bgcolor">#9999cc</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="employee">
	<tr>
		<td xsl:use-attribute-sets="columnstyle"><xsl:value-of select="name" /></td>
		<td xsl:use-attribute-sets="columnstyle"><xsl:value-of select="age" /></td>
		<td xsl:use-attribute-sets="columnstyle"><xsl:value-of select="month_pay" /></td>	
	</tr>
</xsl:template>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值