2 Replies Latest reply on Jul 12, 2010 2:30 PM by ilssac

    Regex Line Break Oddness

    ilssac Level 5

      I have an XML file created by a tool I use.  I am attempting to apply an XSLT transformation to this file to create a nice report.  I am finding some strange behavior when I do so.

       

      There is an attribute that contains carriage returns and line feeds.  This is desirable and I wish to include them in my output.  Everything I read about XML says this just fine.  But when I process the XML with the XSLT at least three out of five tools strip these line break characters.

       

      Here is an example of the behavior using ColdFusion to manipulate the XML.

       

      <cfsavecontent variable="toDoListXML"><?xml version="1.0" encoding="windows-1252" ?>
      <?xml-stylesheet type="text/xsl" href="status-report.xsl"?>
      
      <TODOLIST>
           <TASK COMMENTS="Sent e-mail asking if this task can be removed.
      
      Removed Schedule Task per Julie's authorization.
      "/>
      </TODOLIST>
      </cfsavecontent>
      
      
      <cfset toDoListOBJ = xmlParse(toDoListXML)>
      <cfset taskNode = toDoListOBJ.toDoList.task[1].xmlAttributes['COMMENTS']>
      
      <cfdump var="#toDoListOBJ.toDoList.Task[1]#">
      <cfset comment = toDoListOBJ.toDoList.task[1].xmlAttributes["COMMENTS"]>
      <cfoutput>
      <pre>#comment#</pre>
      
      <cfloop from="1" to="#len(taskNode)#" index="char">
      #mid(comment,char,1)#[#asc(mid(comment,char,1))#]
      </cfloop>
      </cfoutput>
      
      <cfset commentSegment = refind('COMMENTS="Sent e-mail[^"]*"',toDoListXML,1,true)>
      
      <cfset comment = mid(toDoListXML,commentSegment.pos[1],commentSegment.len[1])>
      <cfset comment = reReplace(comment,'COMMENTS="(Sent e-mail[^"]*)"','\1','ONE')>
      
      <cfoutput>
      <pre>#comment#</pre>
      
      <cfloop from="1" to="#len(taskNode)#" index="char">
      #mid(comment,char,1)#[#asc(mid(comment,char,1))#]
      </cfloop>
      </cfoutput>
      

       

      As one can see in the first part of the output where the xml had been parsed into a ColdFusion xml object, the line breaks are gone.  In the second part where raw xml text is output shows that they are there.

       

      Interestingly if I directly connect the style sheet to the xml and open it in a browser, Firefox, Chrome and Opera all transform it the same as CF stripping the line breaks.  IE and an XML Viewer by MindFusion do it the way I expect it to by done, leaving the breaks in.  Unfortunatly IE does not like my CSS formating applied to the <pre> tag.

       

      Other then just breaking down and working this in IE does anybody have any other thoughs on what is going on here?

        • 1. Re: Regex Line Break Oddness
          Adam Cameron. Level 5

          I'm not having much luck suggesting to people they google stuff today, but I'll try it on you, Ian.

           

          I've not come across this myself, but when I googled on "xsl not preserving line breaks in attribute names", I came up with some stuff which looked like it might be worth investigating?

           

          That <xsl:preserve-space elements="text"/> thing looks promising?

           

          --

          Adam

          • 2. Re: Regex Line Break Oddness
            ilssac Level 5

            Adam Cameron. wrote:

            That <xsl:preserve-space elements="text"/> thing looks promising?

             

            --

            Adam

             

            Yup, I thought so too, when I found it myself.  But it had no affect on the output.

             

            What made this particularly wierd for me, is that different tools got different results.  I ended up thinking outside the box.  When I got the XML from the tool and applied the transformation with ColdFusion I get the undersiable results I discussed above.

             

            Instead, I took the XML file from the tool and used ColdFusion to modify it to include a stylesheet declaration directly in the XML then returned that to the browser.  That allowed me to run it in I.E. and get the output as I desired.

             

            But I am sitll curious why there was such inconsistancy between results.

             


            If you care, I am now looking for help on a new XML and ColdFusion question

            http://forums.adobe.com/thread/678689