0

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 know my first token is always going to be the title
  • Second token is always going to be a Section
  • there can be 1 to n Sections
  • There can be many lines of text between Sections where in this case the Token will be a null value

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

4
  • In XSLT 2.0, this is fairly trivial using the tokenize() function. Commented Mar 7, 2020 at 8:59
  • Thank you @michael.hor257k for your response. I will give this a try and get back to you Commented Mar 7, 2020 at 13:14
  • Hello @michael.hor257k, I have added some further details but I'm struggling with how to insert the tags. Not sure if I'm on the right track here or if there is an easier way to do this....struggling to put all of this together to be honest and get my expected output Commented Mar 8, 2020 at 4:36
  • 1
    I have rolled your question back to what it was when I answered it. Please post your entirely different question as a new one. Commented Mar 8, 2020 at 16:39

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you @michael.hor257k for your response.The report body is 1 full string including HTML tags. i.e.: input report: John|Doe|Report Title\.br\\.br\SECTION1\.br\\.br****Plain Text*****\.br\\.br\SECTION2\...|25|1|6.3 output Report: John|Doe|<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">...... </body> </html>|25|1|6.3. My apologies for not being clear enough...is this possible?
@micheal.hor257k somehow i need to build the string as I go through the tokens and then return the node <Report> <xsl:value-of select="$MyBuiltReportStringWithHtmTags" /> </Report> this will then be consumed as a string for my output process..hopefully this is a bit clearer.
This is too confusing. Please post a new question.
Thanks @micheal.hor257k, yes I agree it was getting a bit confusing....i created a new post as you suggested and called it "Manipulating String inside XML node"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.