有如下XML代码,有如下几个问题:
<?xml version="1.0" encoding="gb2312" ?>
< person>
<student id="1234">
<name>Amy</name>
<sex>female</sex>
<age>15</age>
<lxfs>
<tel>1234567</tel>
</lxfs>
</student>
<student>
<name>Tom</name>
<sex>male</sex>
<age>23</age>
<lxfs>
<email>tom@163.com</email>
</lxfs>
</student>
<student>
<name>Sonia</name>
<sex>female</sex>
<age>25</age>
<tel>7654321</tel>
</student>
</person>
源代码如下:
a.xml
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="aa.xslt"?>
<person>
<student id="1234">
<name>Amy</name>
<sex>famale</sex>
<age>15</age>
<lxfs>
<tel>1234567</tel>
</lxfs>
</student>
<student>
<name>Tom</name>
<sex>male</sex>
<age>23</age>
<lxfs>
<email>tom@163.com</email>
</lxfs>
</student>
<student>
<name>Sonia</name>
<sex>female</sex>
<age>25</age>
<tel>7654321</tel>
</student>
</person>
aa.xslt
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<center>
<table border="1" width="300">
<tr>
<td align="center">tel</td>
</tr>
<xsl:for-each select="person/student">
<tr>
<td align="center">
<xsl:value-of select="*/*"/>
</td>
</tr>
</xsl:for-each>
</table>
</center>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
运行结果:
b.xml
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="bb.xslt"?>
<person>
<student id="1234">
<name>Amy</name>
<sex>famale</sex>
<age>15</age>
<lxfs>
<tel>1234567</tel>
</lxfs>
</student>
<student id="2">
<name>Tom</name>
<sex>male</sex>
<age>23</age>
<lxfs>
<email>tom@163.com</email>
</lxfs>
</student>
<student id="3">
<name>Sonia</name>
<sex>female</sex>
<age>25</age>
<tel>7654321</tel>
</student>
</person>
bb.xslt
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<center>
<table border="1" width="300">
<tr>
<td align="center">name</td>
<td align="center">sex</td>
<td align="center">lxfs</td>
</tr>
<xsl:for-each select="person/student[@id>'12'][name !='Tom']">
<xsl:value-of select="name"/>
<tr>
<td align="center">
<xsl:value-of select="name"/>
</td>
<td align="center">
<xsl:value-of select="sex"/>
</td>
<td align="center">
<xsl:value-of select="lxfs"/>
</td>
</tr>
</xsl:for-each>
</table>
</center>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
运行结果:
c.xml
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="cc.xslt"?>
<person>
<student id="1234">
<name>Amy</name>
<sex>famale</sex>
<age>15</age>
<lxfs>
<tel>1234567</tel>
</lxfs>
</student>
<student>
<name>Tom</name>
<sex>male</sex>
<age>23</age>
<lxfs>
<email>tom@163.com</email>
</lxfs>
</student>
<student>
<name>Sonia</name>
<sex>female</sex>
<age>25</age>
<tel>7654321</tel>
</student>
</person>
cc.xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="person/student/lxfs/tel">
<xsl:value-of select="."/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
运行结果: