Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4023540
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:36:39+00:00 2026-05-20T10:36:39+00:00

I have a question. My input XML looks like <?xml version=1.0 encoding=UTF-8?> <Text> <Message>this

  • 0

I have a question.

My input XML looks like

<?xml version="1.0" encoding="UTF-8"?>
<Text>
    <Message>this is line 1
    this is line 2
    this is line 3
    this is line 4
    this is line 5
    this is line 6
    this is line 7
    ....
    ....n
    </Message>
</Text>

The content in Message is separated by linefeed or carriage return and the number of line is indefinite.

The output will be:

<?xml version="1.0" encoding="UTF-8" ?> 
<Text>
  <Line>this is line 1</Line> 
  <Line>this is line 2</Line> 
  <Line>this is line 3</Line> 
  <Line>this is line 4</Line> 
  <Line>this is line 5</Line> 
  <Line>this is line 6</Line> 
  <Line>this is line 7</Line> 
  <Line>this is line 8</Line> 
  <Line>this is line 9</Line> 
  <Line>this is line 10</Line> 
</Text>

I have written the following XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>

    <xsl:variable name="lineFeed"><xsl:text>&#xA;</xsl:text></xsl:variable>
    <xsl:variable name="carriageReturn"><xsl:text>&#xD;</xsl:text></xsl:variable>
    <xsl:template match="/">
    <Text>
           <xsl:if test="Text/Message">
                <xsl:choose>
                    <xsl:when test="contains(Text/Message, $lineFeed)">
                        <xsl:call-template name="TextWithLineBreaks">
                            <xsl:with-param name="string" select="Text/Message"/>
                            <xsl:with-param name="delimiter" select="$lineFeed"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:when test="contains(Text/Message, $carriageReturn)">
                        <xsl:call-template name="TextWithLineBreaks">
                            <xsl:with-param name="string" select="Text/Message"/>
                            <xsl:with-param name="delimiter" select="$carriageReturn"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <Line>
                            <xsl:value-of select="Text/Message"/>
                        </Line>
                    </xsl:otherwise>
                 </xsl:choose>
             </xsl:if>
    </Text>
    </xsl:template>

    <xsl:template name="TextWithLineBreaks">
        <xsl:param name="string"/>
        <xsl:param name="delimiter"/>
        <xsl:variable name="Result">
            <xsl:call-template name="extract-bodytext">
                <xsl:with-param name="GetString" select="$string"/>
                <xsl:with-param name="Separator" select="$delimiter"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:copy-of select="$Result"/>
    </xsl:template>

    <xsl:template name="extract-bodytext">
        <xsl:param name="GetString"/>
        <xsl:param name="Separator"/>
        <xsl:choose>
            <xsl:when test="contains($GetString, $Separator)">
                <xsl:variable name="firstline" select="substring-before($GetString, $Separator)"/>
                <xsl:if test="string-length($firstline) > 0">
                    <Line>
                        <xsl:value-of select="substring-before($GetString, $Separator)"/>
                    </Line>
                </xsl:if>
                <xsl:call-template name="extract-bodytext">
                    <xsl:with-param name="GetString">
                        <xsl:value-of select="substring-after($GetString,$Separator)"/>
                    </xsl:with-param>
                    <xsl:with-param name="Separator" select="$lineFeed"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$GetString"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>             
</xsl:stylesheet>

Now my question is, for the output, I can only map to maximum number of 10 <Line>. With the above XSL, it will map to whatever number of line exists in the input.

Any suggestions?

Thanks
Ding

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:36:40+00:00Added an answer on May 20, 2026 at 10:36 am

    This is one way to do it:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/*">
      <Text>
        <xsl:apply-templates/>
      </Text>
     </xsl:template>
    
     <xsl:template match="text()" name="wrapLines">
      <xsl:param name="pText" select="."/>
      <xsl:param name="pNumLines" select="10"/>
    
      <xsl:if test=
       "string-length($pText) and $pNumLines > 0">
       <xsl:variable name="vLine" select=
       "substring-before(concat($pText,'&#xA;'), '&#xA;')"/>
       <Line>
        <xsl:value-of select="$vLine"/>
       </Line>
    
       <xsl:call-template name="wrapLines">
        <xsl:with-param name="pNumLines" select="$pNumLines -1"/>
        <xsl:with-param name="pText" select=
         "substring-after($pText, '&#xA;')"/>
       </xsl:call-template>
      </xsl:if>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the following XML document (containing more than 10 lines):

    <Text>
        <Message>this is line 1
        this is line 2
        this is line 3
        this is line 4
        this is line 5
        this is line 6
        this is line 7
        this is line 8
        this is line 9
        this is line 10
        this is line 11
        </Message>
    </Text>
    

    the wanted, correct result is produced:

    <Text>
       <Line>this is line 1</Line>
       <Line>   this is line 2</Line>
       <Line>   this is line 3</Line>
       <Line>   this is line 4</Line>
       <Line>   this is line 5</Line>
       <Line>   this is line 6</Line>
       <Line>   this is line 7</Line>
       <Line>   this is line 8</Line>
       <Line>   this is line 9</Line>
       <Line>   this is line 10</Line>
    </Text>
    

    Solution 2:

    Using the str-split-to-words template/function of FXSL, one can simply write:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common"
     exclude-result-prefixes="ext"
    >
      <xsl:import href="strSplit-to-Words.xsl"/>
      <xsl:output indent="yes" omit-xml-declaration="yes"/>
    
       <xsl:strip-space elements="*"/>
       <xsl:output indent="yes" omit-xml-declaration="yes"/>
    
       <xsl:param name="pmaxLines" select="10"/>
    
        <xsl:template match="/">
         <Text>
          <xsl:variable name="vwordNodes">
            <xsl:call-template name="str-split-to-words">
              <xsl:with-param name="pStr" select="/"/>
              <xsl:with-param name="pDelimiters"
                              select="'&#10;&#13;'"/>
            </xsl:call-template>
          </xsl:variable>
    
          <xsl:apply-templates select=
           "ext:node-set($vwordNodes)/*[not(position() > $pmaxLines)]"/>
         </Text>
        </xsl:template>
    
        <xsl:template match="word">
          <Line><xsl:value-of select="."/></Line>
        </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the same XML document as above, the wanted, correct result is produced:

    <Text>
       <Line>this is line 1</Line>
       <Line>   this is line 2</Line>
       <Line>   this is line 3</Line>
       <Line>   this is line 4</Line>
       <Line>   this is line 5</Line>
       <Line>   this is line 6</Line>
       <Line>   this is line 7</Line>
       <Line>   this is line 8</Line>
       <Line>   this is line 9</Line>
       <Line>   this is line 10</Line>
    </Text>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text read from a XML file stored in UTF8 encoding. C#
We have a question with regards to XML-sig and need detail about the optional
I have a question about locking. This doesn't have to be only about record
I have a question concerning XML, Java's use of DOM, and empty nodes. I
I have an xml file saved and this is what I want to do,
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
We have the question is there a performance difference between i++ and ++i in
I have a Question class: class Question { public int QuestionNumber { get; set;
I have a question on the best way of exposing an asynchronous remote interface.
I have a question about using streams in .NET to load files from disk.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.