Link to home
Start Free TrialLog in
Avatar of randipsingh
randipsingh

asked on

xsl newlines

I want to do an xsl transform where
any line that has only the newline is stripped out

for example

ABCD

EFGH

LMN

there is a newline character between each lines. I want them stripped out in the output
so i get the output ( not normalize space does not work here )
ABCD
EFGH
LMN
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Well, I have some questions for you first
- Can you use XSLT2, regular expressions there come in handy for this task, or are you bound to XSLT1?
- is this a full text field, or a bunch of elements?
- what are you transforming to? HTML or another XML?
- do you want to show this in a browser?
Avatar of randipsingh
randipsingh

ASKER


- no I can use only XSLT1, I am transforming XML to XML , not shown in browser

-  transforms outputs  and embeds full text extracted from incoming xml ( text embedded in xml)
  1) the newlines at the end of line ARE valid
   Note in the output some lines have on than mode blank lines
   and some its one and in others they are no blank  lines.
   when I say blank lines I mean they have newline characters ( assumption ! )
   note : these  do NOT work.            <xsl:strip-space elements="*"/>
                                                         and/or
                                                                      <xsl:template match="text()">
                                                                      <xsl:value-of select="normalize-space()"/>
                                                                      </xsl:template>

 SAMPLE XML:

                                               <myxml>
                                                             <context>
                                                                  <format>REG</format>
                                                                  <priority>HIGH</priority>
                                                              <context>
                                                              <msg>
                                                                   <payload>My name is :
                                                                                     
                                                                                    randi
                                                                                    Address
                                                                                   123 highway 4
                                                                               
                                                                                  mycity - 23453

                                                                                 Ph : 123451234


                                                ****************************end message*************************************
                                                                    </payload>
                                                               </msg>
                                                              </myxml>>


what I want is :

                                                      <myxml>
                                                             <context>
                                                                  <format>REG</format>
                                                                  <priority>HIGH</priority>
                                                              <context>
                                                              <msg>
                                                                   <payload>My name is :
                                                                                    randi
                                                                                    Address
                                                                                   123 highway 4
                                                                                   mycity - 23453
                                                                                   Ph : 123451234
                                                ****************************end message*************************************
                                                                    </payload>
                                                               </msg>
                                                 </myxml>
                                                             
Well, this is not so simple in XSLT1
You can recursively remove the newlines, but there is an "I don't know how many" series of spaces in front of that.
Do you need to maintain all the spaces as well?

This approach could work:
- replace all newlines with a very strange character, some Korean something that you would not use
- normalize space the string, now all series of spaces will become one space, the newlines are protected
- replace theKorean character back to a newline
- now replace each newline-space-newline and each newline-newline into a single newline

This still would be an interesting recursion

Do you think this might work, I will then show you the code
If it is important that you maintain the indenting of the spaces, I don't think I will code the solution for you

In either case, this kind of work for me would be a definite driver to using XSLT2.
(XSLT1 is a lot better in structural issues compared to string and text processing)
Please evaluate migration to XSLT2, if this is really important for you

cheers

Geert
1. I  do NOT need to maintain indentation spaces
2. I do NOT need to maintain 'trailing' or 'leading spaces'
3. I DO need to maintain spaces between the 'words' in a line as shown in the examples.

I would like to try your approach. So please provide an example.

thanks
also to add  I do need to retain 'newlinw' at the end of each populated newline.

its only the lines that have only a newline that I want to strip from the output,

 just as shown in my example.
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial