我有一个类似的 XML
<a>
<b>text1</b>
<b>text2</b>
<b>text3</b>
.....
....
</a>
这是需要的输出
<div>text1 text2</div>
<div>text3 text4</div>
使用 xsl foreach 可以轻松地为所有文本添加一个包装 div 或为每个文本添加单独的 div。我怎样才能完成上述要求,其中 div 需要一次添加 2 个文本?
最佳答案
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="a/b[position() mod 2 = 1]"/>
</xsl:template>
<xsl:template match="b">
<div>
<xsl:value-of select="concat(., ' ', following-sibling::b)"/>
</div>
</xsl:template>
</xsl:stylesheet>
关于xml - XSL 1.0 如何一次将 div 标签添加到两个元素/文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7771833/