【发布时间】:2011-03-02 07:25:46
【问题描述】:
我必须在 <xsl:for-each> 循环的上下文中从 XML 文档中仅选择唯一记录。我被 Visual Studio 限制为使用 XSL 1.0。
<availList>
<item>
<schDate>2010-06-24</schDate>
<schFrmTime>10:00:00</schFrmTime>
<schToTime>13:00:00</schToTime>
<variousOtherElements></variousOtherElements>
</item>
<item>
<schDate>2010-06-24</schDate>
<schFrmTime>10:00:00</schFrmTime>
<schToTime>13:00:00</schToTime>
<variousOtherElements></variousOtherElements>
</item>
<item>
<schDate>2010-06-25</schDate>
<schFrmTime>10:00:00</schFrmTime>
<schToTime>12:00:00</schToTime>
<variousOtherElements></variousOtherElements>
</item>
<item>
<schDate>2010-06-26</schDate>
<schFrmTime>13:00:00</schFrmTime>
<schToTime>14:00:00</schToTime>
<variousOtherElements></variousOtherElements>
</item>
<item>
<schDate>2010-06-26</schDate>
<schFrmTime>10:00:00</schFrmTime>
<schToTime>12:00:00</schToTime>
<variousOtherElements></variousOtherElements>
</item>
</availList>
唯一性必须基于三个子元素的值:schDate、schFrmTime 和 schToTime。如果两个item 元素对于所有三个子元素具有相同的值,则它们是重复的。在上述 XML 中,第一项和第二项是重复项。其余的都是独一无二的。如上所述,每个项目都包含我们不希望包含在比较中的其他元素。 “独特性”应该是这三个要素的一个因素,而且只有这些要素。
我已尝试通过以下方式完成此操作:
availList/item[not(schDate = preceding:: schDate and schFrmTime = preceding:: schFrmTime and schToTime = preceding:: schToTime)]
这背后的想法是选择前面没有相同schDate、schFrmTime 和schToTime 的元素的记录。但是,它的输出缺少最后一项。这是因为我的 XPath 实际上排除了所有子元素值在整个前面的文档中都匹配的项目。没有一个 item 匹配最后一项的所有子元素 - 但由于每个元素的值单独存在于另一个项中,所以最后一项被排除在外。
通过将所有子值作为连接字符串与前面每个项目的相同连接值进行比较,我可以获得正确的结果。有人知道我可以这样做吗?
【问题讨论】:
-
好问题 (+1)。请参阅我对 XPath 和 XSLT 解决方案的回答。
-
使用key()的方法通常称为Muenchian方法:jenitennison.com/xslt/grouping/muenchian.html