Solved

xsl newlines

Posted on 2009-04-13
8
1,117 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
Comment Utility
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
Comment Utility

- 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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:randipsingh
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now