在xsl中显示来自相同元素名称的多个图像

时间:2017-10-16 07:58:44

标签: xml image xslt

我正在尝试使用xslt在xml中显示多个具有相同名称的图像。以下是我的XML中'restaurant'元素及其子元素的示例:

<cuisine type="Indian">
<restaurant>
    <name>Indu</name>
    <logo>indu_logo.png</logo>
    <pics>Website-Heirloom.jpg</pics>
    <pics>Website-Snapper.jpg</pics>
    <url>http://indudining.com.au</url>
    <address>350 George Street, (entry via Angel Place) Sydney</address>
    <phonenumber>02 9223 0158</phonenumber>
    <openinghours>12:00PM to late</openinghours>
    <delivery>Minimum Price: $50. Delivery Fee: $10</delivery>
    <description>INDU is a celebration of southern Indian and Sri Lankan village culture, flavours and local hospitality.</description>
    <menu>Desserts, Paratha, Basmati rice</menu>
    <onlinebooking>Yes</onlinebooking>
    <event>No</event>
</restaurant>

我正在尝试构建一个样式化的页面来显示这些信息,并且我设法显示了我需要的所有内容,除了“图片”,因为当我称之为“图片”时,它只能(可以理解)显示第一张图片。这是我当前的XSLT:

 <xsl:template match="/">

   <html>
     <head> 

        <title>Restaurant Info</title>      


      </head>
      <body>

        <h1>Restaurant List (A-Z):</h1>

        <xsl:for-each select="cuisine/restaurant">

            <xsl:sort select="name"/>

            <SPAN> <img src="images/{logo}" width="100"/> </SPAN>
            <BR />
            <SPAN STYLE="font-weight:bold">Restauraunt Name: </SPAN>
            <xsl:value-of select="name" />
            <BR />
            <SPAN STYLE="font-weight:bold">Description: </SPAN> 
            <xsl:value-of select="description" /> 
            <BR /> 
            <SPAN STYLE="font-weight:bold">URL: </SPAN> 
            <xsl:value-of select="url" /> 
            <BR /> 
            <SPAN STYLE="font-weight:bold">Address: </SPAN> 
            <xsl:value-of select="address" /> 
            <BR/>
            <SPAN> <img src="images/{pics}" width="200"/> </SPAN>
            <BR />


            <P/>
            <hr/>

        </xsl:for-each>



      </body>
   </html>

</xsl:template>

在不直接引用文件的情况下显示两张照片的最佳方法是什么?我需要我的xslt适用于具有相同节点名称的多个XML文档。

到目前为止,我已经尝试过:在我的主要for-each循环中创建另一个'for-each'循环并在其中调用'pics',在'pics()之后放置括号,希望它会逐步遍历每个循环,调用使用'xsl:value-of'而不是制作新模板并放入体内的照片。这些都没有奏效(虽然我做错了)所以我没有保存我想出的代码,但是如果有人知道怎么做而且不觉得我已经给它一个足够好的去应得的听到怎么样,让我知道并且很高兴重写我错误的代码。

感谢。

1 个答案:

答案 0 :(得分:0)

您可以为<xsl:for-each>添加另一个<pics>循环,如下所示。

<xsl:template match="/">
    <html>
        <head>
            <title>Restaurant Info</title>
        </head>
        <body>
            <h1>Restaurant List (A-Z):</h1>
            <xsl:for-each select="cuisine/restaurant">
                <xsl:sort select="name" />
                ...
                <xsl:for-each select="pics">
                    <SPAN>
                        <img src="images/{.}" width="200" />
                    </SPAN>
                </xsl:for-each>
                ...
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

输出

<SPAN>
    <img src="images/Website-Heirloom.jpg" width="200" />
</SPAN>
<SPAN>
    <img src="images/Website-Snapper.jpg" width="200" />
</SPAN>