user2376114
user2376114

Reputation: 5

insert HTML tags to plain text using XSLT

My input looks as follow

<Report>
Report Title\.br\\.br\SECTION1\.br\\.br\****Plain Text*****\.br\\.br\SECTION2\.br\\.br\******Plain Text*****\.br\\.br\*****Plain Text*****
</Report>

I would like to add the below html tags using XSLT and have my output look as follow

<Report> 
<html> <head>  </head> <body> <p> <b><u>Report Title</u></b> </p> <p align="left"> </p> <p align="left"> <b>SECTION1</b> </p> <p align="left"> *****Plain Text***** </p> <p align="left"> </p> <p align="left"> <b>SECTION2</b> </p> <p align="left"> *****Plain Text***** </p> <p align="left"> </p> <p align="left"> *****Plain Text******<b> </b> </p> </body> </html>
</Report>

any thoughts on how to achieve this?

I started by creating the below variables to tokenize my report body

<xsl:variable name="ReportText" select="/Report" />

then I taught of breaking down the tags by opening and closing tags

Opening Report Tags: <html> <head> </head> <body>

Closing Report Tags: </body> </html>

Report Title Opening Tags: <p> <b><u>

Report Title Closing Tags: </u></b> </p> <p align="left> </p>

Section Opening Tags: <p align="left"> <b>

Section Closing Tags: </b> </p>

Text Opening Tags: <p align="left">

Text Closing Tags: </p> <p align="left">

created the below variables to reflect the break down

<xsl:variable name="OpeningReportTags" select="'&lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt;'" />

<xsl:variable name="ClosingReportTags" select="'&lt;/body&gt; &lt;/html&gt;'" />

<xsl:variable name="OpeningTitleTags" select="'&lt;p&gt; &lt;b&gt;&lt;u&gt;'" />

<xsl:variable name="ClosingTitleTags" select="'&lt;/u&gt;&lt;/b&gt; &lt;/p&gt; &lt;p align=&quot;left&quot;&gt; &lt;/p&gt;'" />

<xsl:variable name="OpeningSectionTags" select="'&lt;p align=&quot;left&quot;&gt; &lt;b&gt;'" />

<xsl:variable name="ClosingSectionTags" select="'&lt;/b&gt; &lt;/p&gt;'" />

<xsl:variable name="OpeningTextTags" select="'&lt;p align=&quot;left&quot;&gt;'" />

<xsl:variable name="ClosingTextTags" select="'&lt;/p&gt; &lt;p align=&quot;left&quot;&gt;'" />

In my example, I worked out that there are 11 tokens.

I am having trouble as the number of tokens is not static and will very much change based on the input report.

Upvotes: 0

Views: 303

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116982

Since you know that the first token is the title, you can do:

XSLT 2.0

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

<xsl:template match="/">
    <Report> 
        <html>
            <head/>
            <body>
                <xsl:for-each select="tokenize(Report, '\\.br\\\\.br\\')">
                    <p>
                        <xsl:choose>
                            <xsl:when test="position()=1">
                                <b><u><xsl:value-of select="."/></u></b>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:attribute name="align" select="'left'"/>
                                <xsl:value-of select="."/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </p>   
                </xsl:for-each>         
            </body>
        </html>
    </Report>
</xsl:template>

</xsl:stylesheet>

to get:

Result

<?xml version="1.0" encoding="UTF-8"?>
<Report>
   <html>
      <head/>
      <body>
         <p>
            <b>
               <u>
Report Title</u>
            </b>
         </p>
         <p align="left">SECTION1</p>
         <p align="left">****Plain Text*****</p>
         <p align="left">SECTION2</p>
         <p align="left">******Plain Text*****</p>
         <p align="left">*****Plain Text*****
</p>
      </body>
   </html>
</Report>

I don't see a way to distinguish between a section and a line of text (except for the very first section). If you know how to do that, you can add another xsl:when instruction to make sections bold:

                            <xsl:when test="????">
                                <b><xsl:value-of select="."/></b>
                            </xsl:when>

Demo: https://xsltfiddle.liberty-development.net/93dFepv

Upvotes: 1

Related Questions