有可能在我的邏輯瑕疵的地方,但試試這個XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:param name="firstYear" select="2012" />
<xsl:param name="lastYear" select="2015" />
<xsl:param name="times" select="2" />
<xsl:variable name="Years" select="$lastYear - $firstYear + 1" />
<xsl:key name="Trip" match="Trip" use="concat(@Year, '|', ../../@UD)" />
<xsl:template match="Customers">
<xsl:copy>
<xsl:apply-templates select="Customer[count(.//Trip
[@Year >= $firstYear and @Year <= $lastYear]
[generate-id() = generate-id(key('Trip', concat(@Year, '|', ../../@UD))[1])]
[count(key('Trip', concat(@Year, '|', ../../@UD))) >= $times]
) = $Years]" />
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
對每一個客戶,這得到了一年的範圍之間不同的出行次數,(所以對於一個爲期三年的範圍,例如,那麼你需要三個不同的年份,否則這意味着他們每年都沒有旅行),並且每個不同的年份都計算他們在一年中旅行的次數,以確保它符合要求。如果此類行程的總次數等於所需的年數,則可輸出該客戶。
** 1。** XML中只有一個客戶。 - ** 2。**「最近X年每年X次以上」的條件含糊不清:這可能意味着在過去Y年的每一次**中X次以上,或者在過去的Y年中平均每年**超過X次**。 –
同意,XML中只有一個客戶,但在根元素下會有更多。我希望得到在過去的Y年中一次旅行超過X次的客戶。希望它現在更有意義。提前謝謝你的幫助。 – user2135025