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>
</xsl:text></xsl:variable>
<xsl:variable name="carriageReturn"><xsl:text>
</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
This is one way to do it:
when this transformation is applied on the following XML document (containing more than 10 lines):
the wanted, correct result is produced:
Solution 2:
Using the
str-split-to-wordstemplate/function of FXSL, one can simply write:when this transformation is applied on the same XML document as above, the wanted, correct result is produced: