2009-10-20 84 views
0

任何人都可以幫助我進行以下轉換嗎?Xslt轉換 - 每個

這裏是XML

<Chart> 
<Chart.Series> 
    <DataSeries LegendText="Complete On Time" > 
     <DataSeries.DataPoints> 
      <DataPoint AxisXLabel="Sep 09" YValue="10" /> 
      <DataPoint AxisXLabel="Oct 09" YValue="11" /> 
      <DataPoint AxisXLabel="Nov 09" YValue="12" /> 
     </DataSeries.DataPoints> 
    </DataSeries> 

    <DataSeries LegendText="Complete Overdue" > 
     <DataSeries.DataPoints> 
      <DataPoint YValue="1" /> 
      <DataPoint YValue="2" /> 
      <DataPoint YValue="3" /> 
     </DataSeries.DataPoints> 
    </DataSeries> 
</Chart.Series> 
</Chart> 

,這裏是輸出ID喜歡

<table> 
<thead> 
    <tr> 
     <th></th> 
     <th>Complete On Time</th> 
     <th>Complete Overdue</th> 
    </tr> 
</thead> 
<tbody> 
    <tr> 
     <th>Sep 09</th> 
     <th>10</th> 
     <th>1</th> 
    </tr> 
    <tr> 
     <th>Oct 09</th> 
     <th>11</th> 
     <th>2</th> 
    </tr> 
    <tr> 
     <th>Nov 09</th> 
     <th>12</th> 
     <th>3</th> 
    </tr> 
</tbody> 

+0

@meboz:你嘗試過這麼遠嗎?你卡在哪裏? – 2009-10-20 22:50:11

+0

@meboz:提供您的XSLT以獲得響應。如果你沒有任何XSLT,請他人爲你做這件事不會讓你多... – jro 2009-10-20 23:05:52

+0

@jro:我不會問,如果我沒有給它一個去。 對於第一個系列中的每個數據點,我不知道如何索引到第二個系列中的數據點。 – boz 2009-10-21 00:44:38

回答

1

一個更自然的解決方案:

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
> 
    <!-- basic table structure --> 
    <xsl:template match="Chart"> 
    <table> 
     <thead> 
     <xsl:apply-templates select="Chart.Series" mode="thead" /> 
     </thead> 
     <tbody> 
     <xsl:apply-templates select="Chart.Series" mode="tbody" /> 
     </tbody> 
    </table> 
    </xsl:template> 

    <!-- table head --> 
    <xsl:template match="Chart.Series" mode="thead"> 
    <tr> 
     <th /> 
     <xsl:for-each select="DataSeries"> 
     <th> 
      <xsl:value-of select="@LegendText" /> 
     </th> 
     </xsl:for-each> 
    </tr> 
    </xsl:template> 

    <!-- table body --> 
    <xsl:template match="Chart.Series" mode="tbody"> 
    <xsl:variable name="ds" select="DataSeries" /> 

    <!-- the first data series contains the labels --> 
    <xsl:for-each select="$ds[1]/*/DataPoint"> 
     <xsl:variable name="pos" select="position()" /> 
     <tr> 
     <td> 
      <xsl:value-of select="@AxisXLabel" /> 
     </td> 
     <!-- process all data points at the current position --> 
     <xsl:apply-templates select="$ds/*/DataPoint[$pos]" /> 
     </tr> 
    </xsl:for-each> 
    </xsl:template> 

    <!-- data points become a <td> --> 
    <xsl:template match="DataPoint"> 
    <td> 
     <xsl:value-of select="@YValue" /> 
    </td> 
    </xsl:template> 
</xsl:stylesheet> 

請注意,我用的模板模式做不同的事情有相同的輸入。

結果是:

<table> 
    <thead> 
    <tr> 
     <th /> 
     <th>Complete On Time</th> 
     <th>Complete Overdue</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td>Sep 09</td> 
     <td>10</td> 
     <td>1</td> 
    </tr> 
    <tr> 
     <td>Oct 09</td> 
     <td>11</td> 
     <td>2</td> 
    </tr> 
    <tr> 
     <td>Nov 09</td> 
     <td>12</td> 
     <td>3</td> 
    </tr> 
    </tbody> 
</table> 
+0

少就是多!不錯的工作 – boz 2009-10-21 21:12:39

0

這是一些接近。雖然我已經完成了XSLT,但對我來說已經過了幾年了 - 忽略糟糕的風格。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
<xsl:output indent="yes" omit-xml-declaration="yes" ></xsl:output> 
<xsl:template match="/"> 
    <table> 
     <thead> 
       <tr> 
         <th></th> 
         <xsl:apply-templates select=".//DataSeries" /> 
       </tr> 
     </thead> 
     <tbody> 
      <xsl:apply-templates select="//DataSeries[1]//DataPoint"> 
       <xsl:with-param name="datablock">1</xsl:with-param> 
      </xsl:apply-templates> 
     </tbody> 
    </table> 
</xsl:template> 
<xsl:template match="DataSeries"> 
    <th> 
     <xsl:value-of select="@LegendText"></xsl:value-of> 
    </th> 
</xsl:template> 
<xsl:template match="DataPoint"> 
    <xsl:param name="datablock" /> 
    <xsl:variable name="posi" select="position()"></xsl:variable> 
    <xsl:if test="$datablock = 1"> 
     <tr> 
      <xsl:for-each select="@*"> 
      <th> 
       <xsl:value-of select="."></xsl:value-of> 
      </th> 
     </xsl:for-each>  
     <xsl:apply-templates select="//DataSeries[$datablock+1]//DataPoint[$posi]"> 
       <xsl:with-param name="datablock" select="$datablock + 1"> 
       </xsl:with-param> 
     </xsl:apply-templates> 
     </tr> 
    </xsl:if> 
    <xsl:if test="$datablock != 1"> 
     <xsl:for-each select="@*"> 
      <th> 
       <xsl:value-of select="."></xsl:value-of> 
      </th> 
     </xsl:for-each>  
     <xsl:apply-templates select="//DataSeries[$datablock+1]//DataPoint[$posi]"> 
       <xsl:with-param name="datablock" select="$datablock + 1"> 
       </xsl:with-param> 
     </xsl:apply-templates> 
    </xsl:if> 
</xsl:template>