Reputation: 5
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="'<html> <head> </head> <body>'" />
<xsl:variable name="ClosingReportTags" select="'</body> </html>'" />
<xsl:variable name="OpeningTitleTags" select="'<p> <b><u>'" />
<xsl:variable name="ClosingTitleTags" select="'</u></b> </p> <p align="left"> </p>'" />
<xsl:variable name="OpeningSectionTags" select="'<p align="left"> <b>'" />
<xsl:variable name="ClosingSectionTags" select="'</b> </p>'" />
<xsl:variable name="OpeningTextTags" select="'<p align="left">'" />
<xsl:variable name="ClosingTextTags" select="'</p> <p align="left">'" />
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
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