0

我的 xml 文件名为 product_file.xml 并且我有一个 xslt 文件,我通过将 xml 和 xslt 文件绑定到控件来在我的网页中使用 xml 控件,但我没有正确获得输出。它多次只显示标题

<?xml version="1.0" encoding="utf-8" ?>
<productlist>
  <product>
    <itemnumber>1</itemnumber>
    <name>Staple Superior Men’s Jacket</name>
    <description>The Staple Superior T-bone PU Jacket is classic hood with a drawstring       fastening, long and a zip up front fastening.</description>
    <color>BLACK</color>
    <image>Staplesuperior1.gif</image>
    <price>$140</price>
    <size>69</size>
    <quantity>1</quantity>
    <category>Men</category>
  </product>
  <product>
    <itemnumber>2</itemnumber>
    <name>Afends Mens t-shirt</name>
    <description>The Afends Thrash For Cash Raglan Tee has a scooped hemline, a 100% cotton composition, and a regular fit.</description>
    <color>Beach Heather &amp; Black</color>
    <image>Afends1.gif</image>
    <price>$90</price>
    <size>80</size>
    <quantity>1</quantity>
    <category>Men</category>
  </product>
  <product>
    <itemnumber>3</itemnumber>
    <name>Wrangler Men Blue Vegas Skinny Fit Jeans</name>
    <description>Blue 5 pocket jeans, stretchable, has a zip fly with buttoned closure in front, 2 scoop pockets at sides</description>
    <image>Wrangler1.gif</image>
    <color>BLUE</color>
    <price>$125</price>
    <size>32</size>
    <quantity>1</quantity>
    <category>Men</category>
  </product>
  <product>
    <itemnumber>4</itemnumber>
    <name>Roadster Women Top</name>
    <description>Black top, knit, V neck, short extended sleeves, lattice like fabric manipulation detail at shoulders</description>
    <color>BLACK</color>
    <image>Roadster.gif</image>
    <price>$55</price>
    <size>Small</size>
    <quantity>1</quantity>
    <category>Women</category>
  </product>      

我的 xslt 文件是

<xsl:template match="product">    
      <html>
        <head>
          <title>Products XSLT Demo</title>
        </head>
        <body>
          <h2>Products Information</h2>
          <br/>          
            <!--<xsl:copy>
              <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>-->          
          <table bgcolor="#008BE7" border="1">
          <xsl:for-each select="itemnumber">            
            <tr>
              <td>Item Number</td>
              <td>
                <xsl:value-of select="itemnumber"/>
              </td>
            </tr>
            <tr> <td>name</td>
              <td>
                <xsl:value-of select="name"/>
              </td>
            </tr>
            <tr>
              <td>description</td>
              <td>
                <xsl:value-of select="description"/>
              </td>
            </tr>
            <tr>
              <td>color</td>
              <td>
                <xsl:value-of select="color"/>
              </td>
            </tr>
            <tr>
              <td>price</td>
              <td>
                <xsl:value-of select="price"/>
              </td>
            </tr>
            <tr>
              <td>size</td>
              <td>
                <xsl:value-of select="size"/>
              </td>
            </tr>
            <tr>  <td>quantity</td>
              <td>
                <xsl:value-of select="quantity"/>
              </td>
            </tr>
            <tr>
              <td>category</td>
              <td>
                <xsl:value-of select="category"/>
              </td>
            </tr>
            </xsl:for-each>
          </table>          
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>

现在错误是 foreach 表达式不起作用,因此输出仅显示多次标题

4

2 回答 2

1

这似乎对我有用。改变templatematchtoproductlistforeachtoproduct

<xsl:template match="productlist">    
      <html>
        <head>
          <title>Products XSLT Demo</title>
        </head>
        <body>
          <h2>Products Information</h2>
          <br/>          
            <!--<xsl:copy>
              <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>-->          
          <table bgcolor="#008BE7" border="1">
          <xsl:for-each select="product">            
            <tr>
              <td>Item Number</td>
              <td>
                <xsl:value-of select="itemnumber"/>
              </td>
            </tr>
            <tr> <td>name</td>
              <td>
                <xsl:value-of select="name"/>
              </td>
            </tr>
            <tr>
              <td>description</td>
              <td>
                <xsl:value-of select="description"/>
              </td>
            </tr>
            <tr>
              <td>color</td>
              <td>
                <xsl:value-of select="color"/>
              </td>
            </tr>
            <tr>
              <td>price</td>
              <td>
                <xsl:value-of select="price"/>
              </td>
            </tr>
            <tr>
              <td>size</td>
              <td>
                <xsl:value-of select="size"/>
              </td>
            </tr>
            <tr>  <td>quantity</td>
              <td>
                <xsl:value-of select="quantity"/>
              </td>
            </tr>
            <tr>
              <td>category</td>
              <td>
                <xsl:value-of select="category"/>
              </td>
            </tr>
            </xsl:for-each>
          </table>          
        </body>
      </html>
    </xsl:template>
于 2013-10-10T07:24:22.210 回答
0

将 foreach 更改为

<xsl:for-each select=".">  
于 2013-10-10T07:22:14.293 回答