代码之家  ›  专栏  ›  技术社区  ›  Tim C

XSL使用空元素拆分字符串

  •  1
  • Tim C  · 技术社区  · 15 年前

    <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>
    

    1 回复  |  直到 15 年前
        1
  •  3
  •   Tomalak    15 年前

    没有必要分开。有一系列节点是的子节点 <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>