2017-09-25 99 views
1

这种划分源XML:复杂的“选择”

<root> 
    <!-- a and b have the same date entries, c is different --> 
    <variant name="a"> 
    <booking> 
     <date from="2017-01-01" to="2017-01-02" /> 
     <date from="2017-01-04" to="2017-01-06" /> 
    </booking> 
    </variant> 
    <variant name="b"> 
    <booking> 
     <date from="2017-01-01" to="2017-01-02" /> 
     <date from="2017-01-04" to="2017-01-06" /> 
    </booking> 
    </variant> 
    <variant name="c"> 
    <booking> 
     <date from="2017-04-06" to="2017-04-07" /> 
     <date from="2017-04-07" to="2017-04-09" /> 
    </booking> 
    </variant> 
</root> 

我想组的三个变种,以便每个日期相同@from@to每个变种要相对集中。

我的尝试是:

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

    <xsl:template match="root"> 
    <variants> 
    <xsl:for-each-group select="for $i in variant return $i" group-by="booking/date/@from"> 
     <group> 
     <xsl:attribute name="cgk" select="current-grouping-key()"/> 
     <xsl:copy-of select="current-group()"></xsl:copy-of> 
     </group> 
    </xsl:for-each-group> 
    </variants> 
    </xsl:template> 
</xsl:stylesheet> 

但是这给了太多组。 (如何)这可能实现?

+0

你得到的输出是什么? – GavinBrelstaff

+0

那么您使用哪种XSLT 2.0处理器?使用Saxon 9.8或任何其他XSLT 3.0处理器和“复合”分组键可能会更容易。另外,具有相同'date'元素的'variant'会发生什么,但是按不同的顺序?订单是否重要? –

+0

@MartinHonnen目前我正在使用Saxon-HE 9.4.0.2J,但我可以轻松更新。 – topskip

回答

2

使用composite key和XSLT 3.0,你可以使用

<xsl:template match="root"> 
    <variants> 
     <xsl:for-each-group select="variant" group-by="booking/date/(@from, @to)" composite="yes"> 
      <group key="{current-grouping-key()}"> 
       <xsl:copy-of select="current-group()"/> 
      </group> 
     </xsl:for-each-group> 
    </variants> 
</xsl:template> 

即应集团任何variant元素结合在一起,他们同为后代date元素序列。

XSLT 3.0由Saxon 9.8(任意版本)或9.7(PE和EE)或2017版Altova XMLSpy/Raptor支持。

使用XSLT 2.0,你可以用string-join()串联所有这些日期值:与date后裔相同的序列

<xsl:template match="root"> 
    <variants> 
     <xsl:for-each-group select="variant" group-by="string-join(booking/date/(@from, @to), '|')"> 
      <group key="{current-grouping-key()}"> 
       <xsl:copy-of select="current-group()"/> 
      </group> 
     </xsl:for-each-group> 
    </variants> 
</xsl:template> 

像XSLT 3.0解决方案,它只是群体variant,我不知道这是否足够或在计算分组键之前,是否可能要先排序date后代。在XSLT 3如果你能做到这一点很容易与

 <xsl:for-each-group select="variant" group-by="sort(booking/date,(), function($d) { xs:date($d/@from), xs:date($d/@to) })!(@from, @to)" composite="yes"> 

在线(虽然留下9.8 HE背后,因为它不支持函数表达式/高阶函数,所以你需要移动的排序,以自己用户定义的xsl:function并在那里使用xsl:perform-sort)。

+0

太棒了!我在我的简单测试用例上尝试过'composite =“yes”',并且它工作正常。我会尝试将其应用于更复杂的“真实世界”数据并回报。 – topskip

+0

我希望我能给你多一个upvote!再次感谢。我非常喜欢看到其他解决方案,这些解决方案激励我阅读并尝试更多。我几次遇到XSLT/XPath 3,但从来没有一个很好的理由尝试它们。 – topskip