您可以在这里使用一种称为Muenchian 分组的技术。p
在这种情况下,您可以按照元素的数量对lb
元素的子节点进行分组
<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上查看它的实际应用