XSLT之路1--基本语法
xlst
- 结构
- 连接到xml
- 模板
- 取值
- 循环
- 条件
- 选择
- 排序
- 引用模板
- 注释
- 变量
结构
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" > <xsl:output method="xml" indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
连接到xml
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
模板
<xsl:template name="show_title" match="/"> <xsl:param name="title" /> <xsl:for-each select="catalog/cd"> <xsl:value-of select="$title" /> </xsl:for-each> </xsl:template> <xsl:call-template name="show_title"> <xsl:with-param name="title" /> </xsl:call-template>
取值
<xsl:value-of select="catalog/cd/title"/>
循环
<xsl:for-each select="catalog/cd"> <xsl:value-of select="title"/> <xsl:value-of select="artist"/> </xsl:for-each>
条件
<xsl:if test="price > 10"> <xsl:value-of select="title"/> <xsl:value-of select="artist"/> </xsl:if>
选择
<xsl:choose> <xsl:when test="price > 10"> <xsl:value-of select="artist"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="artist"/> </xsl:otherwise> </xsl:choose>
排序
<xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <xsl:value-of select="title"/> <xsl:value-of select="artist"/> </xsl:for-each>
引用模板
同文件内
<xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="cd"> <xsl:apply-templates select="title"/> </xsl:template> <xsl:template match="title"> <xsl:value-of select="."/> </xsl:template>
不同文件内导入
<xsl:import href="standard.xsl"/> <xsl:template match="message"> <xsl:apply-imports/> </xsl:template>
不同文件包含(必须是stylesheet的子节点)
<xsl:include href="/xsl/xslincludefile.xsl" />
注释
<xsl:comment>注释</xsl:comment>
变量
<xsl:variable name="color" select="'red'" /> <xsl:comment>引用</xsl:comment> <xsl:copy-of select="$header" />
作者:Roader
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.