2017-02-07 43 views
0

我具有以下XML結構:XSLT,創建表頭和行

<Tab4> 
    <T4Head1>First Table header 1</T4Head1> 
    <T4Head2>First Table header 2</T4Head2> 
    <T4Head3>First Table header 3</T4Head3> 
    <T4Head4>First Table header 4</T4Head4> 
</Tab4> 
<Tab4> 
    <T4Col1>First Table row 1 column 1</T4Col1> 
    <T4Col2>First Table row 1 column 2</T4Col2> 
    <T4Col3>First Table row 1 column 3</T4Col3> 
    <T4Col4>First Table row 1 column 4</T4Col4> 
</Tab4> 
another element than Tab4 might occur here... 
<Tab4> 
    <T4Head1>Second Table header 1</T4Head1> 
    <T4Head2>Second Table header 2</T4Head2> 
    <T4Head3>Second Table header 3</T4Head3> 
    <T4Head4>Second Table header 4</T4Head4> 
</Tab4> 

<Tab4> 
    <T4Col1>Second Table row 1 column 1</T4Col1> 
    <T4Col2>Second Table row 1 column 2</T4Col2> 
    <T4Col3>Second Table row 1 column 3</T4Col3> 
    <T4Col4>Second Table row 1 column 4</T4Col4> 
</Tab4> 
<Tab4> 
    <T4Col1>Second Table row 2 column 1</T4Col1> 
    <T4Col2>Second Table row 2 column 2</T4Col2> 
    <T4Col3>Second Table row 2 column 3</T4Col3> 
    <T4Col4>Second Table row 2 column 4</T4Col4> 
</Tab4> 
<Tab4> 
    <T4Col1>Second Table row 3 column 1</T4Col1> 
    <T4Col2>Second Table row 3 column 2</T4Col2> 
    <T4Col3>Second Table row 3 column 3</T4Col3> 
    <T4Col4>Second Table row 3 column 4</T4Col4> 
</Tab4> 
another element than Tab4 might occur here... 

隨着XSLT 1.0我需要輸出的HTML看起來像的下方。換句話說,我需要包含T4Col1,T4Col2等的每個Tab4塊重複,直到出現下一種元素(不是Tab4/T4Col1)。

<tr> 
     <td> 
      <table class="specTab" border="1"> 
       <tbody> 

        <tr> 
         <th> 
          Table header 1 
         </th> 
         <th> 
          Table header 2 
         </th> 
         <th> 
          Table header 3 
         </th> 
         <th> 
          Table header 4 
         </th> 
        </tr> 

<!-- this part should repeat --> 
         <tr> 
          <td> 
           Table column 1 
          </td> 
          <td> 
           Table column 2 
          </td> 
          <td> 
           Table column 3 
          </td> 
          <td> 
           Table column 4 
          </td> 
         </tr> 
<!-- this part should repeat -->  

       </tbody> 
      </table> 
     </td> 
    </tr> 

我已經有一些嘗試,但不能得到它的抓地力。我的當前XSLT代碼看起來是這樣的:

<xsl:for-each select="./node()"> 

    <xsl:choose> 

     <!-- Multiple other test scenarios --> 
     <xsl:when test="name()='Rub1' and . !=''"> 

     </xsl:when> 

     <xsl:when test="name()='Tab4' and ./T4Head1"> 
      <tr> 
       <td> 
        <table class="specTab" border="1"> 
         <tbody> 
          <tr> 
           <th> 
            <xsl:value-of select="./T4Head1"/> 
           </th> 
           <th> 
            <xsl:value-of select="./T4Head2"/> 
           </th> 
           <th> 
            <xsl:value-of select="./T4Head3"/> 
           </th> 
           <th> 
            <xsl:value-of select="./T4Head4"/> 
           </th> 
          </tr> 

          <xsl:for-each select="following-sibling::node()/T4Col1"> 

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

          </xsl:for-each> 

         </tbody> 
        </table> 
       </td> 
      </tr> 
     </xsl:when> 

     <xsl:otherwise> 
     </xsl:otherwise> 

    </xsl:choose> 

</xsl:for-each> 

問題的上述代碼是,它輸出所有TAB4/T4Col1(1 + 3),即使下一個報頭之後發生的那些。有點我需要將標題和所有行/列組合起來,因爲我希望它們位於同一個HTML表格中。

感謝任何幫助,我可以得到。

回答

0

考慮下面的例子:

XML

<root> 
    <p>para 1</p> 
    <Tab4> 
     <T4Head1>First Table header 1</T4Head1> 
     <T4Head2>First Table header 2</T4Head2> 
     <T4Head3>First Table header 3</T4Head3> 
     <T4Head4>First Table header 4</T4Head4> 
    </Tab4> 
    <Tab4> 
     <T4Col1>First Table row 1 column 1</T4Col1> 
     <T4Col2>First Table row 1 column 2</T4Col2> 
     <T4Col3>First Table row 1 column 3</T4Col3> 
     <T4Col4>First Table row 1 column 4</T4Col4> 
    </Tab4> 
    <p>para 2</p> 
    <Tab4> 
     <T4Head1>Second Table header 1</T4Head1> 
     <T4Head2>Second Table header 2</T4Head2> 
     <T4Head3>Second Table header 3</T4Head3> 
     <T4Head4>Second Table header 4</T4Head4> 
    </Tab4> 
    <Tab4> 
     <T4Col1>Second Table row 1 column 1</T4Col1> 
     <T4Col2>Second Table row 1 column 2</T4Col2> 
     <T4Col3>Second Table row 1 column 3</T4Col3> 
     <T4Col4>Second Table row 1 column 4</T4Col4> 
    </Tab4> 
    <Tab4> 
     <T4Col1>Second Table row 2 column 1</T4Col1> 
     <T4Col2>Second Table row 2 column 2</T4Col2> 
     <T4Col3>Second Table row 2 column 3</T4Col3> 
     <T4Col4>Second Table row 2 column 4</T4Col4> 
    </Tab4> 
    <Tab4> 
     <T4Col1>Second Table row 3 column 1</T4Col1> 
     <T4Col2>Second Table row 3 column 2</T4Col2> 
     <T4Col3>Second Table row 3 column 3</T4Col3> 
     <T4Col4>Second Table row 3 column 4</T4Col4> 
    </Tab4> 
    <p>para 3</p> 
</root> 

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:key name="rows" match="Tab4[T4Col1]" use="generate-id(preceding-sibling::Tab4[T4Head1][1])" /> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Tab4[T4Head1]"> 
    <table border="1"> 
     <thead> 
      <tr> 
       <xsl:apply-templates/> 
      </tr> 
     </thead> 
     <tbody> 
      <xsl:for-each select="key('rows', generate-id())"> 
       <tr> 
        <xsl:apply-templates/> 
       </tr> 
      </xsl:for-each> 
     </tbody> 
    </table> 
</xsl:template> 

<xsl:template match="Tab4[T4Col1]"/> 

<xsl:template match="Tab4[T4Head1]/*"> 
    <th> 
     <xsl:apply-templates/> 
    </th> 
</xsl:template> 

<xsl:template match="Tab4[T4Col1]/*"> 
    <td> 
     <xsl:apply-templates/> 
    </td> 
</xsl:template> 

</xsl:stylesheet> 

此使用key到主體行綁到他們的標題行,所以他們可以c通過它們的標題行的唯一ID進行匹配。

結果將是:

<root> 
    <p>para 1</p> 
    <table border="1"> 
     <thead> 
     <tr> 
      <th>First Table header 1</th> 
      <th>First Table header 2</th> 
      <th>First Table header 3</th> 
      <th>First Table header 4</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr> 
      <td>First Table row 1 column 1</td> 
      <td>First Table row 1 column 2</td> 
      <td>First Table row 1 column 3</td> 
      <td>First Table row 1 column 4</td> 
     </tr> 
     </tbody> 
    </table> 
    <p>para 2</p> 
    <table border="1"> 
     <thead> 
     <tr> 
      <th>Second Table header 1</th> 
      <th>Second Table header 2</th> 
      <th>Second Table header 3</th> 
      <th>Second Table header 4</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr> 
      <td>Second Table row 1 column 1</td> 
      <td>Second Table row 1 column 2</td> 
      <td>Second Table row 1 column 3</td> 
      <td>Second Table row 1 column 4</td> 
     </tr> 
     <tr> 
      <td>Second Table row 2 column 1</td> 
      <td>Second Table row 2 column 2</td> 
      <td>Second Table row 2 column 3</td> 
      <td>Second Table row 2 column 4</td> 
     </tr> 
     <tr> 
      <td>Second Table row 3 column 1</td> 
      <td>Second Table row 3 column 2</td> 
      <td>Second Table row 3 column 3</td> 
      <td>Second Table row 3 column 4</td> 
     </tr> 
     </tbody> 
    </table> 
    <p>para 3</p> 
</root> 

呈現爲:

enter image description here

+0

非常感謝!它節省了我的一天和很多頭痛:) – hico