您可以在这里使用一种叫做Muenchian grouping的技术。在这种情况下,你可以按下面这些lb
元素的数量p
元素的子节点
<xsl:key name="p-nodes" match="tei:p/node()" use="concat(generate-id(..), '|', count(following-sibling::tei:lb))" />
要获得各组的第一个节点,这将表示要输出的,你会每l
选择它们是这样的...
<xsl:for-each
select="node()[generate-id() = generate-id(key('p-nodes', concat($parentId, '|', count(following-sibling::tei:lb)))[1])]">
,并输出<l>
标签本身和组的内容,再次用钥匙...
<l><xsl:apply-templates select="key('p-nodes', concat($parentId, '|', count(following-sibling::tei:lb)))[not(self::tei:lb)]" /></l>
试试这个XSLT(显然改变了命名空间为tei
前缀匹配你的XML真钞)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:tei="tei">
<xsl:output method="xml" indent="yes" />
<xsl:key name="p-nodes" match="tei:p/node()" use="concat(generate-id(..), '|', count(following-sibling::tei:lb))" />
<xsl:template match="tei:div/tei:p">
<lg>
<xsl:variable name="parentId" select="generate-id()" />
<xsl:for-each select="node()[generate-id() = generate-id(key('p-nodes', concat($parentId, '|', count(following-sibling::tei:lb)))[1])]">
<l><xsl:apply-templates select="key('p-nodes', concat($parentId, '|', count(following-sibling::tei:lb)))[not(self::tei:lb)]" /></l>
</xsl:for-each>
</lg>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
在http://xsltransform.net/gWEamMf
嗨。非常感谢@ tim-c :)我想我还有很多要学习xslt。 –
你可能也想看看michael.hor257K的回答。它更整洁,更高效。 –