我有以下XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<result name="response" numFound="10000" start="0">
<doc>
<str name="Title">Title 1</str>
<str name="GUID">IMG_1</str>
<str name="Desc">Desc 1</str>
</doc>
<doc>
<str name="Title">Title 2</str>
<str name="GUID">IMG_2</str>
<str name="Desc">Desc 2</str>
</doc>
</result>
</response>
我想将其转换为以下HTML文件:
<html>
<head> </head>
<body>
<table border="1">
<tr>
<td colspan="2">
{Title}
</td>
</tr>
<tr>
<td>
<img src="http://myserver/images/{GUID}">
</td>
<td>
{Desc}
</td>
</tr>
<tr>
<td colspan="2">
{Title}
</td>
</tr>
<tr>
<td>
<img src="http://myserver/images/{GUID}">
</td>
<td>
{Desc}
</td>
</tr>
</table>
</body>
</html>
{}
应该从XML文件中替换其中的内容。我很好奇如何使用XSL做到这一点。我的尝试如下,但结果是空白的,即我得到了Table结构,但没有Titles或Descriptions。
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >
<xsl:output media-type="text/html; charset=UTF-8" encoding="UTF-8"/>
<xsl:template match='/'>
<html>
<head> </head>
<body>
<table border="1">
<xsl:apply-templates select="response/result/doc"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="doc">
<xsl:variable name="title" select="doc/str[@name='Title']"/>
<xsl:variable name="guid" select="doc/str[@name='GUID']"/>
<xsl:variable name="desc" select="doc/str[@name='Desc']"/>
<tr>
<td colspan="2">
<xsl:value-of select="$title"/>
</td>
</tr>
<tr>
<td>
<img src="http://myserver/images/{GUID}" />
</td>
<td>
<xsl:value-of select="$desc"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
我会很感激任何帮助,因为我是新手。