2013-10-18 89 views
0

你能幫我調試一下找出錯誤的來源嗎?
我想找到XML中的兩個元素的平均值,並將其添加到最終的XML。
我可能會做一些根本性的錯誤,請你幫忙。xsl:xml元素的平均值

在此先感謝。 fornula S =(C + d)/ 2

xml文檔

<?xml version="1.0" encoding="UTF-8"?> 
<top> 
    <Results> 
     <a>no</a> 
     <b>12</b> 
     <c>12</c> 
     <d>9</d> 
    </Results> 
    <Results> 
     <a>yes</a> 
     <b>8</b> 
     <c>50</c> 
     <d>12</d> 
    </Results> 
    <Results> 
     <a>no</a> 
     <b>6</b> 
     <c>55</c> 
     <d>56</d> 
    </Results> 
    <Results> 
     <a>yes</a> 
     <b>23</b> 
     <c>32</c> 
     <d>34</d> 
    </Results> 
</top> 

XSL文件

<xsl:output method="xml" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

    <xsl:variable name="x" select="//c"/> 
    <xsl:variable name="y" select="//d"/> 

<xsl:template match="b"> 
    <xsl:call-template name="identity"/> 
    <s> 
    <xsl:value-of select="($x + $y) div 2"/> 
    </s> 
</xsl:template> 
</xsl:stylesheet> 

期望輸出

<?xml version="1.0"?> 
<top> 
    <Results> 
    <a>no</a> 
    <b>12</b> 
    <s>10.5</s> 
    <c>12</c> 
    <d>9</d> 
    </Results> 
    <Results> 
    <a>yes</a> 
    <b>8</b> 
    <s>31</s> 
    <c>50</c> 
    <d>12</d> 
    </Results> 
    <Results> 
    <a>no</a> 
    <b>6</b> 
    <s>55.5</s> 
    <c>55</c> 
    <d>56</d> 
    </Results> 
    <Results> 
    <a>yes</a> 
    <b>23</b> 
    <s>33</s> 
    <c>32</c> 
    <d>34</d> 
    </Results> 
</top> 

上面的代碼和輸入是一個模擬th的例子e真實數據集。因此,我正在尋找識別代碼中的錯誤,這是更大代碼的一部分。 任何幫助將高度appriciated。

回答

2

你在想太複雜。

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

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

    <xsl:template match="Results"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()" /> 
     <s><xsl:value-of select="(sum(c) + sum(d)) div 2" /></s> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

如果您在<Results>元素堅持的孩子才能上,使用

<xsl:template match="Results"> 
    <xsl:copy> 
    <xsl:apply-templates select="@* | a | b" /> 
    <s><xsl:value-of select="(sum(c) + sum(d)) div 2" /></s> 
    <xsl:apply-templates select="c | d" /> 
    </xsl:copy> 
</xsl:template> 

如果永遠只可能是一個<c><d>在一組,這已經足夠了:

<xsl:value-of select="(c + d) div 2" /> 
+0

我期待修復我的代碼,因爲我有大量元素和平均值需要在特定的地方插入。 – user1495523

+0

我不確定你的意思。我的解決方案將平均值插入特定位置。 – Tomalak

+0

你的解決方案當然適用於這個例子,但是在一個集合中有大約40個元素(而不是上面例子中的4個)的情況下實現會很困難。你能建議一個可以擴展的解決方案嗎?謝謝 – user1495523

0

我想我已經發現了一種解決報告問題的方法。

問候

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method="xml" indent="yes"/> 
<xsl:strip-space elements="*"/> 


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

<xsl:template match="b"> 
    <xsl:call-template name="identity"/> 
    <xsl:variable name="x" select="../c"/> 
    <xsl:variable name="y" select="../d"/> 
    <s><xsl:value-of select="($x + $y) div 2"/></s> 
</xsl:template> 
</xsl:stylesheet> 
+1

[您的解決方案](http://www.xmlplayground.com/2bCK27)和[我的解決方案](http://www.xmlplayground.com/N3O7I6)產生完全相同的輸出。你能解釋你爲什麼認爲你的方法更好(或者我的情況更糟)嗎? – Tomalak