2011-05-21 169 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