Dynamic Message using Barcode4J in Apache FOP XSL Document - xslt

I'm having trouble generating a dynamic message using Barcode4J ean-13 in an Apache FOP xsl document. I did get the barcode to generate using a hard coded message. However, I would like to pass the barcode number to the xsl document as a parameter. How do I go about doing that?
Also, I have referred to the barcode4J site for help page with no luck. I have tried using the technique described here but had no luck.
This is how my xsl document looks like
<fo:block-container left="1000" top="1000"
z-index="1" position="relative">
<fo:block>
<fo:instream-foreign-object>
<bc:barcode xmlns:bc="http://barcode4j.krysalis.org/ns"
message="123456789789">
<bc:ean-13 />
</bc:barcode>
</fo:instream-foreign-object>
</fo:block>
</fo:block-container>

You don't say which XSLT version you are using.
If you want to pass a parameter to your XSLT, you need to declare the parameter as a child element of your xsl:stylesheet, e.g.:
<xsl:param name="barcode" />
For XSLT 1.0, see http://www.w3.org/TR/xslt#top-level-variables. You could declare more about it if you are using XSLT 2.0.
How to pass the parameter value will depend on which XSLT processor you are using, but you can expect that to be covered in the XSLT processor's documentation.
You can then use the $barcode parameter in an 'attribute value template' in your otherwise-literal markup:
<fo:block-container left="1000" top="1000"
z-index="1" position="relative">
<fo:block>
<fo:instream-foreign-object>
<bc:barcode xmlns:bc="http://barcode4j.krysalis.org/ns"
message="{$barcode}">
<bc:ean-13 />
</bc:barcode>
</fo:instream-foreign-object>
</fo:block>
</fo:block-container>
For attribute value templates in XSLT 1.0, see http://www.w3.org/TR/xslt#dt-attribute-value-template

Related

Can I include a variable inside a src attribute of an XSL-FO block?

We have 60-odd images that we want to include, and want to insert them into a doc using a variable name in the src attribute. Here is the code that currently isn't working:
Without XSL:-
<var name="Request.Data.Communication.AddressStructured.Sender.OrgId" type="string" />
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:fox="http://xmlgraphics.apache.org/fop/extensions" xmlns:svg="http://www.w3.org/2000/svg" xmlns:th="http://www.thunderhead.com/XSL/Extensions" font-family="Frutiger 45 Light">
<fo:external-graphic content-height="30mm" content-width="100mm" src="cms:///Resources/Images/Request.Data.Communication.AddressStructured.Sender.OrgId.jpg" />
</fo:block>
With XSL:-
<xsl:block xmlns:xsl="http://www.w3.org/1999/XSL/Format" xmlns:fox="http://xmlgraphics.apache.org/fop/extensions" xmlns:svg="http://www.w3.org/2000/svg"
<xsl:var name="Request.Data.Communication.AddressStructured.Sender.OrgId" select="Request.Data.Communication.AddressStructured.Sender.OrgId"/>
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Transform" xmlns:fox="http://xmlgraphics.apache.org/fop/extensions" xmlns:svg="http://www.w3.org/2000/svg" <fo:external-graphic content-height="30mm" content-width="100mm" src="cms:///Resources/Images/${Request.Data.Communication.AddressStructured.Sender.OrgId}.jpg" />
</fo:block>
</xsl:block>
You might want {$Request.Data.Communication.AddressStructured.Sender.OrgId} rather than ${Request.Data.Communication.AddressStructured.Sender.OrgId}, otherwise read on...
Getting from your source XML to PDF output is a two-step process (unless, that is, you author documents directly in the XSL-FO vocabulary). The steps are:
An XSLT transformation transforms your XML into XML in the XSL-FO vocabulary that an XSL Formatter understands
An XSL Formatter formats the XSL-FO to make pages and outputs those pages as PDF, SVG, etc.
This graphic from the XSL 1.1 Recommendation (https://www.w3.org/TR/xsl11/#d0e147) tries to illustrate the process:
The XSLT stage has variables, but the XSL-FO stage does not. (You can write expressions for the value of (most) XSL-FO properties, but the expression language (see https://www.w3.org/TR/xsl11/#d0e5032) doesn't stretch to having variables.)
So, in your XSLT stylesheet, you would have something like:
{$Request.Data.Communication.AddressStructured.Sender.OrgId}.jpg
where:
$Request.Data.Communication.AddressStructured.Sender.OrgId is a variable (or parameter) reference. We don't have enough information to know how you'd define the variable.
{...} is an Attribute Value Template (AVT) that is used when you want to evaluate an expression to generate some or all of an attribute value.
The output from the XSLT stage would include the literal string resulting from evaluating the expression, and the XSL Formatter will use the actual URL to locate the image correctly.

disable-output-escaping="yes" is not working

I have the following code
<db:P_RECEIVED_XML>
<xsl:value-of disable-output-escaping="yes" select="oraext:get-content-as-string(/ns0:ReceivedMessage/MessageContent/*)"/>
</db:P_RECEIVED_XML>
when i test this transformation, by giving the value as
<MessageContent xmlns="">
<any_0 xmlns="##any">
<note>
<name>GENERAL</name>
<value><![CDATA[test ~<!##$%^&*()_~!##$%^&*()_+]]></value>
</note>
</any_0>
</MessageContent>
the output rendered is
<db:P_RECEIVED_XML><any_0 xmlns="##any">
<note>
<name>GENERAL</name>
<value>test ~<!##$%^&*()_~!##$%^&*()_+</value>
</note>
</any_0>
</db:P_RECEIVED_XML>
Here & is converted to & though i have used disable-output-escaping="yes".
Kindly help.
You have tagged your question as XSLT. In XSLT, using:
<xsl:value-of select="your-node-here" disable-output-escaping="yes" />
would have disabled the escaping, and not output & as &.
If you are seeing a different result, it's probably a result of your using an extension oraext:get-content-as-string() function. Try removing it and see what you get.
disable-output-escaping only works if the result tree produced by the XSLT processor is immediately serialized, and if the serialization is under the control of the the XSLT processor. That means, for example, that it doesn't work if the result is written to a DOM tree, and you then use the DOM serialization to produce lexical XML.
XSLT processors are allowed to ignore disable-output-escaping entirely.
So it basically depends on what XSLT processor you are using and how you are running it.

XPath selector not working during PDF transformation

I have a DITA bookmap where I am storing image paths:
<bookmap>
<bookmeta>
<data id="productLogo">
<image href="images/_notrans/frontcover/productLogo.svg" />
</data>
<data id="productPhoto" >
<image href="images/_notrans/frontcover/productPhoto.jpg" />
</data>
</bookmeta>
</bookmap>
Then I attempt to grab the href values by data[#id]:
<xsl:variable name="productLogo"><xsl:value-of select="//data[#id='productLogo']/image/#href" /></xsl:variable>
<xsl:variable name="productPhoto"><xsl:value-of select="//data[#id='productPhoto']/image/#href" /></xsl:variable>
(These XPath expressions match the href when I test against my bookmap in Oxygen.)
During transformation I output:
<xsl:message>productPhoto: <xsl:value-of select="$productPhoto"/></xsl:message>
The value-of is always empty.
However, everything works as expected if I replace the id attribute with numbers:
<xsl:variable name="productLogo"><xsl:value-of select="//data[1]/image/#href" /></xsl:variable>
<xsl:variable name="productPhoto"><xsl:value-of select="//data[2]/image/#href" /></xsl:variable>
What am I doing wrong that's preventing using #id="whatever"?
The XSLT is not applied directly over the Bookmap contents, it is applied over an XML document which contains the bookmap with all topic references expanded in it and with some preprocessing applied to it.
If you set the "clean.temp" parameter to "no" you will find in the temporary files folder a file called something like "mapName_MERGED.xml", that is the XML document over which the XSLT is applied and as you will see in it, all IDs have been changed to be unique in the context of the entire XML document.
When usually working with data elements you should set the #name attribute to them like:
<data name="productLogo">
and match that name in the XSLT code.
There are examples of using "data" in the DITA 1.2 specs as well:
http://docs.oasis-open.org/dita/v1.2/os/spec/langref/data.html#data
Another option, depending on your needs, is to develop a naming convention for the product photos and use the element to build the URI. As the product logo shouldn't change for a product family, it wouldn't hurt to hard-code that in the XSLT code.

Is there a way to remove all attributes from an XSLT attribute set when overriding that attr set?

I'm working with some third-party XSLT that makes heavy use of attribute sets for transforming XML to various forms of XSL:FO. Example:
notes.xsl:
<xsl:template match="note">
<fo:block xsl:use-attribute-sets="noteAttrs">
<!-- This is a pretty big template with lots of xsl:choose/xsl:if/etc. -->
</fo:block>
<xsl:template>
<xsl:attribute-set name="noteAttrs">
<xsl:attribute name="margin-left">10px</xsl:attribute>
<xsl:attribute name="margin-right">8px</xsl:attribute>
<xsl:attribute name="margin-top">5px</xsl:attribute>
<xsl:attribute name="font-size">10pt</xsl:attribute>
<!-- several other attributes -->
</xsl:attribute>
The idea is that I import this XSLT, redefining the attribute sets as I see fit. If I just need a different font size for a given .fo document...
<xsl:import href="notes.xsl"/>
<xsl:attribute-set name="noteAttrs">
<xsl:attribute name="font-size">12pt</xsl:attribute>
</xsl:attribute>
The problem is that sometimes I need to flat out remove attributes (i.e. so I inherit from the containing fo:block), or a given fo document is going to be so different that it would be easier to start fresh instead of merge my attribute set with the one from notes.xsl. Is there a way in XSLT 2.0 to do that without reproducing the entire template for note and specifying a different attribute set on the fo:block? I guess I'm looking to be able to do something like this:
<xsl:import href="notes.xsl"/>
<xsl:attribute-set name="noteAttrs" merge_with_imported_attr_set="false">
<xsl:attribute name="font-size">12pt</xsl:attribute>
</xsl:attribute>
I can't switch to an XSLT 3.0 processor immediately, but if there's something new in 3.0 that enables this, I'd love to know about it.
Thanks!
Attribute sets are not very widely used and to answer your question I had to look at the spec to refresh my memory. I don't think there is any way of achieving what you are wanting; you can't really override an attribute set in an importing stylesheet, you can only supplement it. In a well-designed XML vocabulary there is usually an attribute value you can set that is equivalent to omitting the attribute, but if that's not the case then you are stuck.

How do I check for the existence of an external file with XSL?

I've found a lot of examples that reference Java and C for this, but how do I, or can I, check for the existence of an external file with XSL.
First, I realize that this is only a snippet, but it's part of a huge stylesheet, so I'm hoping it's enough to show my issue.
<!-- Use this template for Received SMSs -->
<xsl:template name="ReceivedSMS">
<!-- Set/Declare "SMSname" variable (local, evaluates per instance) -->
<xsl:variable name="SMSname">
<xsl:value-of select=" following-sibling::Name"/>
</xsl:variable>
<fo:table font-family="Arial Unicode MS" font-size="8pt" text-align="start">
<fo:table-column column-width=".75in"/>
<fo:table-column column-width="6.75in"/>
<fo:table-body>
<fo:table-row>
<!-- Cell contains "speakers" icon -->
<fo:table-cell display-align="after">
<fo:block text-align="start">
<fo:external-graphic src="../images/{$SMSname}.jpg" content-height="0.6in"/>
What I'd like to do, is put in an "if" statement, surronding the {$SMSname}.jpg line. That is:
<fo:block text-align="start">
<xsl:if test="exists( the external file {$SMSname}.jpg)">
<fo:external-graphic src="../images/{$SMSname}.jpg" content-height="0.6in"/>
</xsl:if>
<xsl:if test="not(exists( the external file {$SMSname}.jpg))">
<fo:external-graphic src="../images/unknown.jpg" content-height="0.6in"/>
</xsl:if>
</fo:block>
Because of "grouping", etc., I'm using XSLT 2.0. I hope that this is something that can be done. I hope even more that it's something simple.
As always, thanks in advance for any help.
LO
For anyone else who might stumble upon this old question, by piecing together information from various sources on the internet, I came up with this XSLT2 function that uses a Java extension for checking whether a file exists:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:java="http://www.java.com/"
exclude-result-prefixes="java xs">
<xsl:function name="java:file-exists" xmlns:file="java.io.File" as="xs:boolean">
<xsl:param name="file" as="xs:string"/>
<xsl:param name="base-uri" as="xs:string"/>
<xsl:variable name="absolute-uri" select="resolve-uri($file, $base-uri)" as="xs:anyURI"/>
<xsl:sequence select="file:exists(file:new($absolute-uri))"/>
</xsl:function>
</xsl:stylesheet>
You can then use the function like this:
<xsl:if test="java:file-exists($filename, base-uri())">
<!-- ... -->
</xsl:if>
This works at least with Saxon 9.1.0.5J — haven't tested it with any other XSLT processors.
Note: If the file for whose existence you're checking is an XML file and you're using XSLT2, you can also use the built-in doc-available() function:
<xsl:if test="doc-available('hello_world.xml')">
<!-- ... -->
</xsl:if>
No, this cannot be done using XSLT 2.0/XPath 2.0.
The XSLT 2.0 function unparsed-text-available() is only suitable for locating text files and even if a text file with the specifies URI exists this function may return false(), because it also must read the contents of the file and check that it only contains allowed characters.
From the spec:
"The unparsed-text-available function determines whether a call on the unparsed-text function with identical arguments would return a string.
If the first argument is an empty sequence, the function returns false. If the second argument is an empty sequence, the function behaves as if the second argument were omitted.
In other cases, the function returns true if a call on unparsed-text with the same arguments would succeed, and false if a call on unparsed-text with the same arguments would fail with a non-recoverable dynamic error.
Note:
This requires that the unparsed-text-available function should actually attempt to read the resource identified by the URI, and check that it is correctly encoded and contains no characters that are invalid in XML
"
End of quotation.
In case someone else needs it and is using fop, I would like to share this answer since it worked for me like a charm.
I've found a solution:
<xsl:when test="fs:exists(fs:new('myfile.html'))" xmlns:fs="java.io.File">
<!-- do something here... -->
</xsl:when>
and it works independently of XSLT 1.0 or 2.0
EDIT: Use unparsed-text-available function. It is part of xslt 2.0, but not XQuery or standalone XPath.
I've left my previous answer here so you can follow the trail of uncertainty...
I don't believe there is a way of doing this in XSLT using the standard functions. You can do it using extension functions, as described here, for java.
There is the unparsed-text-available function, but I'm unsure if this is a standard function. There's an example of it's usage at Zvon. The unparsed-text-available is mentioned here as being part of xslt 2.0, and is supported in Saxon.
A proposed File Module EXPath specification would support file-system functions such as this (file:exists() in the spec) as standard XPath extension functions. There isn't yet an XSLT implementation for this, but its worth watching.
Back to pgfearo's notice.
A proposed File Module EXPath specification would support file-system
functions such as this (file:exists() in the spec) as standard XPath
extension functions. There isn't yet an XSLT implementation for this,
but its worth watching.
For those who need to check if an file exists or not.
file:exists($path as xs:string)
works fine now.
If you still wants to do it in XSLT here is the solution, I have done it for myself as explained below.
This will not working with regular java.io.File class in XSLT. So I have used java.nio.file.Files class.
JARS required - servelt.jar
w: is the namespace of the our own Java class where pathFromURI method is defined.
Code:
<xsl:variable name="fileURI" select="u:new($absoluteFilePath)" xmlns:u="java:java.net.URI"/>
<xsl:variable name="filePathFromURI" select="w:pathFromURI($fileURI)"/>
<xsl:variable name="fileNotExist" select="not(files:exists($filePathFromURI, /..)) or files:size($filePathFromURI) = 0"/>
public static java.nio.file.Path pathFromURI(java.net.URI uri) throws Exception {
return java.nio.file.Paths.get(uri);
}
It can't solely be done by standard XSLT, you have to use an extention function or some annoying workaround. There are two methods using extension functions: use of standard java/.NET for custom functions (works with some versions of Saxon, AltovaXML, and others), or use of processor specific extension functions, like saxon:file-last-modified()/saxon:last-modified(). You can find some sample code here, look for intern:file-exists().
If you can't use extension functions, you can either generate an XML file externaly which contains informations about your file system and pass it to your stylesheet, or you can wrap binary images within SVG and than use fn:doc-available().
If you need to check for the existence of an XML file, use an external entity and an inline doctype:
<!DOCTYPE foo [ <!ENTITY bar SYSTEM "baz.xml"> ]>
Then add the entity to the stylesheet:
&bar;
The processor will timeout if the file is not found.
If you know the files exist but want to dynamically load one out of many, use the concat function and a choose/when block:
<xsl:variable name="page_name">
<xsl:choose>
<xsl:when test="$page = 1">
<xsl:value-of select="'page1.xml'"/>
</xsl:when>
<xsl:when test="$page = 2">
<xsl:value-of select="'page2.xml'"/>
</xsl:when>
<!--...-->
<xsl:when test="$page = 9">
<xsl:value-of select="'page9.xml'"/>
</xsl:when>
<xsl:when test="$page = 10">
<xsl:value-of select="'page10.xml'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'page0.xml'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="agree" select="document(concat('include/pages/',$page_name))//processing-instruction()"/>
References
XML External Entity (XXE) Processing
For anyone who needs plain XSLT solution for checking if file exist you can just use
unparsed-text-available(concat(system-property('user.dir'),'/filename.text'))
or wrap it into a function
<xsl:function name="functx:file-exists" xmlns:functx="http://www.functx.com">
<xsl:param name="path"/>
<xsl:value-of select="unparsed-text-available(concat(system-property('user.dir'),'/',$path))"/>
</xsl:function>
using this unparsed-text-available(concat(system-property('user.dir'),'/filename.text'))=false() will mean that file does not exist.
Have fun!