Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split the string based on linefeed and map to a limited number of elements

Tags:

split

xslt

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

like image 699
Ding Avatar asked Mar 04 '11 22:03

Ding


2 Answers

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>
like image 163
Dimitre Novatchev Avatar answered Oct 14 '22 16:10

Dimitre Novatchev


Have you tried using either position() or <xsl:number/> ? Either of those may be ways to do what you want to do.


<xsl:number/> would be stored in a variable and then checked to see how many nodes have been created at that level in the document. I've used it in an <xsl:for-each> loop and presume it could be used in a similar way in this case.

like image 1
JB King Avatar answered Oct 14 '22 14:10

JB King