xmlxslttextnewlinexmlconvert

XML to Text transform using XSLT with new line


I looked at multiple examples and gave each on them a try. Not sure what am I missing. The only difference I found from other examples was that I have multiple <Line> nodes under <RecordSet>.

XML:

<?xml version="1.0" encoding="utf-8"?>
<urn:FlatStructure">
  <Recordset>
    <Line> 12345678</Line>
    <Line> abcdefgh</Line>
  </Recordset>
</urn:FlatStructure>

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />

<!-- First Trial  -->

<xsl:variable name="newline"><xsl:text>
</xsl:text></xsl:variable>

<xsl:template match="/urn:FlatStructure/RecordSet">
   <xsl:value-of select="concat(Line,$newline)" />
</xsl:template> 

<!-- Second Trial  -->
<xsl:template match="/urn:FlatStructure">
  <xsl:apply-templates select="RecordSet/Line" />
</xsl:template>

<!-- Third Trial  -->
<xsl:template match="/urn:FlatStructure">
<xsl:value-of select="concat(Line,'&#10;')" />
</xsl:template>

</xsl:stylesheet>

Current text output:

12345678 abcdefgh

Desired text output :

12345678
abcdefgh

What am I missing in the XSLT ? Please let me know how can I correct it.

Thanks

I looked at the following examples (some may be duplicates) but none of them worked for me :

XSLT to convert XML to text

Producing a new line in XSLT

how to add line breaks at the end of an xslt output?

not adding new line in my XSLT


Solution

  • Found the solution. Looping through each <Line> node under <Recordset> and selecting the text.

    XSLT that worked:

    <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:urn="someurn">
    
    <xsl:output method="text" />
    
    <xsl:template match="/urn:FlatStructure/Recordset">
        <xsl:for-each select="Line">
            <xsl:value-of select="text()"/>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
     </xsl:template>
    
    </xsl:stylesheet>
    

    Multiple child nodes with the same name seemed to be the problem.

    Thanks everyone for pitching in.

    Cheers !