2012-12-08 88 views
3

嗨,我是一个初学者,使用XML和XSL。我想知道是否有任何方法可以使用元素或属性在XML文件中声明图像,然后在XSL文件中使用此图像将其输入到表格中,而不必在XSL中创建表格并输入图像逐一放入表格单元格中。这是我目前的XML文档(不完整的,因为我只是测试它。)从XML插入图像到XSL文档

<?xml version= "1.0"?> 
<?xml-stylesheet type="text/xsl" href="stylesheet4.xsl"?> 
    <countries> 
     <country> 
      <countryname>United States</countryname> 
      <countryflag>bg_locale.jpg</countryflag> 
     </country> 

     <country> 
      <countryname>United Kingdom</countryname> 
     </country> 

     <country> 
      <countryname>Deutschland</countryname> 
     </country> 
     </countries> 

这里是我创建的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图像逐一显示在表中。

您能否帮我解答谢谢。

+0

请出示你所期望的HTML输出。 – ABach

回答

5

这不是确切的解决方案,但我展示了如何使用for-each来复制不同的标志!

<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是否存在。我假设它会永远在那里。你需要有一个检查,如果countryflag存在/空安全上创建年底img标签,否则它可能会创建一个SRC为空img标签之前..

编辑:简单解决方案没有<xsl:element>标记..

<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> 
+1

非常感谢。这工作。我现在只需要将它放在我的文档中。 thansk这么多的帮助 – Ibrahim

+1

用''代替整个'xsl:element'结构会容易得多。 –

+1

@DanielHaley,谢谢你的建议! –

2

您应该为您的apply-templates元素提供一个select属性。丢弃也TR-标签括起来:

<?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> 

+0

很好的答案+1 .. –