2014-03-24 87 views
0

我正在嘗試將xsl樣式表應用於我的xml文件,遵循W3School和其他網站上的說明,但是由於我遇到的問題而感到困惑。我有多個模板,基本的一個很好,但表格很棘手。我的標題重複每行,顯然爲每個創建新的表。下面是一個小樣本。有人能告訴我我要去哪裏嗎?xsl模板表問題

XSL

<xsl:output method="html"/> 
    <xsl:template match="/"> 
    <html> 
     <body> 
     <h2>Site Visit Form</h2> 
     <xsl:apply-templates/> 
     </body> 
    </html> 
    </xsl:template> 
    <xsl:template match="SiteVisitForm"> 
    <p> 
     <xsl:apply-templates select="SiteVisit"/> 
     <xsl:apply-templates select="field"/> 
    </p> 
    </xsl:template> 
    <xsl:template match="SiteVisit"> 
    <p> 
     Site Visit Code: <span style="color:#ff0000"> 
     <xsl:value-of select="SiteVisitCode"/> 
     </span> 
     Project ID: <span style="color:#ff0000"> 
     <xsl:value-of select="Project_ID"/> 
     <xsl:value-of select="NewProject"/> 
     </span> 
     <br /> 
     Personnel:<span style="color:#ff0000"> 
     <xsl:value-of select="Personnel"/> 
     </span> 
    </p> 
    </xsl:template> 

    <xsl:template match="field"> 
     <table border="1"> 
     <tr bgcolor="#9acd32"> 
     <th>Charactistic Name</th> 
     <th>Result Value</th> 
     <th>Unit</th> 
     <th>Analytical Method</th> 
     <th>Result Comment</th> 
     </tr> 
     <tr> 
      <td><xsl:value-of select="Characteristic_Name"/></td> 
      <td><xsl:value-of select="Result_Value"/></td> 
      <td><xsl:value-of select="Result_Value_Unit"/></td> 
      <td><xsl:value-of select="Analytical_Method"/></td> 
      <td><xsl:value-of select="Result_Comment"/></td> 
     </tr> 
     </table> 
     </xsl:template> 

XML

<SiteVisit> 
<SiteVisitCode>Test</SiteVisitCode> 
<Project_ID>FLAT-STILL-TPA</Project_ID> 
<NewProject></NewProject> 
<Personnel>Andersen, Laura,Apfelbeck, Randy,Arroues, Pamela</Personnel> 
</siteVisit> 
<field> 
<Characteristic_Name>Temperature, water</Characteristic_Name> 
<Result_Value>12</Result_Value> 
<Result_Value_Unit>deg C</Result_Value_Unit> 
</field> 
<field> 
<Characteristic_Name>Temperature, air</Characteristic_Name> 
<Result_Value>60</Result_Value> 
<Result_Value_Unit>deg F</Result_Value_Unit> 
</field> 
<field> 
<Characteristic_Name>Specific conductance</Characteristic_Name> 
<Result_Value>122</Result_Value> 
<Result_Value_Unit>uS/cm</Result_Value_Unit> 
</field> 
<field> 
<Characteristic_Name>pH</Characteristic_Name> 
<Result_Value>123</Result_Value> 
<Result_Value_Unit>None</Result_Value_Unit> 
</field> 

回答

1

你必須在模板申報 「表」 和 「TR」 只有一次 「SiteVisitForm」,並定義後應用模板的 「場」 「tr」:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
<xsl:strip-space elements="*"/> 
<xsl:output method="html"/> 
<xsl:template match="/"> 
    <html> 
     <body> 
      <h2>Site Visit Form</h2> 
      <xsl:apply-templates/> 
     </body> 
    </html> 
</xsl:template> 
<xsl:template match="SiteVisitForm"> 
    <p> 
     <xsl:apply-templates select="SiteVisit"/> 
     <table border="1"> 
      <tr bgcolor="#9acd32"> 
       <th>Charactistic Name</th> 
       <th>Result Value</th> 
       <th>Unit</th> 
       <th>Analytical Method</th> 
       <th>Result Comment</th> 
      </tr> 
      <xsl:apply-templates select="field"/> 
     </table> 
    </p> 
</xsl:template> 
<xsl:template match="SiteVisit"> 
    <p> 
    Site Visit Code: <span style="color:#ff0000"> 
      <xsl:value-of select="SiteVisitCode"/> 
     </span> 
    Project ID: <span style="color:#ff0000"> 
      <xsl:value-of select="Project_ID"/> 
      <xsl:value-of select="NewProject"/> 
     </span> 
     <br /> 
    Personnel:<span style="color:#ff0000"> 
      <xsl:value-of select="Personnel"/> 
     </span> 
    </p> 
</xsl:template> 

<xsl:template match="field"> 

    <tr> 
     <td> 
      <xsl:value-of select="Characteristic_Name"/> 
     </td> 
     <td> 
      <xsl:value-of select="Result_Value"/> 
     </td> 
     <td> 
      <xsl:value-of select="Result_Value_Unit"/> 
     </td> 
     <td> 
      <xsl:value-of select="Analytical_Method"/> 
     </td> 
     <td> 
      <xsl:value-of select="Result_Comment"/> 
     </td> 
    </tr> 

</xsl:template> 
</xsl:stylesheet> 
+1

你是天才!謝謝! – pja