XSLT 元素
输出节点值用 value-of,输出属性用 attribute,生成元素用 element,还有 text 输出纯文本。这是 XSLT 输出的基本手段。
输出方式
- <xsl:value-of select="XPath"/>:输出文本值
- <xsl:attribute name="id">...</xsl:attribute>:动态属性
- <xsl:element name="div">...</xsl:element>:动态元素
- <xsl:text>:输出纯文本(保留空白)
属性值模板
属性里用 {XPath} 直接插值:<a href="{link}">。
代码示例
<xsl:template match="book">
<!-- 属性值模板 -->
<div class="book-item" data-id="{@id}">
<!-- value-of 输出 -->
<h3><xsl:value-of select="title"/></h3>
<!-- 动态属性 -->
<a>
<xsl:attribute name="href">
/book/<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:text>查看详情</xsl:text>
</a>
<!-- 条件输出价格 -->
<xsl:if test="price">
<span>¥<xsl:value-of select="price"/></span>
</xsl:if>
</div>
</xsl:template>