none
XSLT Newline handling quirks - Text mode

    Question

  • Hi

    You can see below a relatively simple XSL file and the output produced.

    Notice in the output, an extra line at line 1 and line 14
    >> So my question is why the lack of consistency ?

    e.g. Text in line 7 and line 14 directly follow XSL from the lines below => This causes an extra newline
    However text in line 12 does NOT cause an extra newline, this being inside a 'for-each-select' while the others followed a 'template match' and 'if test'. Neither items in the XML had an newlines embedded in them.

    Thanks in advance

    Friday, June 13, 2014 1:10 AM

Answers

  • Thanks Fred

    Let me share what i found out at the weekend which enabled a big cludge but it works!!

    I experimented more with output mode and other items, but i could not get any consistency for "text" mode. Html is ok because by default you have to add newlines manually via <br /> etc.

    e.g. i have a pre processor and when it sees NEWLINE() it adds this in.

    So for anyone wanting a text mode report as well as html, this  is what i did and it gave me 100% control.

    SO i introduced a marker on my side ("NEWLINE()") for a newline and it adds "<br />" when it sees this marker. 

    The output processing NewLineHandling is set to NONE, this then means you get NO newlines, however as i have the marker, i simply replace the marker with "\r\n". 

    This now works a treat.... 100% control. 

    Now whether a SELECT or an IF etcetra across multiple lines, i can control the newline handling.

    static public bool ProcessFiles(string xmlFile, string xsltFile, string htmlFile) { bool bReturn = false; // Create the XslCompiledTransform and load the stylesheet. XslCompiledTransform xslt = new XslCompiledTransform(true); XmlWriterSettings xmlSettings; try { xslt.Load(xsltFile); bReturn = true; } catch (XsltCompileException ex) { DisplayInfo("Xslt.Load", BuildExceptionString("XsltCompileException", ex, ex.SourceUri, ex.LineNumber, ex.LinePosition)); } catch (XsltException ex) { DisplayInfo("Xslt.Load", BuildExceptionString("XsltException", ex, ex.SourceUri, ex.LineNumber, ex.LinePosition)); } catch (XmlException ex) { DisplayInfo("Xslt.Load", string.Format("XmlException: {0}", ex.Message)); } catch (Exception ex) { DisplayInfo("Xslt.Load", string.Format("GenException: {0}", ex.Message)); } if (bReturn) { // Create an XsltArgumentList. XsltArgumentList xslArg = new XsltArgumentList(); OurString obj1 = new OurString(); xslArg.AddExtensionObject("urn:SergeStringFunc", obj1); XmlWriterSettings xmlSet = new XmlWriterSettings(); if (!TopLevelCode._bHtmlOut) {

    // In Text Mode xmlSet.NewLineHandling = NewLineHandling.None; } xmlSet.ConformanceLevel = ConformanceLevel.Auto; using (XmlWriter xmlw = XmlWriter.Create(htmlFile, xmlSet)) { // Transform the file. try { xslt.Transform(xmlFile, xslArg, xmlw); bReturn = true; } catch (XsltException ex) { DisplayInfo("Xslt.Transform", string.Format("XsltException: {0}", ex.Message)); } catch (XmlException ex) { DisplayInfo("Xslt.Transform", string.Format("XmlException: {0}", ex.Message)); } catch (Exception ex) { DisplayInfo("Xslt.Transform", string.Format("GenException: {0}", ex.Message)); } } // Post processing of "<br />" if (!TopLevelCode._bHtmlOut) {

    // Text mode // @"<br />" exists as markers for NEWLINE, so replace string readText = File.ReadAllText(htmlFile); readText = readText.Replace(cNewLine, "\r\n"); File.WriteAllText(htmlFile, readText); } } return bReturn; }


    How does
    
    <xsl:value-of select="normalize-space(…)"/>
    
    fits into
    
    <xsl:value-of select="myObj:SergeStringFunc(@outcome, 'padright', 14)"


    I am on 2 weeks holidays so not sure on my updates.

    regards

    Monday, June 16, 2014 6:15 AM

All replies

  • Thanks

    But i am struggling to see the answer in the supplied URL.

    In my case it is a text file created (nothing to do with IE or firefox) and the xslt engine used is .net 4 C# XslCompiledTransform.Transform

    regards



    Friday, June 13, 2014 12:11 PM
  • Hello,

    Since I do not know the exact XML file, one possibility I know is the original XML source may contain these newlines (indentation), using normalize-space() to remove this.

    <xsl:value-of select="normalize-space(…)"/>

    And also please put all your context under xsl:text tags as:

    <xsl:text>Build Info for …</xsl:text>       --Line 7
    
    <xsl:text>Output Trance …</xsl:text>    --Line15           

    If this does not work for you, please share your XML and XLST file with us which you are working with.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, June 16, 2014 2:11 AM
  • Thanks Fred

    Let me share what i found out at the weekend which enabled a big cludge but it works!!

    I experimented more with output mode and other items, but i could not get any consistency for "text" mode. Html is ok because by default you have to add newlines manually via <br /> etc.

    e.g. i have a pre processor and when it sees NEWLINE() it adds this in.

    So for anyone wanting a text mode report as well as html, this  is what i did and it gave me 100% control.

    SO i introduced a marker on my side ("NEWLINE()") for a newline and it adds "<br />" when it sees this marker. 

    The output processing NewLineHandling is set to NONE, this then means you get NO newlines, however as i have the marker, i simply replace the marker with "\r\n". 

    This now works a treat.... 100% control. 

    Now whether a SELECT or an IF etcetra across multiple lines, i can control the newline handling.

    static public bool ProcessFiles(string xmlFile, string xsltFile, string htmlFile) { bool bReturn = false; // Create the XslCompiledTransform and load the stylesheet. XslCompiledTransform xslt = new XslCompiledTransform(true); XmlWriterSettings xmlSettings; try { xslt.Load(xsltFile); bReturn = true; } catch (XsltCompileException ex) { DisplayInfo("Xslt.Load", BuildExceptionString("XsltCompileException", ex, ex.SourceUri, ex.LineNumber, ex.LinePosition)); } catch (XsltException ex) { DisplayInfo("Xslt.Load", BuildExceptionString("XsltException", ex, ex.SourceUri, ex.LineNumber, ex.LinePosition)); } catch (XmlException ex) { DisplayInfo("Xslt.Load", string.Format("XmlException: {0}", ex.Message)); } catch (Exception ex) { DisplayInfo("Xslt.Load", string.Format("GenException: {0}", ex.Message)); } if (bReturn) { // Create an XsltArgumentList. XsltArgumentList xslArg = new XsltArgumentList(); OurString obj1 = new OurString(); xslArg.AddExtensionObject("urn:SergeStringFunc", obj1); XmlWriterSettings xmlSet = new XmlWriterSettings(); if (!TopLevelCode._bHtmlOut) {

    // In Text Mode xmlSet.NewLineHandling = NewLineHandling.None; } xmlSet.ConformanceLevel = ConformanceLevel.Auto; using (XmlWriter xmlw = XmlWriter.Create(htmlFile, xmlSet)) { // Transform the file. try { xslt.Transform(xmlFile, xslArg, xmlw); bReturn = true; } catch (XsltException ex) { DisplayInfo("Xslt.Transform", string.Format("XsltException: {0}", ex.Message)); } catch (XmlException ex) { DisplayInfo("Xslt.Transform", string.Format("XmlException: {0}", ex.Message)); } catch (Exception ex) { DisplayInfo("Xslt.Transform", string.Format("GenException: {0}", ex.Message)); } } // Post processing of "<br />" if (!TopLevelCode._bHtmlOut) {

    // Text mode // @"<br />" exists as markers for NEWLINE, so replace string readText = File.ReadAllText(htmlFile); readText = readText.Replace(cNewLine, "\r\n"); File.WriteAllText(htmlFile, readText); } } return bReturn; }


    How does
    
    <xsl:value-of select="normalize-space(…)"/>
    
    fits into
    
    <xsl:value-of select="myObj:SergeStringFunc(@outcome, 'padright', 14)"


    I am on 2 weeks holidays so not sure on my updates.

    regards

    Monday, June 16, 2014 6:15 AM
  • Hello Greg,

    The -space Function needs a string type parameter if the "myObj:SergeStringFunc(@outcome, 'padright', 14)" rreturns a string value, it can be used as:

    <xsl:value-of select="normalize-space(myObj:SergeStringFunc(@outcome, 'padright', 14))"
    Anyway, it is great that you find a solution and share it with us. It will be very beneficial for other community members who have similar questions.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 17, 2014 1:41 AM
  • Just back from hols. Looked up what this does, in my case where i blank fill strings i don't want normalize-space.

    The text side is looked after my the above and in the html case my functions are only on tokens within lines and i don't need space handling. Any code relying on line up will have set a mono-spaced font.

    regards

    Sunday, June 29, 2014 6:32 AM