0

I have this xml input (it is a proper xml, I previously add the root tag to use the xslt).

<root>
Line 1
Line 2
Line 3</root>

And I need this output.

<root>
<line>Line 1</line>
<line>Line 2</line>
<line>Line 3</line></root>

I'm using this xslt but is producing one single line with all the three values in it.

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" indent="yes"/<xsl:template match="root"><xsl:copy><xsl:apply-templates select="text()[normalize-space()]"/></xsl:copy></xsl:template><xsl:template match="text()"><line>
  <xsl:value-of select="normalize-space()"/></line></xsl:template></xsl:stylesheet>

How can I fix this to achieve the expected output?

1 Answers1

0

My suggestion from the comment slightly improved and morphed into a complete snippet can be tested at this fiddle and is e.g.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:output indent="yes"/>
  
  <xsl:template match="root">
    <xsl:copy>
      <xsl:for-each select="tokenize(., '&#10;')[normalize-space()]"><line><xsl:value-of select="."/></line></xsl:for-each>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>

and outputs

<root>
   <line>Line 1</line>
   <line>Line 2</line>
   <line>Line 3</line>
</root>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110