xml文档:yufa.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="yufa.xsl"?>
<phone>person通讯录
<person>编号001
<name>刘丽</name>
<gudingphone>11111</gudingphone>
<telephone>111111</telephone>
</person>
<person>编号002
<name>张慧</name>
<gudingphone>4444444</gudingphone>
<telephone>4444444</telephone>
</person>
<person>编号003
<name>孙永</name>
<gudingphone>000000</gudingphone>
<telephone>000000</telephone>
</person>
<person>
<name></name>
<gudingphone></gudingphone>
<telephone></telephone>
</person>
</phone>
xsl的基本语法:
yufa.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<!--xsl基本语法:<xsl:value-of select="元素标记">提取某个选定节点的值,将源文档中元素的文本值写到输出文档中。
select选择节点元素的路径。如:
-->
<h2><xsl:value-of select="phone/person"></xsl:value-of></h2>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
结果为:
编号001 刘丽 11111 111111
yufa.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="phone/person">
<!-- 使用<xsl:value-of>元素时,只能显示所选择的数据,并且只能显示一行,如果想要使用<xsl:value-of>元素来循环显示所有的XML元素,并显示所有的记录,就需要使用<xsl:for-each>来循环处理被选择的节点。
--> 姓名 : <xsl:value-of select="name"></xsl:value-of><br />
固定电话: <xsl:value-of select="gudingphone"></xsl:value-of> <br />
联系电话: <xsl:value-of select="telephone"></xsl:value-of> <br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
结果为:
姓名 : 刘丽
固定电话: 11111
联系电话: 111111
姓名 : 张慧
固定电话: 4444444
联系电话: 4444444
姓名 : 孙永
固定电话: 000000
联系电话: 000000
转载于:https://blog.51cto.com/1085616858/847709