我想输出数据库的名称,域和用法,其版本等于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>
答案 0 :(得分:0)
我认为您希望将有关该版本的条件纳入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>