0

我有一个问题,我需要从 xml 生成 html,并且我可以有多个标签相互嵌套。我怎样才能通过递归传递它们?

这是来自 xml 的示例:

<rows>
   <row>
       <cell>1</cell>
       <cell>2</cell>
       <cell>1</cell>
       <cell>2</cell>
       <row>
         <cell>3</cell>
         <cell>4</cell>
            <row>
              <cell>5</cell>
              <cell>6</cell>
              <cell>6</cell>
           </row>
       </row>
   </row>
 </rows>

我的 xslt 是:

    <table>
      <th>1</th><th>2</th>3<th>4</th><th>5</th>
      <xsl:for-each select="rows/row">
        <tr>
          <xsl:for-each select="cell">
            <td>
              <xsl:value-of select="."/>
            </td>
          </xsl:for-each>
         </tr>
         <xsl:for-each select="row">
           <tr>
             <xsl:for-each select="cell">
               <td>
                 <xsl:value-of select="."/>
               </td>
             </xsl:for-each>
           </tr>
         </xsl:for-each>    
       </xsl:for-each>   
     </table>

所以我现在的问题是如何显示每行中的所有属性?

编辑:从 xslt 生成的 html

<html><body>
<table>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>

<tr>
<td>1</td>
<td>2</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
</body></html>

第二次编辑:

xslt:

    <xsl:template match="cell">


     <td style="overflow:hidden;border:1px solid black;">
         <div style="width:100px;height:20px;margin-bottom: 10px;margin-top: 10px;">

           <xsl:variable name="id1" select="row/@id"/>

           <xsl:if test="starts-with(id1, 'Dir')">
               <xsl:value-of select="cell/@image"/>
             </xsl:if>

            <xsl:value-of select="."/>


         </div>
      </td>

   </xsl:template>

xml:

<row id="Dir_44630">
  <cell>Text</cell>
  <cell>1</cell>
  <cell>1.00</cell>
  <cell>3</cell>
  <cell 4</cell>
  <cell>5</cell>
  <cell>6</cell>
  <cell>7</cell>
</row>
4

1 回答 1

1

首先,在您的情况下,您将首先使用一个模板来匹配您的根元素

<xsl:template match="/rows">

在这种情况下,您必须编写代码来构建表头,然后开始寻找子元素

<xsl:template match="/rows">
   <table>
      <!-- Header -->
      <xsl:apply-templates select="row"/>
   </table>
</xsl:template>

然后,您将有一个模板来匹配元素,因此您可以输出tr元素,然后查找各个单元格

<xsl:template match="row">
   <tr>
      <xsl:apply-templates select="cell"/>
   </tr>
   <xsl:apply-templates select="row"/>
</xsl:template>

请注意递归调用以继续查找嵌套在当前行元素中的元素。

同样,您将有一个模板来匹配单元格元素,它只会输出一个td元素和单元格值。

我唯一不确定的是你关于应该输出哪些行的规则。看起来您不想输出嵌套两层或多层深的行元素。在这种情况下,您可以添加一个模板来忽略至少有两行或多行是祖先的行

<xsl:template match="row[ancestor::row[2]]"/>

这是完整的 XSLT

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

   <xsl:template match="/rows">
      <table>
         <tr>
            <th>1</th>
            <th>2</th>
            <th>3</th>
            <th>4</th>
            <th>5</th>
         </tr>
         <xsl:apply-templates select="row"/>
      </table>
   </xsl:template>

   <xsl:template match="row">
      <tr>
         <xsl:apply-templates select="cell"/>
      </tr>
      <xsl:apply-templates select="row"/>
   </xsl:template>

   <xsl:template match="row[ancestor::row[2]]"/>

   <xsl:template match="cell">
      <td>
         <xsl:value-of select="."/>
      </td>
   </xsl:template>
</xsl:stylesheet>

应用于您的示例 XML 时,将输出以下内容

<table>
   <tr>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
      <th>5</th>
   </tr>
   <tr>
      <td>1</td>
      <td>2</td>
      <td>1</td>
      <td>2</td>
   </tr>
   <tr>
      <td>3</td>
      <td>4</td>
   </tr>
</table>

编辑:如果要从与单元格元素匹配的模板中访问元素上的属性,则需要指定它是父元素,如下所示

<xsl:variable name="id1" select="../@id"/> 

这样做select="row/@id"实际上会查找作为当前单元格元素的子元素的行。

于 2012-10-19T07:54:43.143 回答