2017-10-11 112 views
2

我有一个XML文件等如下:XSL:计数连续属性

<Query> 
<Rows> 
    <Row Pd="1"></Row> 
    <Row Pd="1"></Row> 
    <Row Pd="0"></Row> 
    <Row Pd="0"></Row> 
    <Row Pd="0"></Row> 
    <Row Pd="0"></Row> 
    <Row Pd="1"></Row> 
    <Row Pd="1"></Row> 
    <Row Pd="1"></Row> 
    <Row Pd="1"></Row> 
    <Row Pd="1"></Row> 
    <Row Pd="1"></Row> 
    <Row Pd="1"></Row> 
    <Row Pd="0"></Row> 
    <Row Pd="0"></Row> 
    <Row Pd="0"></Row> 
    <Row Pd="1"></Row> 
    <Row Pd="1"></Row> 
    <Row Pd="1"></Row> 
</Rows> 
</Query> 

这是用“PD”属性项的基本上的有序列表(值= 0或1)。 我需要显示的结果如下:

  1. 用Pd = 1的行的总数,
  2. 用Pd = 1(0,如果最新的Pd = 0)
  3. 最大数目最新连续的行的用PD =

连续行的意甲我设法得到1和2,但未能达到3

我1 & 2(XSLT 1.0)溶液:

<xsl:output method="html" indent="yes"/> 
<xsl:template match="/"> 
    <div> 
     <h1>total # of Pd1 : <xsl:value-of select="sum(//Row/@Pd)"/></h1> 
     <h1># of consecutive Pd1: <xsl:apply-templates/> </h1> 
    </div> 
</xsl:template> 
<xsl:template match="//Row[not(following-sibling::Row[1]/@Pd=self::Row/@Pd)][1]"> 
    <xsl:value-of select="sum(self::Row[1]/@Pd|preceding-sibling::Row/@Pd)"/> 
</xsl:template> 
</xsl:stylesheet> 

对于上面引述的XML,预期的结果应该是:

欢迎提供针对问题3的解决方案的任何帮助(以及对提供的其他解决方案的改进)

+0

您可以使用XSLT 2.0和这样'XSL :for-each-group select =“// Row”group-adjacent =“@ Pd”'? –

+0

不幸的是,只有XSLT 1.0 ...即使分组是一回事,在这些组中获得最长的序列也不会那么容易。 – Tang

回答

1

以下是XSLT 1.0中执行第3个操作的一种方法,但它不会令人愉快(特别是因为XSLT 1.0没有max命令)。首先定义像这样的关键...

<xsl:key name="rows" match="Row" use="count(preceding-sibling::Row[@Pd != current()/@Pd])" /> 

然后,为了获得最大的系列PD =连续行的,你可以做到这一点

<h1> 
    <xsl:text>Largest serie of consecutive rows with Pd=1: </xsl:text> 
    <xsl:for-each select="//Row[@Pd='1']"> 
     <xsl:sort select="count(key('rows', count(preceding-sibling::Row[@Pd != current()/@Pd]))[@Pd='1'])" order="descending" /> 
     <xsl:if test="position() = 1"> 
      <xsl:value-of select="count(key('rows', count(preceding-sibling::Row[@Pd != current()/@Pd]))[@Pd='1'])" /> 
     </xsl:if> 
    </xsl:for-each> 
</h1> 
+0

你可以编辑你的问题来显示一个示例输入,它不起作用吗?我在http://xsltransform.net/a9GixJ测试了你当前的输入,并且按照预期测试了总共7个。感谢 –

+0

我刚测试过它。对于提供的系列(1100001111111000111) - > 7,输出正常。但是,对于不同的数据集,结果是错误的:(1100001110111000111) - > 6(应该是3)。我没有足够的流畅的xpath语法来解释为什么... – Tang

+0

我已经纠正了这个问题。它失败了,因为它也包含了0的最大长度。 –