我有一個XML結構如下:XSLT 2.0:迭代,直到元件
<document>
<text>Here is some text </text>
<bold>and this part is in bold</bold>
<text> and this part is not.</text>
<newline />
<bold>foo foo foo</bold>
<newline />
<newline />
<text>bar bar bar</text>
</document>
我想編寫XSLT變換,其產生輸出這樣的:
<document>
<line>Here is some text <b>and this part is in bold</b> and this part is not.</line>
<line><b>foo foo foo</b></line>
<blankline>
<line>bar bar bar</line>
</document>
我覺得這是一個問題,需要一個循環結構的文檔/ *直到下列兄弟:name()='Newline',但我不能完全弄清楚如何構造它。
在這裏,我想嘗試寫算法:
<xsl:template match="document">
<xsl:apply-templates select="*[1]"/>
</xsl:template>
<xsl:template match="text | bold">
<line>
<xsl:if test="name() = 'text'>
<xsl:value-of select="." />
</xsl:if>
<xsl:if test="name() = 'bold'>
<b><xsl:value-of select="." /></b>
</xsl:if>
<!-- I want a loop which runs over all following 'text' or 'bold' elements, but stops before the next Newline is reached -->
<xsl:for-each select="preceding-sibling::*[self:newline]">
<xsl:if test="name() = 'text'>
<xsl:value-of select="." />
</xsl:if>
<xsl:if test="name() = 'bold'>
<b><xsl:value-of select="." /></b>
</xsl:if>
</xsl:for-each>
<!-- I then want to apply-templates on the next item (the newline) -->
<xsl:apply-templates select="following-sibling" />
</line>
</xsl:template>
<xsl:template match="newline">
<!-- i need to work out how to ignore single <newline> here but output <blankline> if more than one <newline /> is together. -->
<xsl:apply-templates select="following-sibling" />
</xsl:template>
非常感謝你,我是不是意識到'xsl:for-each-group'中的'group-ending-with'屬性,但是這個功能完美無缺。 –