Html 将图像从XML插入XSL文档
我想知道是否有任何方法可以使用元素或属性在XML文件中声明图像,然后在XSL文件中使用该图像将其输入到表中,而不必在XSL中创建表并将图像逐个输入到表单元格中。这是我当前的XML文档(不完整,因为我正在测试它)Html 将图像从XML插入XSL文档,html,xml,image,templates,xslt,Html,Xml,Image,Templates,Xslt,我想知道是否有任何方法可以使用元素或属性在XML文件中声明图像,然后在XSL文件中使用该图像将其输入到表中,而不必在XSL中创建表并将图像逐个输入到表单元格中。这是我当前的XML文档(不完整,因为我正在测试它) 美国 bg_locale.jpg 大不列颠联合王国 德国 以下是我创建的XSL文件和我尝试使用的方法: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:x
美国
bg_locale.jpg
大不列颠联合王国
德国
以下是我创建的XSL文件和我尝试使用的方法:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/countries">
<html>
<body bgcolor="black">
<div id="container" style="100%">
<div id="header" style="background-color:black; height:60px"></div>
<div id="content_container" align="center">
<div id="content" align="left" style="background: url('bg_locale.jpg');height:845px;width:848px">
Content goes here
<img src="logo_timber.jpg" align="left"/><br/>
<br/>
<br/>
<br/>
<table border="1">
<tr>
<xsl:apply-templates/>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
</xsl:template>
<xsl:template match="country">
<tr><td><xsl:value-of select="countryflag"/></td></tr>
</xsl:template>
</xsl:stylesheet>
内容在这里
正如您所见,我创建了一个表,希望XSL从XML文件中获取图像,然后将其保存,以便每个countryflag图像都一个接一个地显示在表中。这不是精确的解决方案,但我正在向您演示如何使用每个图像来复制不同的标志
<xsl:template match="/countries">
<table>
<xsl:for-each select="country">
<tr>
<td>
<xsl:value-of select="countryname"/>
</td>
<td>
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="countryflag"/>
</xsl:attribute>
<xsl:attribute name="align">left</xsl:attribute>
</xsl:element>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
左边
这将复制不同行中不同countryflag的值
Ps:这只是一个示例代码!我没有检查countryflag是否存在。我想它会一直在那里。。在创建img标记之前,您需要检查countryflag是否在安全端存在/null,否则它可能会创建src为null的img标记
编辑:不带
标签的更简单解决方案
<xsl:template match="/countries">
<table>
<xsl:for-each select="country">
<tr>
<td>
<xsl:value-of select="countryname"/>
</td>
<td>
<img src="{countryflag}" align="left"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
您应该为apply templates元素提供一个select属性。同时丢弃其周围的tr标签:
<?xml version="1.0" encoding="ISO-8859-1"?>
内容在这里
请显示您期望的HTML输出。将整个
xsl:element
结构替换为
会更容易。
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:template match="/countries">
<html>
<head></head>
<body bgcolor="black">
<div id="container" style="100%">
<div id="header" style="background-color:black; height:60px"></div>
<div id="content_container" align="center">
<div id="content" align="left" style="background: url('bg_locale.jpg');height:845px;width:848px">
Content goes here
<img src="logo_timber.jpg" align="left"/><br/>
<br/>
<br/>
<br/>
<table border="1">
<xsl:apply-templates select="country" />
</table>
</div>
</div>
</div>
</body>
</html>
</xsl:template>
<xsl:template match="country">
<tr>
<td>
<xsl:value-of select="countryflag"/>
</td>
</tr>
</xsl:template>