2014-06-21 29 views
-1

所有提交的數據這是我的XSLT:充分利用XML通過XSLT

<?xml version="1.0" encoding="UTF-8" ?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="xml" indent="yes"/> 

<xsl:template match="/"> 
<graph> 
    <categories> 
     <category> 
     <xsl:attribute name="name"> 
      <xsl:value-of select="Rows/row/@Date" /> 
     </xsl:attribute> 
     </category>  
    </categories> 

    <dataset> 
     <xsl:attribute name="seriesName"> 
     <xsl:value-of select="Rows/row/@Actual_Amount"/> 
     </xsl:attribute> 
    </dataset> 

    <dataset> 
    <xsl:attribute name="seriesName"> 
     <xsl:value-of select="Rows/row/@Threshold"/> 
    </xsl:attribute> 
    </dataset> 

    <dataset> 
    <xsl:attribute name="seriesName"> 
     <xsl:value-of select="Rows/row/@Forecast_Amount"/> 
    </xsl:attribute> 
    </dataset> 

</graph> 
</xsl:template> 

這是我的結果:

<graph> 
<categories> 
    <category name="2014-01-01"/> 
</categories> 
<dataset seriesName="1800.0000"/> 
<dataset seriesName="500.0000"/> 
<dataset seriesName="2800.0000"/> 
</graph> 

誰能告訴我爲什麼我只得到了我的許多XML的第一個數據請? 我也嘗試過<xsl:value-of select="."/> ......但只有我的第一個數據生成。請幫忙。

+0

請顯示您的輸入XML。 – helderdarocha

+0

和您的*預期*輸出。你期望幾個'graph'元素? – helderdarocha

+0

你好!謝謝你回覆我... –

回答

0

我猜你的XML看起來是這樣的:

<Rows> 
    <row Date="2014-01-01" Actual_Amount="1800.0000" Threshold="500.0000" Forecast_Amount="2800.0000" /> 
    <row Date="2014-01-02" Actual_Amount="1850.0000" Threshold="550.0000" Forecast_Amount="2850.0000" /> 
</Rows> 

,因爲你的「文檔節點」

<xsl:template match="/"> 

匹配開始你只得到第一個數據這將只只需調用一次,所以模板中的所有代碼只會被輸出一次。此外,如果你這樣做

<xsl:value-of select="Rows/row/@Date" /> 

聲明價值的將只返回它找到匹配,不管有多少行是第一個節點的值。

你可能想改變你的第一個模板,只是這個

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

然後改變你現有的模板匹配,而不是...

<xsl:template match="row"> 
    <graph> 
     ... 

試試這個XSLT

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

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

<xsl:template match="row"> 
    <graph> 
     <categories> 
      <category name="{@Date}" /> 
     </categories> 
     <dataset seriesName="{@Actual_Amount}"/> 
     <dataset seriesName="{@Threshold}"/> 
     <dataset seriesName="{@Forecast_Amount}"/> 
    </graph> 
</xsl:template> 
</xsl:stylesheet> 

請注意使用屬性值模板在輸出名稱seriesName屬性。花括號{ }表示要評估的表達式,而不是字面輸出。

還要注意的是匹配的XPath表達式將是相對於你匹配當前節點,所以你只需要做@Date例如模板,而不是Rows\row\@Date

如果內這不完全是你想要的輸出,請編輯你的問題,以顯示你的期望,但希望這應該指向你在正確的方向。