2011-05-21 115 views
4

我有XML:XSL和兄弟姐妹值

<people> 
    <man age="20" /> 
    <man age="40" /> 
    <man age="30" /> 
    <man age="80" /> 
<people> 

使用XSL我試圖輸出:

first age:20 
first and second age (combined): 60 
first second and third age(combined) :110 
first second third and fouth age(combined) :190 

我知道如何選擇的年齡,但我怎麼把它們加起來,寫出來?

還要注意,<man>元素可不止是4

+0

是 「第一時代」, 「第一和第二年齡(組合)」 所需輸出的部分等等?如果是的話,我認爲這會很困難。僅僅一小部分的款項應該是可行的(儘管我現在沒有一個方便的解決方案)。 – musiKk 2011-05-21 10:31:38

+0

@musiKk:我只需要值,而不是文本。 – 2011-05-21 11:43:29

+0

好問題,+1。查看我的答案,獲取完整的,非常短的非遞歸解決方案。 :) – 2011-05-21 17:09:09

回答

6

還行,我只是看你剛纔所需要的數字,所以下面剝離XSLT

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

    <xsl:variable name="elements" select="/people/man"/> 
    <xsl:variable name="count" select="count($elements)"/> 

    <!-- Main Entry point --> 
    <xsl:template match="/"> 
     <xsl:call-template name="addthem"> 
      <xsl:with-param name="pos" select="1"/> 
      <xsl:with-param name="sum" select="$elements[1]/@age"/> 
     </xsl:call-template> 
    </xsl:template> 

    <!-- recursive calling template to sum up the ages --> 
    <xsl:template name="addthem"> 
     <xsl:param name="pos"/> 
     <xsl:param name="sum"/> 

     <xsl:value-of select="$sum"/> 
     <xsl:text> 
</xsl:text> 

     <!-- recursive call to sum up the ages --> 
     <xsl:if test="$pos lt number($count)"> 
      <xsl:call-template name="addthem"> 
       <xsl:with-param name="pos" select="$pos + 1"/> 
       <xsl:with-param name="sum" select="number($sum) + number($elements[$pos + 1]/@age)"/> 
      </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

產生你的樣品以下輸入 -

20 
60 
90 
170 

模板(原始一個帶有標籤和東西):

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

    <xsl:variable name="txtlabels" select="tokenize('first,second,third,fourth,fifth,sixth,seventh,eights,ninth,tenth,eleventh,twelveth,thirteenth,fourteenth,fifteenth', ',')"/> 

    <!-- Util template to generate labels --> 
    <xsl:template name="getlabel"> 
     <xsl:param name="startat" select="1"/> 
     <xsl:param name="idx"/> 

     <xsl:if test="number($startat) lt number($idx)"> 
      <xsl:value-of select="$txtlabels[$startat]"/> 
      <xsl:text> </xsl:text> 
      <xsl:call-template name="getlabel"> 
       <xsl:with-param name="startat" select="$startat + 1"/> 
       <xsl:with-param name="idx" select="$idx"/> 
      </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 


    <!-- Main Entry point --> 
    <xsl:template match="/"> 
     <xsl:variable name="count"> 
      <xsl:value-of select="count(/people/man)"/> 
     </xsl:variable> 

     <xsl:call-template name="addthem"> 
      <xsl:with-param name="count" select="count(/people/man)"/> 
      <xsl:with-param name="pos" select="1"/> 
      <xsl:with-param name="sum" select="/people/man[1]/@age"/> 
      <xsl:with-param name="elements" select="/people/man"/> 
     </xsl:call-template> 
    </xsl:template> 

    <!-- recursive calling template to sum up the ages --> 
    <xsl:template name="addthem"> 
     <xsl:param name="count"/> 
     <xsl:param name="pos"/> 
     <xsl:param name="sum"/> 
     <xsl:param name="elements"/> 

     <!-- get the label prefix, without the 'and' clause --> 
     <xsl:variable name="thelabelprefix"> 
      <xsl:call-template name="getlabel"> 
       <xsl:with-param name="startat" select="1"/> 
       <xsl:with-param name="idx" select="$pos"/> 
      </xsl:call-template> 
     </xsl:variable> 

     <!-- Now append the 'and' clause, if required, to the labels!!! --> 
     <xsl:variable name="thelabel"> 
      <xsl:choose> 
       <xsl:when test="number($pos) eq 1"> 
        <xsl:value-of select="$txtlabels[$pos]"/> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of 
select="concat($thelabelprefix, ' and ', $txtlabels[$pos])"/> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:variable> 

     <xsl:value-of select="$thelabel"/> 
     <xsl:text> : </xsl:text> <xsl:value-of select="$sum"/> 
     <xsl:text> 

     </xsl:text> 

     <!-- recursive call to sum up the ages --> 
     <xsl:if test="$pos lt number($count)"> 
      <xsl:call-template name="addthem"> 
       <xsl:with-param name="count" select="$count"/> 
       <xsl:with-param name="pos" select="$pos + 1"/> 
       <xsl:with-param name="sum" select="number($sum) + number($elements[$pos + 1]/@age)"/> 
       <xsl:with-param name="elements" select="$elements"/> 
      </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

產生以下爲您輸入XML輸出:

first : 20 
first and second : 60 
first second and third : 90 
first second third and fourth : 170 

我已經添加評論裏面,讓我知道如果你需要進一步的幫助。 它基本上使用了兩個遞歸模板,一個用於'標籤',另一個用於添加。

而且,你的樣本輸出應改爲90和170,而不是110和190或您的樣品輸入應該說年齡= 50代替年齡= 30

+0

哇,可能是我見過的最完整的答案... ... thanx,我很感激。 – 2011-05-21 15:57:04

1

一個簡單的,非遞歸的解決方案,是適用於增量的兄弟元素小序列的總和:

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

<xsl:template match="man"> 
    <xsl:value-of select= 
    "sum(@age|preceding-sibling::man/@age)"/> 
    <xsl:text>&#xA;</xsl:text> 
</xsl:template> 
</xsl:stylesheet> 

當施加到所提供的XML文檔(校正爲進行良好的形成):

<people> 
    <man age="20" /> 
    <man age="40" /> 
    <man age="30" /> 
    <man age="80" /> 
</people> 

的希望,正確的結果產生

20 
60 
90 
170 

II。對於巨大的序列(節點集)的簡單而有效的解決方案如下,利用FXSLscanl模板/功能:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:f="http://fxsl.sf.net/" 
xmlns:myAdd="f:myAdd" xmlns:myParam="f:myParam" 
> 
    <xsl:import href="scanl.xsl"/> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 

    <myAdd:myAdd/> 

    <myParam:myParam>0</myParam:myParam> 

    <xsl:template match="/"> 

    <xsl:variable name="vFun" select="document('')/*/myAdd:*[1]"/> 
    <xsl:variable name="vZero" select="document('')/*/myParam:*[1]"/> 


    <xsl:call-template name="scanl"> 
     <xsl:with-param name="pFun" select="$vFun"/> 
     <xsl:with-param name="pQ0" select="$vZero" /> 
     <xsl:with-param name="pList" select="/*/*/@age"/> 
    </xsl:call-template> 
    </xsl:template> 

    <xsl:template match="myAdd:*" mode="f:FXSL"> 
    <xsl:param name="pArg1" select="0"/> 
    <xsl:param name="pArg2" select="0"/> 

    <xsl:value-of select="$pArg1 + $pArg2"/> 
    </xsl:template> 
</xsl:stylesheet> 

開箱即用這個簡單的解決方案(只需要調用一個模板 - 無遞歸代碼寫)產生通緝的結果

<el>0</el> 
<el>20</el> 
<el>60</el> 
<el>90</el> 
<el>170</el> 
+0

即將回來,並說我管理了一個較短的版本,我的解決方案也是「總和之前的兄弟姐妹」。因爲他的例子讓我進一步挖掘(iv'e把你的答案投給了你) – 2011-05-21 17:36:06

3

以下簡短的樣式表生成了您首先要求的輸出,包括序號:

樣式表:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" 
    version="2.0"> 

<xsl:output method="text"/> 

<xsl:template match="/" > 
    <xsl:for-each select="people/man"> 
     <xsl:for-each select=".|preceding-sibling::man"> 
     <xsl:value-of select="if (position() = last() and last() != 1) 
           then ' and ' else ' '"/> 
     <xsl:number format="w" ordinal="yes"/> 
     </xsl:for-each> 
     <xsl:text> age </xsl:text> 
     <xsl:if test="position() > 1">(combined)</xsl:if> 
     <xsl:value-of select="':', sum((.|preceding-sibling::man)/@age), '&#xa;'"/> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 

輸出:

first age : 20 
first and second age (combined): 60 
first second and third age (combined): 90 
first second third and fourth age (combined): 170 
+1

'ordinal =「yes」'+1的好用處。 – 2011-05-21 21:14:03