1

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

Community
  • 1
  • 1
  • Have you tried ` ` as in the example you link to? – PM 77-1 Mar 03 '16 at 23:00
  • The code should be fine as written. To solve the problem we'll need to know more about exactly how you are running it: what XSLT processor, what environment, what API. – Michael Kay Mar 03 '16 at 23:51
  • Although it may be typo in your question, bear in mind that XSLT is case-sensitive. You have `Recordset` elements in your XML (note the small `s`), but are looking for `RecordSet` elements in your XSLT. – Tim C Mar 04 '16 at 11:41
  • @PM77-1, Yes I tried that one. It works if I put it between two ``, but that duplicates the data. – user6015642 Mar 04 '16 at 14:49
  • @TimC : Thanks for catching that. I fixed it but it still didn't work. I think XSLT is case insensitive. I am new to it so do not know much in detail – user6015642 Mar 04 '16 at 14:56
  • @MichaelKay : I am using the C# XslCompiledTransform.Transform Method `req.XMLFile.Save(sourceStream); sourceStream.Position = 0; XPathDocument myXPathDoc = new XPathDocument(sourceStream); XslCompiledTransform myXslTrans = new XslCompiledTransform(); myXslTrans.Load(GetXsltFilePath()); XmlTextWriter myWriter = new XmlTextWriter(stream, null); myXslTrans.Transform(myXPathDoc, null, myWriter);` – user6015642 Mar 04 '16 at 15:18

2 Answers2

0

What helped is replacing the newline variable with &#10; like

<xsl:variable name="newline"><xsl:text>&#10;</xsl:text></xsl:variable>

and as a consequence replacing

<xsl:value-of select="concat(Line,'&#10;')" />

with

<xsl:value-of select="concat(Line,$newline)" />

This gives the desired result.

However, you're code has some namespace issues to be resolved... So add the urn: namespace to your XML

<urn:FlatStructure xmlns:urn="http://some.urn">

and XSLT like this

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:urn="http://some.urn">

and after that, remove the urn prefixes from the FlatStructure matches in the XSLT templates.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • Thanks, I did try this. Forgot to mention it in the initial question. – user6015642 Mar 04 '16 at 14:57
  • I do have a URN. however since it was a proprietary information, I didn't include it. Does the `xmlns:urn` effects the way XSLT transforms the XML ? – user6015642 Mar 04 '16 at 15:03
  • @user6015642: Yes. It does affect what nodes are matched. I had to add a namespace to make your sample work at all. – zx485 Mar 04 '16 at 19:57
0

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 !

Chris
  • 2,254
  • 8
  • 22