Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

xsl newlines

Posted on 2009-04-13
8
Medium Priority
?
1,129 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:randipsingh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
8 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24136619
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
 

Author Comment

by:randipsingh
ID: 24141775

- 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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24165491
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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:randipsingh
ID: 24169354
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
 

Author Comment

by:randipsingh
ID: 24169386
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
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 750 total points
ID: 24178182
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question