我的XML文件使用XSLT显示,我希望这些表彼此相邻,即一行3列。到目前为止,我所看到的是一个接一个的显示。

这是xml:

<Result>
<DressPrice>
  <Name>Dress 2</Name>
  <Price>20</Price>
  <Image>2.jpg</Image>
</DressPrice>
<DressPrice>
  <Name>Dress 9</Name>
  <Price>20</Price>
  <Image>9.jpg</Image>
</DressPrice>
<DressPrice>
  <Name>Dress 10</Name>
  <Price>20</Price>
  <Image>10.jpg</Image>
</DressPrice>
<DressPrice>
  <Name>Dress 11</Name>
  <Price>20</Price>
  <Image>11.jpg</Image>
</DressPrice>
<DressPrice>
  <Name>Dress 12</Name>
  <Price>20</Price>
  <Image>12.jpg</Image>
</DressPrice>
</Result>


XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*" />
<xsl:template match="/">
<html>
    <head><title>Dresses Per Price</title>
    <link rel="stylesheet" type="text/css" href="price.css"/>
    </head>
    <body>
        <h3>Dresses Per Price Displayed</h3>
        <table>
            <thead>
                <tr style="background-color:PaleGreen;"></tr>
                <tr></tr>
            </thead>
            <tbody>
                <xsl:for-each select="Result">
                    <xsl:apply-templates>
                        <xsl:sort select="Name" data-type="text" order="ascending"/>
                    </xsl:apply-templates>
                </xsl:for-each>
            </tbody>
        </table>

    </body>
</html>
</xsl:template>

<xsl:template match="DressPrice">
<xsl:variable name="cssClass">
    <xsl:choose>
        <xsl:when test="position() mod 2 = 0">coloured</xsl:when>
        <xsl:otherwise>normal</xsl:otherwise>
    </xsl:choose>
</xsl:variable>
        <table>
       <tr>
        <xsl:apply-templates select="Image"/>
        </tr>
        </table>
        <table  width="300px" style="text-align:center;">
   <tr style="font-family:Gisha; font-size:15px; color:black;" width="100px" colspan="2"> <xsl:apply-templates select="Name"/></tr>
   </table>
   <table width="300px" style="text-align:center;">
   <tr style="font-family:Gisha; font-size:15px; color:black;"><xsl:apply-templates select="Price"/>
</tr>
</table>
</xsl:template>
<xsl:template match="Image"> <!--To display image-->
    <td style="border: 2px solid black;"><img src="{.}" width="350px"/></td>
</xsl:template>

<xsl:template match="Price">
<td style="width:100px;">$<xsl:value-of select="text()"/></td>
</xsl:template>

<xsl:template match="Name">
<td style="width:100px;"><xsl:value-of select="text()"/></td>
</xsl:template>
</xsl:stylesheet>


我如何使其每行显示3个?(图像,说明和每行价格)。

最佳答案

---编辑---

要创建在同一行中并排显示3个产品的表,请尝试:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/Result">
    <html>
        <head>
            <title>Dresses Per Price</title>
            <link rel="stylesheet" type="text/css" href="price.css"/>
        </head>
        <body>
            <h3>Dresses Per Price Displayed</h3>
            <table>
                <xsl:for-each select="DressPrice[position() mod 3 = 1]">
                    <tr>
                        <xsl:apply-templates select=". | following-sibling::DressPrice[position() &lt; 3]"/>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="DressPrice">
    <td>
        <img src="{Image}"/>
        <br/>
        <xsl:value-of select="Name"/>
        <br/>
        <xsl:value-of select="format-number(Price, '$0.00')"/>
    </td>
</xsl:template>

</xsl:stylesheet>




注意:

另一个选择是创建所有产品的无序列表:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/Result">
    <html>
        <head>
            <title>Dresses Per Price</title>
            <link rel="stylesheet" type="text/css" href="price.css"/>
        </head>
        <body>
            <h3>Dresses Per Price Displayed</h3>
            <table>
                <ul>
                    <xsl:apply-templates select="DressPrice"/>
                </ul>
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="DressPrice">
    <li>
        <img src="{Image}"/>
        <br/>
        <xsl:value-of select="Name"/>
        <br/>
        <xsl:value-of select="format-number(Price, '$0.00')"/>
    </li>
</xsl:template>

</xsl:stylesheet>


然后使用CSS将列表排列在网格中。

07-24 09:44
查看更多