XSLT 语言:
XSL/XSLT是一种xml语言转换。XSL(eXtensible Style sheet Language)是XML的样式表语言。
它包含三个部分:转换XML文档以及定义输出XML部分以及格式化XML文档;
包含了三种语言:XSLT、XPath和XSL格式对象。其中XSLT最为重要,它用于将XML文档转换成另一个XML文档或另一种类型的文档。也就是能将一个XML文档转换成浏览器能识别的一种格式。
XSL转换过程:XSLT用XPath来获取源XML文档中一部分需要的数据,经过XSL语言转换后或是直接将数据输出。
支持XSL的浏览器:用XSL来处理一个XML文档,需要一个带有XSL引擎的XML解析器。目前IE5或以上版本是符合这一条件的唯一浏览器。
XML范例:
<Book>
<CateGory_B1>文学</CateGory_B1>
<CateGory_B2>社会科学</CateGory_B2>
<CateGory_B3>工业技术</CateGory_B3>
<CateGory_B4>综合性图书</CateGory_B4>
</Book>
元素:
1.
创建模板,匹配元素"CateGory_B2"的值
<xsl:template
match="/Root/CateGory_B2"></xsl:template>
2.
调用命名模板传参,call-template的使用前提是必须有一个命名的模板,required设置参数为必须被使用。
<xsl:template
name="group2">
<xsl:param name="title"
required="yes">
...
</xsl:template>
<xsl:call-template name="group2">
<xsl:with-param name="title"
select="/Root/CateGory_B4"/>...
</xsl:call-template>
3. 创建一个元素到输出文档
<xsl:element name="CateGory_B5">...</element>
4.
创建一个值为"CodeValue"的Code属性
<xsl:attribute name="Code">CodeValue</xsl:attribute>
5.
创建一个属性集合
<xsl:attribute-set name="AttributeCollection">
<xsl:attribute name="Code1">Red</xsl:attribute>
<xsl:attribute name="Code2">Bule</xsl:attribute>
<xsl:attribute name="Code3">Yellow</xsl:attribute>
</xsl:attribute-set>
6. 定义一个值为"文学"的变量,变量的使用
<xsl:variable name="variable1" select="Root/CateGory_B1"/></xsl:variable>
7. 条件选择给变量variable2赋值。otherwise相当于C#switch中的default。
<xsl:variable name="variable2">
<xsl:choose>
<xsl:when test="@variable1= "'文学'">
<xsl:value-of select="'文学作品'"/>
<xsl:when>
<xsl:when test="@variable1= "'工业技术'">
<xsl:value-of select="'文学作品'"/>
<xsl:when>
<xsl:otherwise>
<xsl:value-of select="'经济科学'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
8. 指定节点集中循环。找出节点下所有名为"CateGory_B3"的元素值
<xsl:for-each select="/Root/">
<xsl:value-of select="CateGory_B3"/>
</xsl:for-each>
特殊字符的实体编码:
<
<
>
>
‘
'
“
"
&
&