我不知道如何在维护内容顺序的同时在混合内容节点中添加空白。
我的XML如下所示:
<paragraph>
<p>
<keyword>First keyword</keyword>First text.
<author>First author</author>
<keyword>Second keyword</keyword>Second text.
<author>Second author</author>
<keyword>Third keyword</keyword>Third text.
<author>Third author</author>
</p>
</paragraph>
我的模板:
<xsl:template match="p" mode="readContentW">
<xsl:value-of select="."/><xsl:text> </xsl:text>
</xsl:template>
我现在的输出:
<phrase>First keywordFirst text. First authorSecond keywordSecond Text. Second authorThird keywordThird text. Third author</phrase>
我想要的输出:
<phrase>First keyword First text. First author Second keyword Second Text. Second author Third keyword Third text. Third author</phrase>
或者更好的是这样的东西:
<phrase>
<b>First keyword</b> First text. <i>First author</i>
<b>Second keyword</b> Second Text. <i>Second author</i>
<b>Third keyword</b> Third text. <i>Third author</i>
</phrase>
但我需要在关键字之后和作者之前的空白。
我尝试过在<phrase>
节点后通过<xsl:text> </xsl:text>
手动添加空白,但我不知道如何在保持内容顺序的同时做到这一点
XML可以有任意数量的短语/文本/作者组合,所以通过手动添加空白,我必须重新组合这个谜题,但如果没有任何循环,又如何呢?
如果在创建文本输出时转换元素并添加空格,则插入空格很容易:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<phrase>
<xsl:apply-templates/>
</phrase>
</xsl:template>
<xsl:template match="keyword">
<b>
<xsl:apply-templates/>
</b>
</xsl:template>
<xsl:template match="author">
<i>
<xsl:apply-templates/>
</i>
</xsl:template>
<xsl:template match="p/text()[preceding-sibling::node()[1][self::keyword]]">
<xsl:value-of select="concat(' ', .)"/>
</xsl:template>
</xsl:transform>
您需要进一步解释First text.
末尾的换行符为什么或何时转换为空格,以便我们将其作为模板来实现。