要使用XSL/XML创建表吗

要使用XSL/XML创建表吗,xml,xslt,Xml,Xslt,我正在尝试使用XSL/XML创建一个表。我不熟悉XSL和XML,所以请对我放松点 我在一些事情上有点麻烦。 这是我的XML文件: <List> <Classification> <Class> <Label>Milk</Label> <NumberZone>1</NumberZone> <Zone> <Label&

我正在尝试使用XSL/XML创建一个表。我不熟悉XSL和XML,所以请对我放松点 我在一些事情上有点麻烦。 这是我的XML文件:

<List>
<Classification>
    <Class>
        <Label>Milk</Label>
        <NumberZone>1</NumberZone>

        <Zone>
            <Label>Milk1</Label>
            <Frontier>500</Frontier>
        </Zone>

        <Zone>
            <Label>Milk2</Label>
            <Frontier>600</Frontier>
        </Zone>

        <Zone>
            <Label>Milk3</Label>
            <Frontier>600</Frontier>
        </Zone>

        <Zone>
            <Label>Milk4</Label>
            <Frontier>700</Frontier>
        </Zone>

        <image>
            <File>milk.jpg</File>
        </image>
    </Class>

    <Class>
        <Label>Water</Label>
        <NumberZone>2</NumberZone>

        <Zone>
            <Label>Water1</Label>
            <Frontier>800</Frontier>
        </Zone>

        <Zone>
            <Label>Water2</Label>
            <Frontier>900</Frontier>
        </Zone>

        <image>
            <File>water.jpg</File>
        </image>
    </Class>

    <Class>
        <Label>Juice</Label>
        <NumberZone>3</NumberZone>

        <Zone>
            <Label>Juice1</Label>
            <Frontier>950</Frontier>
        </Zone>

        <Zone>
            <Label>Juice2</Label>
            <Frontier>990</Frontier>
        </Zone>

        <image>
            <File>juice.jpg</File>
        </image>
    </Class>

</Classification>
</List>
它通常较长,但我剪掉了一些部分

我希望我的桌子看起来像这样:

<td colspan="1"><input type="checkbox" name="Zone1" id="Zone1" /><xsl:value-of select="Zone[1]/Label"/></td>
<td colspan="1"><input type="checkbox" name="Zone2" id="Zone1" /><xsl:value-of select="Zone[2]/Label"/></td>
<td colspan="1"><input type="checkbox" name="Zone3" id="Zone1"/> <xsl:value-of select="Zone[3]/Label"/></td>
<td colspan="1"><input type="checkbox" name="Zone4" id="Zone1" /><xsl:value-of select="Zone[4]/Label"/></td>
第一栏:牛奶,水,果汁。。。 第二栏:我想要图片。 第三列:牛奶1、水1、果汁1。 第四列:牛奶2、水2、果汁2。 等等 到目前为止,我有:

<table border="1">

 <tr><th>Column 1</th><th>Image</th><th>Column 3</th></tr>


        <xsl:for-each select="Classification/Class">
        <tr>
            <td><em><xsl:value-of select="Label" /></em></td>

            <td>
            <xsl:attribute name="src">
            <xsl:value-of select="image/File"/>
            </xsl:attribute>
            </td>

            <td><xsl:value-of select="Zone/Label"/></td>
            <td colspan="1"><xsl:value-of select="Zone/Label"/></td>
            <td colspan="1"><xsl:value-of select="Zone/Label"/></td>
            </tr>
        </xsl:for-each>
</table>
这将完美地显示包含牛奶、水和果汁的第一列。 第二列不起作用,只是空白。图像不会出现在表中,您知道如何修复吗* 第三列也可以完美地显示牛奶1、水1、果汁1。 显然,第四列不起作用,因为我使用的值与第三列中的值相同。我不知道如何让Milk2、Water2和Juice2显示在该列中,因为它使用与Zone/Label相同的元素名称 所以有两件事我需要修正:我需要修正第二列以实际显示每个框中的图像。 我还需要修复第四列以显示Milk2、Water2和juice 2。 我现在的主要问题是让第四列正常工作,我真的不知道如何显示Milk2、Water2等等

我还希望稍后在每个单元格中添加单选按钮或复选框,以便在我的网页中使用JavaScript。我觉得每个框都需要不同的ID,所以我不确定如何使用XSL实现这一点,因为它只是在XML文件中循环,所以我不确定如何在每个单元格中添加单选按钮或复选框。*现在这并没有那么重要,在我希望得到前面提到的帮助之后,我会继续努力,但这也会很有帮助

编辑:

我有两个问题。 我通过如下操作添加了复选框:

<td colspan="1"><input type="checkbox" name="Zone1" id="Zone1" /><xsl:value-of select="Zone[1]/Label"/></td>
<td colspan="1"><input type="checkbox" name="Zone2" id="Zone1" /><xsl:value-of select="Zone[2]/Label"/></td>
<td colspan="1"><input type="checkbox" name="Zone3" id="Zone1"/> <xsl:value-of select="Zone[3]/Label"/></td>
<td colspan="1"><input type="checkbox" name="Zone4" id="Zone1" /><xsl:value-of select="Zone[4]/Label"/></td>
问题是,我认为每列中的每个元素都有相同的ID,对吗?我如何更改它,使它们都具有不同的ID


另外,在我的问题中,从XML文件中看不到它,但稍后将只有Milk4。这意味着我希望Water4和Juice 4的单元格完全消失,但它仍然存在,并带有一个复选框。

对于您的第二列,您正在这样做

<td>
   <xsl:attribute name="src">
   <xsl:value-of select="image/File"/>
   </xsl:attribute>
</td>
但这会导致以下HTML

<td src="milk.jpg" />
但是,您确实需要使用img标记来显示图像,因此请改为使用img标记

<td>
   <img src="{image/File}" />
</td>
对于第三列和第四列,可以添加xpath表达式来指定位置

<xsl:value-of select="Zone[1]/Label"/>
<xsl:value-of select="Zone[2]/Label"/>
事实上,为了避免一些代码重复,最好在这里使用模板匹配

 <xsl:apply-templates select="Zone[position() &lt; 3]" />

 <xsl:template match="Zone">
     <xsl:value-of select="Label"/>
 </xsl:template>
如果你想扩展它来显示你的复选框,你可以这样做

<xsl:template match="Zone">
   <xsl:variable name="index">
      <xsl:number />
   </xsl:variable>
   <input type="checkbox" name="Zone{$index}" id="Zone{$index}" />
   <xsl:value-of select="Label"/>
</xsl:template>
请注意这是如何使用属性值模板来输出id的。大括号{}表示它是一个要计算的表达式,而不是字面上的输出

这是完整的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="/List">
      <table border="1">
         <tr>
            <th>Column 1</th>
            <th>Image</th>
            <th>Column 3</th>
         </tr>
         <xsl:for-each select="Classification/Class">
            <tr>
               <td>
                  <em>
                     <xsl:value-of select="Label"/>
                  </em>
               </td>
               <td>
                  <img src="{image/File}"/>
               </td>
               <td colspan="1"><xsl:apply-templates select="Zone[1]" /></td>
               <td colspan="1"><xsl:apply-templates select="Zone[2]" /></td>
               <td colspan="1"><xsl:apply-templates select="Zone[3]" /></td>
               <td colspan="1"><xsl:apply-templates select="Zone[4]" /></td>
            </tr>
         </xsl:for-each>
      </table>
   </xsl:template>

   <xsl:template match="Zone">
      <xsl:variable name="index">
         <xsl:number />
      </xsl:variable>
      <input type="checkbox" name="Zone{$index}" id="Zone{$index}" />
      <xsl:value-of select="Label"/>
   </xsl:template>
</xsl:stylesheet>
应用于XML时,将输出以下内容

<table border="1">
   <tr>
      <th>Column 1</th>
      <th>Image</th>
      <th>Column 3</th>
   </tr>
   <tr>
      <td>
         <em>Milk</em>
      </td>
      <td>
         <img src="milk.jpg"/>
      </td>
      <td colspan="1">
         <input type="checkbox" name="Zone1" id="Zone1"/>Milk1</td>
      <td colspan="1">
         <input type="checkbox" name="Zone2" id="Zone2"/>Milk2</td>
      <td colspan="1">
         <input type="checkbox" name="Zone3" id="Zone3"/>Milk3</td>
      <td colspan="1">
         <input type="checkbox" name="Zone4" id="Zone4"/>Milk4</td>
   </tr>
   <tr>
      <td>
         <em>Water</em>
      </td>
      <td>
         <img src="water.jpg"/>
      </td>
      <td colspan="1">
         <input type="checkbox" name="Zone1" id="Zone1"/>Water1</td>
      <td colspan="1">
         <input type="checkbox" name="Zone2" id="Zone2"/>Water2</td>
      <td colspan="1"/>
      <td colspan="1"/>
   </tr>
   <tr>
      <td>
         <em>Juice</em>
      </td>
      <td>
         <img src="juice.jpg"/>
      </td>
      <td colspan="1">
         <input type="checkbox" name="Zone1" id="Zone1"/>Juice1</td>
      <td colspan="1">
         <input type="checkbox" name="Zone2" id="Zone2"/>Juice2</td>
      <td colspan="1"/>
      <td colspan="1"/>
   </tr>
</table>

对于第二个专栏,您正在这样做

<td>
   <xsl:attribute name="src">
   <xsl:value-of select="image/File"/>
   </xsl:attribute>
</td>
但这会导致以下HTML

<td src="milk.jpg" />
但是,您确实需要使用img标记来显示图像,因此请改为使用img标记

<td>
   <img src="{image/File}" />
</td>
对于第三列和第四列,可以添加xpath表达式来指定位置

<xsl:value-of select="Zone[1]/Label"/>
<xsl:value-of select="Zone[2]/Label"/>
事实上,为了避免一些代码重复,最好在这里使用模板匹配

 <xsl:apply-templates select="Zone[position() &lt; 3]" />

 <xsl:template match="Zone">
     <xsl:value-of select="Label"/>
 </xsl:template>
如果你想扩展它来显示你的复选框,你可以这样做

<xsl:template match="Zone">
   <xsl:variable name="index">
      <xsl:number />
   </xsl:variable>
   <input type="checkbox" name="Zone{$index}" id="Zone{$index}" />
   <xsl:value-of select="Label"/>
</xsl:template>
请注意这是如何使用属性值模板来输出id的。大括号{}表示它是一个要计算的表达式,而不是字面上的输出

这是完整的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="/List">
      <table border="1">
         <tr>
            <th>Column 1</th>
            <th>Image</th>
            <th>Column 3</th>
         </tr>
         <xsl:for-each select="Classification/Class">
            <tr>
               <td>
                  <em>
                     <xsl:value-of select="Label"/>
                  </em>
               </td>
               <td>
                  <img src="{image/File}"/>
               </td>
               <td colspan="1"><xsl:apply-templates select="Zone[1]" /></td>
               <td colspan="1"><xsl:apply-templates select="Zone[2]" /></td>
               <td colspan="1"><xsl:apply-templates select="Zone[3]" /></td>
               <td colspan="1"><xsl:apply-templates select="Zone[4]" /></td>
            </tr>
         </xsl:for-each>
      </table>
   </xsl:template>

   <xsl:template match="Zone">
      <xsl:variable name="index">
         <xsl:number />
      </xsl:variable>
      <input type="checkbox" name="Zone{$index}" id="Zone{$index}" />
      <xsl:value-of select="Label"/>
   </xsl:template>
</xsl:stylesheet>
应用于XML时,将输出以下内容

<table border="1">
   <tr>
      <th>Column 1</th>
      <th>Image</th>
      <th>Column 3</th>
   </tr>
   <tr>
      <td>
         <em>Milk</em>
      </td>
      <td>
         <img src="milk.jpg"/>
      </td>
      <td colspan="1">
         <input type="checkbox" name="Zone1" id="Zone1"/>Milk1</td>
      <td colspan="1">
         <input type="checkbox" name="Zone2" id="Zone2"/>Milk2</td>
      <td colspan="1">
         <input type="checkbox" name="Zone3" id="Zone3"/>Milk3</td>
      <td colspan="1">
         <input type="checkbox" name="Zone4" id="Zone4"/>Milk4</td>
   </tr>
   <tr>
      <td>
         <em>Water</em>
      </td>
      <td>
         <img src="water.jpg"/>
      </td>
      <td colspan="1">
         <input type="checkbox" name="Zone1" id="Zone1"/>Water1</td>
      <td colspan="1">
         <input type="checkbox" name="Zone2" id="Zone2"/>Water2</td>
      <td colspan="1"/>
      <td colspan="1"/>
   </tr>
   <tr>
      <td>
         <em>Juice</em>
      </td>
      <td>
         <img src="juice.jpg"/>
      </td>
      <td colspan="1">
         <input type="checkbox" name="Zone1" id="Zone1"/>Juice1</td>
      <td colspan="1">
         <input type="checkbox" name="Zone2" id="Zone2"/>Juice2</td>
      <td colspan="1"/>
      <td colspan="1"/>
   </tr>
</table>

谢谢你抽出时间。我会尽量让单选按钮正常工作,如果我不能让它正常工作,我会问更多的问题。我有一些问题,如果你能看一下的话,我会编辑我的问题。谢谢你抽出时间。我会努力让单选按钮工作,如果我不能让它工作,我会问更多的问题。我有一些问题,编辑了我的问题,如果你能看一下的话。