您的位置:首页 > 其它

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

2013-02-28 18:03 344 查看
属性值模版

在一对花括号({})中使用任意的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>中包含了换行字符,那么在输出文档中换行符会被转化为 。

命名属性集

多个元素可能需要用到一些相同的属性,例如:尺寸相同的照片,需要将相同的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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: