1.xsl的开始语句
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet>
2.xsl选择根标签作为模板
<xsl:template match="/"></xsl:template>
3.xsl 遍历每个同名标签元素
<xsl:for-each select="tool/field">
</xsl:for-each>
4.xsl 提取xml标签中的属性内容
<!-- 提取当前节点的id属性值 -->
<xsl:value-of select="@id" />
5.xsl 提取xml标签中的元素内容
<!-- 提取value标签的元素内容 -->
<xsl:value-of select="value" />
<!-- 也可以这样子写 -->
<xsl:value-of select="value" ></xsl:value-of>
6.xsl 将提取提取的元素作为 XHTML 某元素的属性值
<input>
<xsl:attribute name="name"><xsl:value-of select="@id" /></xsl:attribute>
</input>
完整示例
[tool.xml]
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v2007 (http://www.altova.com) -->
<?xml-stylesheet type="text/xsl" href="tool.xsl" rel="external nofollow" ?>
<tool>
<field id="prodName">
<id>11351</id>
<value>HAMMER HG2606</value>
</field>
<field id="prodNo">
<id>11377</id>
<value>32456240</value>
</field>
<field id="price">
<id>11385</id>
<value>$30.00</value>
</field>
</tool>
[tool.xsl]
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v2007 (http://www.altova.com) --><xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">
<html>
<body>
<form method="post" action="edittool.asp">
<h2>Tool Information (edit):</h2>
<table border="0">
<xsl:for-each select="tool/field">
<tr>
<td>
<xsl:value-of select="@id"/>
</td>
<td>
<input type="text">
<xsl:attribute name="id"> <xsl:value-of select="@id" ></xsl:value-of> </xsl:attribute>
<xsl:attribute name="name"><xsl:value-of select="@id" /></xsl:attribute>
<xsl:attribute name="value"> <xsl:value-of select="value" /></xsl:attribute>
</input>
</td>
</tr>
</xsl:for-each>
</table>
<br />
<input type="submit" id="btn_sub" name="btn_sub" value="Submit" />
<input type="reset" id="btn_reset" name="btn_reset" value="Reset" />
</form>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
展示结果如下啊图
其他xsl语句
<!-- 通过xpath语法查找子元素id内容不为11351的info元素 -->
<xsl:for-each select="tool/field[id!='11351']"> <!-- 通过xsl筛选id 大于 11351 的info元素 -->
<xsl:if test="id < 11351" /> <!-- 按照子元素id内容进行排序-->
<xsl:sort select="id" /> <!-- 通过xsl实现多条件语句筛选-->
<xsl:choose>
<xsl:when test="id < 50">
<xsl:value-of select="id" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="id" />
</xsl:otherwise>
</xsl:choose></xsl:for-each>