Solved

xsl newlines

Posted on 2009-04-13
8
1,122 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 250 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
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 …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

821 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