views:

154

answers:

1

Let's say I have a line in an XML file like the following (derived from an HTML file):

<i>This is a nice poem<br/>It doesn't rhyme<br/>because right now<br/>I don't have time</i>

I am attempting to right an XSLT to split this string with the following output:

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

I have found numerous examples on how to split this if it were delimited with some sort of text, but because it it delimited with an actual element tag, I am really getting stuck. Anyone have any thoughts?

+3  A: 

There is no need to split. You have a series of nodes that are children of <i>, some of them (the text nodes) you are interested in, some of them not (the <br> nodes).

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

The <br> nodes do not appear in the output because you do not handle them - you are applying templates to the text nodes only (note that text() selects text nodes).

The <br> still fulfill an important function - they are the ones that delimit your text nodes, effectively doing the "split" for you.

Tomalak
Thank you! That makes perfect sense. And thank you for explaining it as well :) that is always very helpful.
Tim C
Glad to help. :)
Tomalak