假设我在XML文件中有一行,如下所示(从HTML文件派生):
<i>This is a nice poem<br/>It doesn't rhyme<br/>because right now<br/>I don't have time</i>
我试图让XSLT用以下输出分割这个字符串:
<stanza>
<line>This is a nice poem</line>
<line>It doesn't rhyme</line>
<line>because right now</line>
<line>I don't have time</line>
</stanza>
如果用某种文本分隔,我发现了很多关于如何分割它的例子,但是因为它用实际的元素标记分隔,我真的被卡住了。有人有什么想法吗?
答案 0 :(得分:3)
没有必要拆分。您有一系列<i>
子节点,其中一些节点(文本节点)您感兴趣,其中一些节点(<br>
节点)。
<!-- <i> turns into <stanza> -->
<xsl:template match="i">
<stanza>
<xsl:apply-templates select="text()" />
</stanza>
</xsl:template>
<!-- text nodes within <i> turn into <line> nodes -->
<xsl:template match="i/text()" />
<line><xsl:value-of select="." /></line>
</xsl:template>
<br>
节点没有出现在输出中,因为您没有处理它们 - 您只是将模板应用于文本节点(请注意text()
选择文本节点)。
<br>
仍然可以完成一项重要的功能 - 它们可以界定您的文本节点,有效地为您进行“拆分”。