我想输出一个版本为20i的数据库的名称、域和使用情况。我已经创建了一个样式表来完成这项工作—当我加载xml时,它检测到表中有一个符合我的需求的元素,但实际上并不显示它们。不知道我做错了什么!
XML: <?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="xrt.xsl"?>
<Inventory>
<DatabaseName>
<GlobalName>Tom</GlobalName>
<Function>production</Function>
<Domain>tom.info</Domain>
<Administrator EmailAlias="xrichards" Extension="221">Xavier Richards</Administrator>
<Attributes Type="Production" Version="20ix"/>
<Comments>
...
</Comments>
<Usage>
500
</Usage>
</DatabaseName>
<DatabaseName>
<GlobalName>Ricardo</GlobalName>
<Function>production</Function>
<Domain>tom.info</Domain>
<Administrator EmailAlias="xrichards" Extension="221">Xavier Richards</Administrator>
<Attributes Type="Production" Version="20i"/>
<Comments>
...
</Comments>
<Usage>
500
</Usage>
</DatabaseName>
<WebserverName>
<GlobalName>Jim</GlobalName>
<Function>distribution</Function>
<Domain>jim1235.com</Domain>
<Administrator EmailAlias="rkarvani" Extension="134237">Richard Karvani</Administrator>
<Administrator EmailAlias="stones" Extension="222237">Steve Jones</Administrator>
<Attributes Type="Production" Version="20i"/>
<Comments>
...
</Comments>
<Usage>
1200
</Usage>
</WebserverName>
</Inventory>
XSLT文档: <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>My Server List</h1>
<h2>Database Servers</h2>
<xsl:apply-templates select="Inventory/DatabaseName" />
<h2>Webservers</h2>
<xsl:apply-templates select="Inventory/WebserverName" />
</body>
</html>
</xsl:template>
<xsl:template match="DatabaseName">
<table border="1">
<tr>
<th>Name</th>
<th>Domain</th>
<th>Usage</th>
</tr>
<xsl:for-each select="Attributes[@Version='20i']">
<tr>
<td><xsl:value-of select="GlobalName"/></td>
<td><xsl:value-of select="Domain"/></td>
<td><xsl:value-of select="Usage"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="WebserverName">
<table border="1">
<tr>
<th>Name</th>
<th>Domain</th>
<th>Usage</th>
</tr>
<xsl:for-each select="Attributes[@Version='20i']">
<tr>
<td><xsl:value-of select="GlobalName"/></td>
<td><xsl:value-of select="Domain"/></td>
<td><xsl:value-of select="Usage"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
我认为你想把关于那个版本的条件放入apply-templates
,并将table
的创建移动到另一个模板:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>My Server List</h1>
<h2>Database Servers</h2>
<table border="1">
<tr>
<th>Name</th>
<th>Domain</th>
<th>Usage</th>
</tr>
<xsl:apply-templates select="Inventory/DatabaseName[Attributes[@Version='20i']]" />
</table>
<h2>Webservers</h2>
<table border="1">
<tr>
<th>Name</th>
<th>Domain</th>
<th>Usage</th>
</tr>
<xsl:apply-templates select="Inventory/WebserverName[Attributes[@Version='20i']]" />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="DatabaseName">
<tr>
<td><xsl:value-of select="GlobalName"/></td>
<td><xsl:value-of select="Domain"/></td>
<td><xsl:value-of select="Usage"/></td>
</tr>
</xsl:template>
<xsl:template match="WebserverName">
<tr>
<td><xsl:value-of select="GlobalName"/></td>
<td><xsl:value-of select="Domain"/></td>
<td><xsl:value-of select="Usage"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>