如何通过将第一个子节点(-entry)对它们进行分组,然后将它们从a到z进行排序,将多个XML结构与XSLT 2.0和XSL-FO合并?
详细说明如下:
<keyword>
<key-entry>example<key-entry>
<key-entry>hello</key-entry>
<key-entry>world</key-entry>
</keyword>
<keyword>
<key-entry>another example</key-entry>
<key-entry>with other data</key-entry>
</keyword>
<keyword>
<key-entry>example</key-entry>
<key-entry>nice to see you!</key-entry>
</keyword>
这样的事情:
<fo:block>A</fo:block>
<fo:block>another example</fo:block>
<fo:block>with other data</fo:block>
<fo:block>E</fo:block>
<fo:block>example</fo:block>
<fo:block>hello</fo:block>
<fo:block>world</fo:block>
<fo:block>nice to see you!</fo:block>
答案 0 :(得分:2)
由于您使用的是XSLT 2.0,因此可以使用xsl:for-each-group
。见http://www.w3.org/TR/xslt20/#grouping
您需要两个xsl:for-each-group
级别:一个用于获取首字母,另一个用于按key-entry
中的第一个keyword
分组。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output indent="yes" />
<xsl:template match="keywords">
<fo:block>
<xsl:for-each-group
select="keyword"
group-by="upper-case(substring(key-entry[1], 1, 1))">
<xsl:sort select="current-grouping-key()" />
<fo:block>
<xsl:value-of select="current-grouping-key()" />
</fo:block>
<xsl:for-each-group
select="current-group()"
group-by="key-entry[1]">
<fo:block>
<xsl:value-of select="current-grouping-key()" />
</fo:block>
<xsl:apply-templates
select="current-group()/key-entry[position() > 1]" />
</xsl:for-each-group>
</xsl:for-each-group>
</fo:block>
</xsl:template>
<xsl:template match="key-entry">
<fo:block>
<xsl:apply-templates />
</fo:block>
</xsl:template>
</xsl:stylesheet>
产生
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:block>A</fo:block>
<fo:block>another example</fo:block>
<fo:block>with other data</fo:block>
<fo:block>E</fo:block>
<fo:block>example</fo:block>
<fo:block>hello</fo:block>
<fo:block>world</fo:block>
<fo:block>nice to see you!</fo:block>
</fo:block>