我有一个人员列表,我想用每个人的名字对它们进行分组,然后检查哪个<vitalDates>
节点有值,并将其放在分组<person>
节点中。
<persons>
<person>
<name>Goethe, Johann Wolfgang von </name>
<vitalDates>
<earliest/>
<latest/>
</vitalDates>
</person>
<person>
<name>Müthel, Lothar</name>
<vitalDates>
<earliest/>
<latest/>
</vitalDates>
</person>
<person>
<name>Goethe, Johann Wolfgang von</name>
<vitalDates>
<earliest>1749</earliest>
<latest>1832</latest>
</vitalDates>
</person>
<person>
<name>Müthel, Lothar</name>
<vitalDates>
<earliest>1896</earliest>
<latest>1964</latest>
</vitalDates>
</person>
<person>
<name>Goethe, Johann Wolfgang von</name>
<vitalDates>
<earliest>1749</earliest>
<latest>1832</latest>
</vitalDates>
</person>
</persons>
此刻我有这种转变:
<persons>
<xsl:for-each-group select="person" group-by="name">
<person>
<name>
<xsl:value-of select="current-grouping-key()"/>
</name>
<vitalDates>
<earliest>
<xsl:for-each select="/persons/person[name=current-grouping-key()]/vitalDates/earliest">
<xsl:if test=". != ''">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</earliest>
<latest>
<!-- do the same? -->
</latest>
</vitalDates>
</person>
</xsl:for-each-group>
</persons>
我得到了这个结果:
<persons>
<person>
<name>Goethe, Johann Wolfgang von</name>
<vitalDates>
<earliest>17491749</earliest>
<latest></latest>
</vitalDates>
</person>
<person>
<name>Müthel, Lothar</name>
<vitalDates>
<earliest></earliest>
<latest></latest>
</vitalDates>
</person>
</persons>
就像你可以看到<earliest>
元素的值被发现两次,但我只需要使用一个值,我不知道如何。
答案 0 :(得分:1)
首先,当您使用for-each-group
而不是/persons/person[name=current-grouping-key()]
时,您可以而且应该只使用current-group()
。
至于选择第一项,请使用(current-group()/vitalDates/earliest[normalize-space()])[1]
。