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
Related
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
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
<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>
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).
In a xslt-stylesheet I'm using the methods exsl:node-set and set:distinct to access and filter unique nodes from a variable that contains a result tree fragment.
I can write the values of these nodes into my output file, example:
<xsl:variable name="myNodes">
<xsl:call-template name="getNodes"/>
</xsl:variable>
<xsl:for-each select="set:distinct(exsl:node-set($myNodes)/key)">
<xsl:value-of select="."/>
</xsl:for-each>
The values of the keys are written in the output, just as expected.
However, if I try to use the values in an XPath expression, it fails:
<xsl:for-each select="set:distinct(exsl:node-set($myNodes)/key)">
<xsl:variable name="result" select="/tree//somenode[#key = current()]"/>
<xsl:value-of select="$result"/>
</xsl:for-each>
Now, the output is empty, whereas I know that there is a "somenode" in my input-xml that should be selected by the XPath expression and it's value is not empty.
Now my question is: Why does this happen?
I'm using Java 1.6, Xerces 2.7 and Xalan 2.7.
update:
as requested, some data for the example:
xml doc contains:
<tree>
<somenode key="123"/>
<num>123</num>
<num>0815</num>
</tree>
the getNodes template:
<xsl:template name="getNodes">
<xsl:for-each select="/tree/num">
<xsl:element name="key">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:template>
Here is a transformation that does something close to what you want:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:set="http://exslt.org/sets"
xmlns:exsl="http://exslt.org/common"
>
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="myNodes">
<xsl:call-template name="getNodes"/>
</xsl:variable>
<xsl:variable name="vDoc" select="/"/>
<xsl:for-each select="set:distinct(exsl:node-set($myNodes)/key)">
<xsl:variable name="result" select="$vDoc/tree//somenode[#key = current()]"/>
<xsl:copy-of select="$result"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="getNodes">
<xsl:for-each select="/tree/num">
<xsl:element name="key">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<tree>
<somenode key="123"/>
<num>123</num>
<num>0815</num>
</tree>
the wanted result is produced:
<somenode key="123"/>
Do note:
The source XML document cannot direstly be accessed inside the <xsl:for-each>, because this instruction sets the current node to a node in another document -- the temporary tree created by exsl:node-set().
For this reason we capture the source XML document in a variable $vDoc. We access the source XML document inside the <xsl:for-each> via this variable.
The element <somenode key="123"/> has no text-node descendents and hence no string value. Using <xsl:value-of> on it will not produce any output. This is why we use <xsl:copy-of> here -- it copies the complete element and we see the result.
This stylesheet does the same you want without extensions:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="NumByValue" match="num" use="."/>
<xsl:template match="num[count(.|key('NumByValue',.)[1])=1]">
<xsl:copy-of select="../somenode[#key=current()]"/>
</xsl:template>
</xsl:stylesheet>
Output:
<somenode key="123" />