XSLT split string on every character - xslt

I have a string like : "ABCDEFGHI"
I want the output as A,B,C,D,E,F,G,H,I in xslt-
I have been using -
<xsl:variable name="string_Comma_Delimited">a,b,c,d,e,f,g,h,i</xsl:variable>
<xsl:call-template name="parseString">
<xsl:with-param name="list" select="$string_Comma_Delimited"/>
</xsl:call-template>
<xsl:template name="parseString">
<xsl:param name="list"/>
<xsl:if test="contains($list, ',')">
<fo:table-cell border-width="0.000pt " border-style="solid" border-color="rgb(0,0,0)" padding-top="4.000pt">
<fo:block-container height="6mm" border-width="0.200pt" border-style="solid" border-color="rgb(0,0,0)" text-align="center">
<fo:block text-align="center">
<xsl:value-of select="substring-before($list, ',')"/>
</fo:block>
</fo:block-container>
</fo:table-cell>
<xsl:call-template name="parseString">
<xsl:with-param name="list" select="substring-after($list, ',')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

Your current template splits the string by commas. To simply split it on every single character you can still use a recursive template. All the template would do is output the first character using substring, and then, if the length of the string was 2 or more characters, recursively call the template with the remaining portion of the string.
Try this
<xsl:template name="parseString">
<xsl:param name="text"/>
<letter>
<xsl:value-of select="substring($text, 1, 1)"/>
</letter>
<xsl:if test="string-length($text) > 1">
<xsl:call-template name="parseString">
<xsl:with-param name="text" select="substring($text, 2, string-length($text) - 1)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Given INDIA as input, the following is output:
<letter>I</letter>
<letter>N</letter>
<letter>D</letter>
<letter>I</letter>
<letter>A</letter>
Now, if you were using XSLT 2.0, you could use the xsl:analyze-string function to achieve the same
<xsl:template name="parseString">
<xsl:param name="text"/>
<xsl:analyze-string select="$text" regex=".">
<xsl:matching-substring>
<letter>
<xsl:value-of select="." />
</letter>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
(Of course, if you were using XSLT 2.0, you could have used tokenize in the first case to split the comma-delimited string)

Related

FOP XSL 1.0 Replace text and keep line break

I'm trying to generate a pdf using xsl + xml, but Im having problems on a part of the document where there is some lines of text with line breaks.
When generating the document it did not keep those line breaks.
I have tried everything, and Im completely stuck.
My code is:
<fo:table-row>
<fo:table-cell margin-right="0mm" margin-left="0mm"
margin-bottom="0mm" margin-top="0mm"
xsl:use-attribute-sets="bordergris" number-columns-spanned="5">
<fo:block xsl:use-attribute-sets="titoldades"
space-before.optimum="0pt" space-after.optimum="0pt"
keep-together="always" >
More info:
</fo:block>
<fo:block xsl:use-attribute-sets="dades"
space-before.optimum="0pt" space-after.optimum="0pt"
keep-together="always" linefeed-treatment="preserve">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="/Response/DataList/Data/MoreInfo"/>
<xsl:with-param name="replace" select="' '" />
<xsl:with-param name="with" select="'<fo:block/>'"/>
</xsl:call-template>
</fo:block>
</fo:table-cell>
</fo:table-row>
Which use a template function:
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text"
select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
And the resulting text appears with the hardcoded text "<fo:block/>" instead of applying the line break.
What am I doing wrong?
Thanks!!!
The important thing is to put the following properties inside the fo:block:
inefeed-treatment="preserve" white-space-collapse="false" white-space-treatment="preserve"
And then do the replace with the following:
<fo:block xsl:use-attribute-sets="dades"
linefeed-treatment="preserve" white-space-collapse="false" white-space-treatment="preserve">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="/Response/DataList/Data/MoreInfo"/>
<xsl:with-param name="replace" select="' '" />
<xsl:with-param name="with" select="'
'"/>
</xsl:call-template>

Table of Contents XSL-FO XSLT 1.0

XML:
<levelledPara><title>Tools List and Tool Illustrations</title>
<levelledPara><title>General</title>
<levelledPara><para>The special tools, fixtures, and equipment needed.</para></levelledPara></levelledPara>
XSLT:
<xsl:template match="levelledPara" name="levelledPara" mode="tocdm">
<xsl:if test="*[self::title] and not(parent::*[self::levelledPara])">
<xsl:variable name="id">
<xsl:call-template name="para.id"/>
</xsl:variable>
<fo:table-row>
<fo:table-cell xsl:use-attribute-sets="table.cell.padding1" number-columns-spanned="2">
<fo:block text-transform="capitalize" text-align-last="justify" text-indent="21mm">
<xsl:number count="levelledPara" from="content" level="multiple" format="1.1.1.1.1"/>
<xsl:text>   </xsl:text>
<xsl:value-of select="title" /><fo:leader leader-pattern="dots"/><fo:basic-link internal-destination="{$id}"><fo:page-number-citation ref-id="{$id}"/></fo:basic-link>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:if>
</xsl:template>
<xsl:template match="levelledPara">
<fo:list-block
provisional-distance-between-starts="21mm"
provisional-label-separation="4pt">
<fo:list-item space-after="8pt" space-before="13pt" start-indent="0pt">
<xsl:variable name="id">
<xsl:if test="*[self::title] and not(parent::*[self::levelledPara])">
<xsl:call-template name="para.id"/>
</xsl:if>
</xsl:variable>
<fo:list-item-label
end-indent="label-end()"
text-align="start">
<fo:block font-weight="bold" id="{$id}">
<xsl:if test="not(./table)">
<xsl:number count="levelledPara" from="content" level="multiple" format="1.1.1.1.1"/>
</xsl:if>
</fo:block>
</fo:list-item-label>
<fo:list-item-body
start-indent="body-start()">
<xsl:apply-templates/>
</fo:list-item-body>
</fo:list-item>
</fo:list-block>
</xsl:template>
<xsl:template name="para.id">
<xsl:param name="object" select="."/>
<xsl:apply-templates select="ancestor::dmodule/identAndStatusSection/dmAddress/dmIdent/dmCode"/>
<xsl:choose>
<xsl:when test="$object/#id">
<xsl:value-of select="$object/#id"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(count(ancestor::node()),'00000000',count(preceding::node()))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
The first titled levelledPara should be included in the Table of Contents. In my sample markup none have IDs. The page number wasn't resolving because I forgot to assign an id to the fo:block for levelledPara.
You've shown the XSLT for the table of contents. The ID in the TOC should match an ID in the main text of your document.
So the template match="levelledPara" should contain a block that sets the ID:
<xsl:variable name="lpcode"><xsl:value-of select="$dmcode" /><xsl:value-of select="generate-id(.)" /></xsl:variable>
<fo:block id="{$lpcode}">

XSL:FO Different list-item-label per level

I'm using Apache FOP to generate PDFs from my web application, where users can edit richtext using CKEditor.
My problem is that users sometimes use different levels of indentation in (un-)ordered lists, e.g.:
List item level 1
List item level 2
The CKEditor shows different bulletins per level (or indentation), but the generated PDFs do not, because my template looks like this:
<!-- Lists -->
<xsl:template match="ul|ol" mode="content">
<xsl:apply-templates select="li" mode="content">
<xsl:with-param name="ordered">
<xsl:value-of select="name()='ol'"/>
</xsl:with-param>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="li" mode="content">
<xsl:param name="ordered"/>
<xsl:variable name="label">
<xsl:choose>
<xsl:when test="$ordered='true'">
<xsl:value-of select="position()"/>
.
</xsl:when>
<xsl:otherwise>
•
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<fo:list-block padding-bottom="0pt">
<fo:list-item>
<fo:list-item-label>
<fo:block>
<xsl:value-of select="$label"/>
</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="body-start()">
<fo:block>
<xsl:apply-templates mode="content"/>
</fo:block>
</fo:list-item-body>
</fo:list-item>
</fo:list-block>
</xsl:template>
So how can I set the label variable depending on which level of indentation I'm at?
Something like:
1st level: &#8226
2nd level: &#9702
3rd level: &#8269
Thanks in advance, ~Fabi
EDIT: So the solution suggested by #fafl looks like this:
<!-- Lists -->
<xsl:template match="ul|ol" mode="content">
<xsl:param name="depth" select="0"/>
<xsl:apply-templates select="li" mode="content">
<xsl:with-param name="ordered">
<xsl:value-of select="name()='ol'"/>
</xsl:with-param>
<xsl:with-param name="depth">
<xsl:value-of select="$depth + 1"/>
</xsl:with-param>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="li" mode="content">
<xsl:param name="depth" select="0"/>
<xsl:param name="ordered"/>
<xsl:variable name="label">
<xsl:choose>
<xsl:when test="$ordered='true'">
<xsl:value-of select="position()"/>
.
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="$depth = 1">
• <!--filled circle-->
</xsl:when>
<xsl:when test="$depth = 2">
◦ <!--not-filled circle-->
</xsl:when>
<xsl:otherwise>
■ <!--black square -->
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<fo:list-block padding-bottom="0pt">
<fo:list-item>
<fo:list-item-label>
<fo:block>
<xsl:value-of select="$label"/>
</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="body-start()">
<fo:block>
<xsl:apply-templates mode="content">
<xsl:with-param name="depth">
<xsl:value-of select="$depth"/>
</xsl:with-param>
</xsl:apply-templates>
</fo:block>
</fo:list-item-body>
</fo:list-item>
</fo:list-block>
</xsl:template>
Try adding a parameter "depth" to both templates with a default value of 1. On each recursive call of "ul|ol" increase it by 1. Then you can query it inside both templates.

How to use regex in xsl

I have this request coming in xml. Its a CDATA
<cmd>
<![CDATA[HG<><><36.75><0420>< ><HS6011201700446279><><>< >< >< ><><>< ><>< >< ><>< ><>< ><>]]>
</cmd>
I need to extract HS6011201700446279 from the cdata path.
Following is the regex they gave. How to use this in xsl
HG<\\s*><\\s*><.*><.*><.*><[A-Z]{2}(\\d{10,}).*
There is no regex support in XSLT 1.0. Assuming that the sub-string you want is within the 6th "tag" of the given string, you could extract it by calling a recursive named template:
<xsl:template match="cmd">
<result>
<xsl:call-template name="get-Nth-value">
<xsl:with-param name="list" select="."/>
<xsl:with-param name="N" select="6"/>
</xsl:call-template>
</result>
</xsl:template>
<xsl:template name="get-Nth-value">
<xsl:param name="list"/>
<xsl:param name="N"/>
<xsl:param name="delimiter" select="'><'"/>
<xsl:choose>
<xsl:when test="$N = 1">
<xsl:value-of select="substring-before(concat($list, $delimiter), $delimiter)"/>
</xsl:when>
<xsl:when test="contains($list, $delimiter) and $N > 1">
<!-- recursive call -->
<xsl:call-template name="get-Nth-value">
<xsl:with-param name="list" select="substring-after($list, $delimiter)"/>
<xsl:with-param name="N" select="$N - 1"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>

Increment Letter's in XSL 1.0

I have from XML for example the String "AA"
and i need to increment it Like:
AA AB AC AD AE (...) AZ BA BB (...)
For example increment it for 35 times (coming from a varaible called (`xsl:variable name ="NumIncr">)) starting at AA and finishing in BG.
The string don't always have "AA" can be any 2 letters...
Any idea to do this?!
I think the <xsl:number> tag can help, but still the problem to pass from letters to numbers.
Need something like
<fo:table-cell border-collapse="collapse" border-color="gray" font-family="Helvetica" font-size="8pt" border="solid 1pt gray" padding="1pt" display-align="before">
<fo:block text-align="center">
<xsl:value-of select="string($Sequence)"/>
</fo:block>
</fo:table-cell>
Where $sequence is the AA AB AC (...)
Can writte evrything in the same cell, the problem isn't the output but the tamplate to increment the AA
HELP!!!
To translate a string like "AA" to its numerical value, you can use the following template:
<xsl:template name="string-to-num">
<xsl:param name="string"/>
<xsl:param name="alpha" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:param name="magnitude" select="1"/>
<xsl:param name="carryover" select="0"/>
<xsl:param name="bit" select="substring($string, string-length($string), 1)"/>
<xsl:param name="bit-value" select="string-length(substring-before($alpha, $bit)) + 1"/>
<xsl:variable name="return" select="$carryover + $bit-value * $magnitude"/>
<xsl:choose>
<xsl:when test="string-length($string) > 1">
<xsl:call-template name="string-to-num">
<xsl:with-param name="string" select="substring($string, 1, string-length($string) - 1)"/>
<xsl:with-param name="magnitude" select="string-length($alpha) * $magnitude"/>
<xsl:with-param name="carryover" select="$return"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$return" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Some examples of calling the template:
<xsl:call-template name="string-to-num">
<xsl:with-param name="string">A</xsl:with-param>
</xsl:call-template>
returns 1;
<xsl:call-template name="string-to-num">
<xsl:with-param name="string">Z</xsl:with-param>
</xsl:call-template>
returns 26;
<xsl:call-template name="string-to-num">
<xsl:with-param name="string">AA</xsl:with-param>
</xsl:call-template>
returns 27;
<xsl:call-template name="string-to-num">
<xsl:with-param name="string">ZZ</xsl:with-param>
</xsl:call-template>
returns 702;
<xsl:call-template name="string-to-num">
<xsl:with-param name="string">AAA</xsl:with-param>
</xsl:call-template>
returns 703.
These results are the exact reverse of:
<xsl:number value="$return" format="A"/>