|
XSLT 语法详解: xsl:attribute
|
|
| |
|
来源:XSLT.org.cn 作者:XSLT专业站 发布时间:2007-10-27
|
|
XSLT 语法详解: xsl:attribute
<xsl:attribute>提供在输出文档中创建元素属性的功能,与直接在文档中设置元素属性不同,<xsl:attribute>的好处是可以根据输入的文档为创建的属性赋值。
必需属性: name: 该属性定义通过<xsl:attribute>元素创建的属性的名字。
可选属性: namespace:该属性定义创建出来的属性的nameplace。
可以通过<xsl:choose>, <xsl:text>, 和 <xsl:value-of> 元素为<xsl:attribute>元素创建的属性赋值。
例如:如果我们想根据下面的XML文档生成一个HTML表格。
<?xml version="1.0"?> <list xml:lang="en"> <title>Albums I've bought recently:</title> <listitem>The Sacred Art of Dub</listitem> <listitem>Only the Poor Man Feel It</listitem> <listitem>Excitable Boy</listitem> <listitem xml:lang="sw">Aki Special</listitem> <listitem xml:lang="en-gb">Combat Rock</listitem> <listitem xml:lang="zu">Talking Timbuktu</listitem> <listitem xml:lang="jz">The Birth of the Cool</listitem> </list> |
表格中左边一列只有一行(多行合并),因此需要知道右边一列的行数,但是我们不能对行数进行硬编码,也就是说必须是通过计算得到rowspan 的值,此时我们可以使用<xsl:attribute>通过下面的XSLT代码来实现:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/"> <html> <head> <title><xsl:value-of select="list/title"/></title> </head> <body> <xsl:apply-templates select="list"/> </body> </html> </xsl:template>
<xsl:template match="list"> <table border="1" width="75%"> <tr> <td bgcolor="lightslategray" width="100" align="right"> <xsl:attribute name="rowspan"> <xsl:value-of select="count(listitem)"/> </xsl:attribute> <p style="font-size: 125%"> <xsl:value-of select="title"/> </p> </td> <td> <xsl:value-of select="listitem[1]"/> </td> </tr> <xsl:for-each select="listitem"> <xsl:if test="position() > 1"> <tr> <td> <xsl:value-of select="."/> </td> </tr> </xsl:if> </xsl:for-each> </table> </xsl:template>
</xsl:stylesheet> |
生成的结果如下图:

标题: XSLT 语法详解: xsl:attribute
关键字:XSLT 语法详解 xsl:attribute 属性
|
| 上一篇:XSLT 语法详解: xsl:apply-templates 下一篇:XSLT 语法详解: xsl:attribute-set |
|
|
|
|
|