XSLT为动态XML列创建HTML表

时间:2013-11-17 00:52:16

标签: xml xslt

我正在努力将xml数据放入html表中。我是xslt的新手并试图完成这项工作。我有xml具有特定格式的动态#列 * xml有前两个固定列ID和ItemNum *然后下一列是3的集合,可以更改列的名称;在集合第一列名称必须在标题行中第二个是单元格的背景颜色,第三个是单元格的边框颜色

XML文档:(可以有多行和多组列)

    <?xml version="1.0" encoding="UTF-8" ?> 
<Rowsets DateCreated="2013-11-16T19:19:22" EndDate="2013-11-16T19:19:22" StartDate="2013-11-16T18:19:22" Version="12.1.4 Build(53)">
<Rowset>
    <Columns>
      <Column Description="ID"  /> 
      <Column Description="ItemNum"  /> 
      <Column Description="Column1Name"  /> 
      <Column Description="Column1NameBkgdColor"  /> 
      <Column Description="Column1NameBorderColor"  /> 
      <Column Description="Column2Name"  /> 
      <Column Description="Column2NameBkgdColor"  /> 
      <Column Description="Column2NameBorderColor"  /> 
    </Columns>
    <Row>
      <ID>1</ID> 
      <ItemNum>Item01</ItemNum> 
      <Column1Name>DataA1</Column1Name> 
      <Column1NameBkgdColor>#FFFFFF</Column1NameBkgdColor> 
      <Column1NameBorderColor>#000000</Column1NameBorderColor> 
      <Column2Name>DataB1</Column2Name> 
      <Column2NameBkgdColor>#3366FF</Column2NameBkgdColor> 
      <Column2NameBorderColor>#000000</Column2NameBorderColor> 
    </Row>
    <Row>
      <ID>2</ID> 
      <ItemNum>Item02</ItemNum> 
      <Column1Name>DataA2</Column1Name> 
      <Column1NameBkgdColor>#3366FF</Column1NameBkgdColor> 
      <Column1NameBorderColor>#FF66FF</Column1NameBorderColor> 
      <Column2Name>DataB2</Column2Name> 
      <Column2NameBkgdColor>#FFFFFF</Column2NameBkgdColor> 
      <Column2NameBorderColor>#FF9933</Column2NameBorderColor> 
    </Row>
  </Rowset>

我正在研究的XSLT(列名是硬编码的。需要一种方法才能接受动态列):

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:template match="/">
  <table border="1px">
     <tr>
        <xsl:for-each select="Rowsets/Rowset/Columns/Column">
           <xsl:if test="not(contains(@Description, 'Color'))">
              <td>
                 <xsl:value-of select="@Description"/>
              </td>
           </xsl:if>
        </xsl:for-each>
     </tr>
     <xsl:for-each select="Rowsets/Rowset/Row">
        <tr>
           <td>
              <xsl:value-of select="ID"/>
           </td>
           <td>
              <xsl:value-of select="ItemNum"/>
           </td>
           <td border="1px">
              <xsl:attribute name="bordercolor">
                 <xsl:value-of select="Column1NameBorderColor"/>
              </xsl:attribute>
              <xsl:attribute name="bgcolor">
                 <xsl:value-of select="Column1NameBkgdColor"/>
              </xsl:attribute>
              <xsl:value-of select="Column1Name"/>
           </td>
           <td border="1px">
              <xsl:attribute name="bordercolor">
                 <xsl:value-of select="Column2NameBorderColor"/>
              </xsl:attribute>
              <xsl:attribute name="bgcolor">
                 <xsl:value-of select="Column2NameBkgdColor"/>
              </xsl:attribute>
              <xsl:value-of select="Column2Name"/>
           </td>
        </tr>
     </xsl:for-each>
     <!-- Rowsets/Rowset/Row -->
  </table>

我正在寻找的最终HTML:

   <table border="1px">
   <tr>
   <td>ID</td><td>ItemNum</td><td>Column1Name</td><td>Column2Name</td>
   </tr>
   <tr><td>1</td><td>Item01</td><td border="1px" bordercolor="#000000" bgcolor="#FFFFFF">DataA1</td><td border="1px" bordercolor="#000000" bgcolor="#3366FF">DataB1</td>
   </tr>
   <tr>
   <td>2</td><td>Item02</td><td border="1px" bordercolor="#FF66FF"        bgcolor="#3366FF">DataA2</td><td border="1px" bordercolor="#FF9933" bgcolor="#FFFFFF">DataB2</td>
   </tr>
   <tr>
   tr for additional columns
   <td>multiple tds for set of columns</td>
   </tr>
   </table>

提前致谢!

1 个答案:

答案 0 :(得分:2)

以下XSLT 2.0在列和local-name()函数上使用循环来检查列标记并生成请求的结果

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
  <xsl:template match="/">
    <table border="1px">
      <tr>
        <xsl:for-each select="Rowsets/Rowset/Columns/Column">
          <xsl:if test="not(contains(@Description, 'Color'))">
            <td>
              <xsl:value-of select="@Description"/>
            </td>
          </xsl:if>
        </xsl:for-each>
      </tr>

      <xsl:for-each select="Rowsets/Rowset/Row">
        <tr>
          <td>
            <xsl:value-of select="ID"/>
          </td>
          <td>
            <xsl:value-of select="ItemNum"/>
          </td>

          <xsl:variable name="row" select="."/>
          <xsl:for-each select="../Columns/Column">

            <xsl:variable name="columnName" select="@Description"/>

            <xsl:if test="contains($columnName, 'Column') and not(contains($columnName, 'Color'))">
              <td border="1px">
                <xsl:attribute name="bordercolor">
                  <xsl:value-of select="$row/*[name() = concat($columnName, 'BorderColor')]"/>
                </xsl:attribute>
                <xsl:attribute name="bgcolor">
                  <xsl:value-of select="$row/*[name() = concat($columnName, 'BkgdColor')]"/>
                </xsl:attribute>
                <xsl:value-of select="$row/*[name() = $columnName]"/>
              </td>
            </xsl:if>

          </xsl:for-each>

        </tr>
      </xsl:for-each>
      <!-- Rowsets/Rowset/Row -->
    </table>
  </xsl:template>
</xsl:stylesheet>