Add dynamically Hexa decimal code in .xsl file using Database value - java

<xsl:template match="">
<fo:inline color="#ff0000">
<xsl:apply-templates select=""/>
</fo:inline>
Here #ff0000 as static but I need to update as dynamically using DB Value.
DB value: msg = <p><span style="color: #ff0000;">Test</span></p>
Here <fo:inline color="#ff0000"> color is dynamic. p

XSLT operates on XML, and it is not really relevant if the XML values come from the database. But anyway, assuming your XML looks like this:
<p><span style="color: #ff0000;">Test</span></p>
You could extract the "color" from the style, using this expression
normalize-space(substring-after(substring-before(#style, ';'), 'color:'))
You could then use Attribute Value Templates to populate the color attribute in your output
<fo:inline color="{normalize-space(substring-after(substring-before(#style, ';'), 'color:'))}">
Try this sample XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:fo="...">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="span">
<fo:inline color="{normalize-space(substring-after(substring-before(#style, ';'), 'color:'))}">
<xsl:apply-templates />
</fo:inline>
</xsl:template>
<xsl:template match="p">
<fo:block>
<xsl:apply-templates />
</fo:block>
</xsl:template>
</xsl:stylesheet>
Alternatively, if there is a possibility of the "color" not being present, you can do it this way
<xsl:template match="span">
<fo:inline>
<xsl:if test="contains(#style, 'color')">
<xsl:attribute name="color">
<xsl:value-of select="normalize-space(substring-after(substring-before(#style, ';'), 'color:'))" />
</xsl:attribute>
</xsl:if>
<xsl:apply-templates />
</fo:inline>
</xsl:template>

Related

Can you declare / generate xslt params dynamically?

I'm trying to generate an xml with the help of xslt templates and I have a tricky thing to do:
I have to generate a certain number of elements that have same tags but with different values inside them. Example of xslt stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="NbOfBatches"/>
<xsl:param name="wholeTag"/>
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<Document xmlns="urn:something" xmlns:file="someUrn>
<FileAppHdr>
<file:NbOfBatches><xsl:value-of select="$NbOfBatches"></xsl:value-of></file:NbOfBatches>
<xsl:call-template name="selects">
<xsl:with-param name="i">1</xsl:with-param>
<xsl:with-param name="count"><xsl:value-of select="$NbOfBatches"/></xsl:with-param>
</xsl:call-template>
</FileAppHdr>
</Document>
</xsl:template>
<xsl:template name="selects">
<xsl:param name="i" />
<xsl:param name="count" />
<xsl:if test="$i <= $count">
<xsl:call-template name="credit">
<xsl:with-param name="param0"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="$i <= $count">
<xsl:call-template name="selects">
<xsl:with-param name="i">
<xsl:value-of select="$i + 1"/>
</xsl:with-param>
<xsl:with-param name="count">
<xsl:value-of select="$count"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="credit">
<xsl:param name="param0"/>
<CreditTransfer>
<xsl:value-of select="$param0"/>
</CreditTransfer>
</xsl:template>
</xsl:stylesheet>
In this way, I can use Saxon or JAXP and call .setParameter() for NbOfBatches, it will generate that number of tags. However is there a way to generate / modify name of param0 inside CreditTransfer so that I can loop through data in Java and use .setParameter("param1", value1) and so on ?
Thank you
Rather than trying to pass structured data as a stylesheet parameter, why not generate that structured data as XML and pass that XML document as the source document of the transformation?
As an example, imagine a source XML document with param0 in each Batch:
<Batches>
<Batch>
<param0>it was the best of times</param0>
</Batch>
<Batch>
<param0>it was the worst of times</param0>
</Batch>
<Batch>
<param0>it was the age of wisdom</param0>
</Batch>
<Batch>
<param0>it was the age of foolishness</param0>
</Batch>
</Batches>
with a simplified version of your stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<Document xmlns:file="someUrn">
<FileAppHdr>
<file:NbOfBatches><xsl:value-of select="count(//Batch)"/></file:NbOfBatches>
<xsl:for-each select="//Batch">
<xsl:call-template name="selects">
<xsl:with-param name="param0" select="param0" />
</xsl:call-template>
</xsl:for-each>
</FileAppHdr>
</Document>
</xsl:template>
<xsl:template name="selects">
<xsl:param name="param0" />
<xsl:call-template name="credit">
<xsl:with-param name="param0" select="$param0" />
</xsl:call-template>
</xsl:template>
<xsl:template name="credit">
<xsl:param name="param0"/>
<CreditTransfer>
<xsl:value-of select="$param0"/>
</CreditTransfer>
</xsl:template>
</xsl:stylesheet>
produces this output:
<Document xmlns:file="someUrn">
<FileAppHdr>
<file:NbOfBatches>4</file:NbOfBatches>
<CreditTransfer>it was the best of times</CreditTransfer>
<CreditTransfer>it was the worst of times</CreditTransfer>
<CreditTransfer>it was the age of wisdom</CreditTransfer>
<CreditTransfer>it was the age of foolishness</CreditTransfer>
</FileAppHdr>
</Document>
It's fairly trivial to generate XML from structured data in Java using the Saxon sapling classes. https://www.saxonica.com/html/documentation10/javadoc/net/sf/saxon/sapling/package-summary.html

How to Render styles like font color and size which is present inside a span tag in xslt using xsl-fo?

I am generating pdf files based on the output from a rich text editor, some of the components like font color, font size for a specific word or a paragraph comes like
<p>Hello Hi <strong>skansdjnsjc</strong>
<span style="color:#ce181e"><em>cddsklncjkdsv</em></span>
<span style="color:#ce181e">sdsadsad</span></p>
And in my xslt file I did a template match like
<xsl:template match="span">
<xsl:variable name="color">
<xsl:choose>
<xsl:when test="#color">
<xsl:value-of select="#color"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>black</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
</xsl:template>
But the required styles are not being rendered in the pdf file. Am I missing anything? or is there any solution to it.
Thanks For the help in advance !!!
In XSLT 2.0, you could extract the color from the style attribute like so
<xsl:variable name="extractColor" select="tokenize(tokenize(#style, ';')[normalize-space(substring-before(., ':')) = 'color'], ':')[2]" />
Then, to set your color variable (setting it to black if no color was extracted), do this....
<xsl:variable name="color" select="($extractColor, 'black')[1]" />
Of course, if you extended to extract other values, you could create a function.
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:my="my">
<xsl:output method="html" indent="yes" html-version="5"/>
<xsl:template match="span">
<span>
<xsl:variable name="color" select="(my:extract(#style, 'color'), 'black')[1]" />
<xsl:value-of select="$color" />
</span>
</xsl:template>
<xsl:function name="my:extract">
<xsl:param name="text" />
<xsl:param name="name" />
<xsl:sequence select="tokenize(tokenize($text, ';')[normalize-space(substring-before(., ':')) = $name], ':')[2]" />
</xsl:function>
</xsl:stylesheet>

How to do a xsl-template match for a html string

I have a scenario where i need to render html in the pdf using XSLT. I have some html contents in xml file like
<section>
<p><b><u>Heelo</u></b></p>
</section>
I need to render this in the pdf.
<xsl:template match="b">
<fo:inline font-weight="bold">
<xsl:apply-templates select="*|text()" />
</fo:inline>
</xsl:template>
<xsl:template match="u">
<fo:inline text-decoration="underline">
<xsl:apply-templates select="*|text()" />
</fo:inline>
</xsl:template>
<xsl:template match="i">
<fo:inline font-style="italic">
<xsl:apply-templates select="*|text()" />
</fo:inline>
</xsl:template>
But this template match is not working. How to achieve this or is there any way to replace < as < and > as > while creating xml in java?
Thanks for the help in advance !!!
If you want to parse HTML you need a way to integrate an HTML parser, that is possible with an XSLT 2 processor if you use David Carlisle's HTML parser implementation in XSLT 2 from https://github.com/davidcarlisle/web-xslt/blob/master/htmlparse/htmlparse.xsl, you can then import it and call the function to parse the content of the section element into nodes to be processed by your templates:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:d="data:,dpc"
exclude-result-prefixes="#all"
version="3.0">
<xsl:import href="https://raw.githubusercontent.com/davidcarlisle/web-xslt/master/htmlparse/htmlparse.xsl"/>
<xsl:output indent="yes"/>
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="first" page-height="29.7cm" page-width="21cm" margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
<fo:region-body margin-top="1cm"/>
<fo:region-before extent="1cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="first">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<xsl:apply-templates/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="section">
<fo:block>
<xsl:apply-templates select="d:htmlparse(., '', true())/node()"/>
</fo:block>
</xsl:template>
<xsl:template match="b">
<fo:inline font-weight="bold">
<xsl:apply-templates select="*|text()" />
</fo:inline>
</xsl:template>
<xsl:template match="u">
<fo:inline text-decoration="underline">
<xsl:apply-templates select="*|text()" />
</fo:inline>
</xsl:template>
<xsl:template match="i">
<fo:inline font-style="italic">
<xsl:apply-templates select="*|text()" />
</fo:inline>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/94hvTAp
I have used your templates as shown in your question but note that you can simplify all the uses of <xsl:apply-templates select="*|text()" /> to <xsl:apply-templates/> normally.
Other ways depend on the particular XSLT processor used (i.e. whether it offers an extension like http://saxonica.com/html/documentation/functions/saxon/parse-html.html or whether it allows you to implement your own extension functions integrating an HTML parser).
If the HTML is well-formed XML (e.g. has all necessary end tags and quotes attributes, doesn't use HTML specific entity references) then you can also use the XPath 3.1 function parse-xml-fragment with an XSLT 3 processor like Saxon 9.8 or later:
<xsl:template match="section">
<fo:block>
<xsl:apply-templates select="parse-xml-fragment(.)/node()"/>
</fo:block>
</xsl:template>
https://xsltfiddle.liberty-development.net/94hvTAp/1

Generate xpath of XML using XSLT for all Nodes

I was trying to find a way to convert the XML tags to their respective unique address like xPath. Nd I found an XSLT, where XML is processed and the unique address is created only for the nodes which doesnt have child elements and with attributes. [link]:Generate/get xpath from XML node java
XSLT :
<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:variable name="vApos">'</xsl:variable>
<xsl:template match="*[#* or not(*)] ">
<xsl:if test="not(*)">
<xsl:apply-templates select="ancestor-or-self::*" mode="path"/>
<xsl:value-of select="concat('=',$vApos,.,$vApos)"/>
<xsl:text>
</xsl:text>
</xsl:if>
<xsl:apply-templates select="#*|*"/>
</xsl:template>
<xsl:template match="*" mode="path">
<xsl:value-of select="concat('/',name())"/>
<xsl:variable name="vnumPrecSiblings" select=
"count(preceding-sibling::*[name()=name(current())])"/>
<xsl:if test="$vnumPrecSiblings">
<xsl:value-of select="concat('[', $vnumPrecSiblings +1, ']')"/>
</xsl:if>
</xsl:template>
<xsl:template match="#*">
<xsl:apply-templates select="../ancestor-or-self::*" mode="path"/>
<xsl:value-of select="concat('[#',name(), '=',$vApos,.,$vApos,']')"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
when passed to this
<?xml version="1.0" encoding="UTF-8"?>
<root>
<main>
<tag1>001</tag1>
<tag2>002</tag2>
<tag3>
<tag4>004</tag4>
</tag3>
<tag2>002</tag2>
<tag5>005</tag5>
</main>
</root>
produces
/root/main/tag1='001' /root/main/tag2='002' /root/main/tag3/tag4='004' /root/main/tag2[2]='002' /root/main/tag5='005'
So I need the xslt to generate in the following way
/root
/root/main
/root/main/tag1
/root/main/tag2
/root/main/tag3
/root/main/tag3/tag4
/root/main/tag2[2]
/root/main/tag5
Also I dont need values. So please help me with this
Your result could be produced rather simply by:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="name()" />
<xsl:variable name="i" select="count(preceding-sibling::*[name()=name(current())])"/>
<xsl:if test="$i">
<xsl:value-of select="concat('[', $i + 1, ']')"/>
</xsl:if>
<xsl:if test="position()!=last()">
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
<xsl:apply-templates select="*"/>
</xsl:template>
</xsl:stylesheet>
Note that this does not process attributes (or any other type of nodes other than elements).

Producing html xslt from xsl:fo

I'm tring to transform an xsl:fo into xslt (for HTML output). Then, I would apply xslt instead of xsl:fo obtaining the HTML output instead of a PDF.
How can do this?
I need API for XML Processing, or JAXP that transforms XML and XSL to another output. So, I tried to write the xslt template:
<xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo"
>
<xsl:template match="/xsl:template[#match='/root']/fo:root">
<xsl:apply-templates select="fo:page-sequence"/>
</xsl:template>
<xsl:template match="fo:page-sequence">
<xsl:for-each select="fo:flow[#flow-name='xsl-region-body']">
<xsl:call-template name="xsl-regional-body">
<xsl:with-param name="fontsize"><xsl:value-of select="#font-size"/></xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="xsl-regional-body">
<xsl:param name="fontsize" />
<body>
<xsl:if test="$fontsize"> <!-- costruisce <font size=""> -->
<font>
<xsl:attribute name="size">
<xsl:value-of select="$fontsize"/>
</xsl:attribute>
</font>
</xsl:if>
<xsl:for-each select="*/xsl:choose">
<xsl:call-template name="xsl-choose"/>
</xsl:for-each>
<xsl:apply-templates select="."/>
</body>
</xsl:template>
<xsl:template name="xsl-choose">
<xsl:value-of select="."/>
</xsl:template>
I obtain something like
<body><font size="10pt"/>
...
text words..
</body>
But it delete all xsl:choose xsl:when and other tags like
I need all these tags because i need to pass xml data in second pass using Jaxp and producing html..
I would obtain
<body><font size="10pt"/>
<xsl:choose>
<xsl:when test="ddx[#id='LET.....>
<xsl::value-of select="ddx[#id='Lx']/r/PE...>
</xsl:when>..
</xsl:choose>
text words..
</body>
How can get the XSL nodes like text node?
If you want to use XSLT to output XSLT elements (i.e. elements in the XSLT namespace) then you need to use a namespace alias as shown in http://www.w3.org/TR/xslt#literal-result-element:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias">
<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
<xsl:template match="/">
<axsl:stylesheet>
<xsl:apply-templates/>
</axsl:stylesheet>
</xsl:template>
<xsl:template match="block">
<axsl:template match="{.}">
<fo:block><axsl:apply-templates/></fo:block>
</axsl:template>
</xsl:template>
</xsl:stylesheet>
i put xsl: code between < ! CDATA [... ] ] >
anyway using another namespace

Categories

Resources