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
randipsinghAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
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?
0
randipsinghAuthor Commented:

- 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>
                                                             
0
Geert BormansInformation ArchitectCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

randipsinghAuthor Commented:
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
0
randipsinghAuthor Commented:
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.
0
Geert BormansInformation ArchitectCommented:
Hey, I am sorry, I thought I posted the XSLT that does this a while ago, I just discovered I pasted it in the wrong question.
Here is an XSLT that does all you need, I think
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:variable name="rep-str" select="'&#7902;'"></xsl:variable>
    <xsl:output indent="yes"/>
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="payload">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:variable name="normalized-payload" select="normalize-space(translate(.,'&#10;', $rep-str))"/>
            <xsl:call-template name="remove-empty-lines">
                <xsl:with-param name="str" select="$normalized-payload"></xsl:with-param>
            </xsl:call-template>
        </xsl:copy>
    </xsl:template>
    <xsl:template name="remove-empty-lines">
         <xsl:param name="str"/>
        <xsl:variable name="big" select="concat($rep-str, ' ', $rep-str)"/>
        <xsl:variable name="small" select="concat($rep-str, $rep-str)"/>
        <xsl:choose>
            <xsl:when test="contains($str, $big)">
                <xsl:call-template name="remove-empty-lines">
                    <xsl:with-param name="str">
                        <xsl:value-of select="substring-before($str, $big)"/>
                        <xsl:value-of select="$rep-str"/>
                        <xsl:value-of select="substring-after($str, $big)"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($str, $small)">
                <xsl:call-template name="remove-empty-lines">
                    <xsl:with-param name="str">
                        <xsl:value-of select="substring-before($str, $small)"/>
                        <xsl:value-of select="$rep-str"/>
                        <xsl:value-of select="substring-after($str, $small)"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="translate($str, $rep-str, '&#10;')"/>
            </xsl:otherwise>
        </xsl:choose>
        
    </xsl:template>
</xsl:stylesheet>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.