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:
<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,'
'), '
')"/>
<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, '
')"/>
</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="' '"/>
</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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With