How to set a color in xslt once for all fields - variables

Can I set a variable or something to lets say "red" once and when i want some font to be red i just call that variable? that way i can edit the color of all specified text with ease for future possibilities. I'm new to xslt and appreciate any help. thank you.
Edit: adding some code with what i would want.
<?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">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<!--Probably declare the variable here-->
<!--Like <variable=outputcolor value="red" -->
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="A4-portrait"
page-height="29.7cm" page-width="21.0cm" margin="2cm">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4-portrait">
<fo:flow flow-name="xsl-region-body" font-family="Helvetica"
font-size="6pt">
<fo:block font-size="8pt" text-indent="5pt">
<fo:inline font-weight="bold"><xsl:text>Application Summary</xsl:text></fo:inline>
<!--Here i would like to make red a variable that i could possibly changed -->
<!--like fo:inline color="{outputcolor}"-->
<fo:inline color="red">
<xsl:value-of select="businessInfo/appSum" />
</fo:inline>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>

Using an attribute-set is an option worth exploring. Another option is to do what you started to do:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<!--Probably declare the variable here-->
<xsl:variable name="outputcolor" select="'red'" />
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="A4-portrait"
page-height="29.7cm" page-width="21.0cm" margin="2cm">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4-portrait">
<fo:flow flow-name="xsl-region-body" font-family="Helvetica"
font-size="6pt">
<fo:block font-size="8pt" text-indent="5pt">
<fo:inline font-weight="bold"><xsl:text>Application Summary</xsl:text></fo:inline>
<!--Here i would like to make red a variable that i could possibly changed -->
<fo:inline color="{$outputcolor}">
<xsl:value-of select="businessInfo/appSum" />
</fo:inline>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
Note that declaring the variable at the top level of the template limits its scope to that template only. You can declare global variables (at the top level of the stylesheet, outside of any template) to make them available anywhere in the stylesheet.

You can define a named attribute set as a direct child of xsl:stylesheet, containing the attributes you want to reuse:
<xsl:attribute-set name="colouredText">
<xsl:attribute name="color">red</xsl:attribute>
<!-- you can set other attributes too: font-weight, font-style, ... -->
</xsl:attribute-set>
and then use it when creating elements with xsl:copy, xsl:element or with a literal result element:
<xsl:copy use-attribute-sets="colouredText">
...
</xsl:copy>
<xsl:element name="fo:inline" use-attribute-sets="colouredText">
...
</xsl:element>
<fo:inline xsl:use-attribute-sets="colouredText">
...
</fo:inline>
If the output requirements change ("Besides being red, important info must be bold also" / "Forget the color, just made them italic" / "Try using Comic Sans") you only need to adjust the attribute definitions inside the attribute-set, without having to modify the templates where these "styles" are applied.
The value of the xsl:use-attribute-sets attribute is a whitespace-separated list of attribute set names:
<xsl:attribute-set name="spacedText">
<xsl:attribute name="space-before">12pt</xsl:attribute>
<xsl:attribute name="space-after">12pt</xsl:attribute>
</xsl:attribute-set>
...
<fo:block xsl:use-attribute-sets="colouredText spacedText">
This block is both coloured and spaced!
</fo:block>
An attribute set can in turn refer to other sets:
<xsl:attribute-set name="colouredTitle" use-attribute-sets="colouredText">
<xsl:attribute name="font-size">16pt</xsl:attribute>
<xsl:attribute name="text-align">center</xsl:attribute>
</xsl:attribute-set>
The linked section of the XSL 1.0 specifications (or the corresponding section of the XSL 2.0 specifications) provides further information about how attribute sets can be extended and merged.

Related

Why is it not OK to put a xsl:value-of into a fo:block?

I have the following XSL file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output media-type="text" omit-xml-declaration="yes"/>
<xsl:template match="root/branch">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="simple"
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="3cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simple">
<fo:flow flow-name="xsl-region-body">
<fo:block font-size="12pt"
font-family="sans-serif"
line-height="15pt"
space-after.optimum="3pt"
text-align="justify">
<xsl:value-of select="branch/greenLeave"/>
<xsl:value-of select="branch/yellowLeave"/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
When I open it in IDEA IntelliJ, I get the following error message:
Does anybody know what is wrong here? I cannot find a plausible explanation.
Seems like the XSD for FOP which I downloaded from https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk/fop/src/foschema/fop.xsd lacked support for nested XSL-T statements.
As a workaround, I manually added a
<any processContent="skip"/>
to that XSD file at the right place.

How to convert XSL To XSLT (in order to eventually convert to pdf)

i have an XML file that i am trying to convert to PDF.
i have an xsl file, but when i convert it to PDF it does not show it properly and the styling is not ok on the converted PDF
i've been told that in order to convert to PDF i need to use XSLT and not XSL.
i didn't find any converter or a guide on how to convert the XSL to XSLT.
Hopefuly someone here knows?
Many Thanks!
the converting itself to PDF is done with Magic XPI (Integration Software)
<?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">
<xsl:template name="RestOfWorldPdf">
<xsl:param name="barcode_url" />
<xsl:param name="images_dir" />
<fo:block-container break-before="page"
border-style="solid"
border-width="1px"
border-collapse="collapse"
font-family="sans-serif"
top="0px"
left="0px"
width="270px"
height="405px">
<!-- Logo-->
<fo:block-container absolute-position="absolute"
top="0px"
left="0px"
width="91px"
height="49px"
border-collapse="collapse"
border-right-width="1px"
border-right-style="solid"
text-align="center">
<fo:block>
<fo:external-graphic src="url('logo.jpg')"
content-height="46px"/>
</fo:block>
</fo:block-container>
<!-- Market & Transport Type -->
<fo:block-container
absolute-position="absolute"
top="0px"
left="93px"
height="23px"
width="111px">
<fo:block font-weight="bold"
font-size="14pt"
text-align="left"
padding-top="2px">
<fo:inline padding-left="1px">
<xsl:value-of select="../consignmentLabelData/marketDisplay"/>
</fo:inline>
<xsl:text> / </xsl:text>
<fo:inline>
<xsl:value-of select="../consignmentLabelData/transportDisplay" />
</fo:inline>
</fo:block>
</fo:block-container>

Unable to Successfully Pass Parameters to xsl:call-template

I have a generic template I've designed with 2 params title and category.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:param name="category" />
<xsl:param name="title" />
<xsl:template name="test-group">
<fo:block>
<xsl:value=of select="$title" />
</fo:block>
<fo:block>
<xsl:value-of select="$category" />
</fo:block>
</xsl:template>
</xsl:stylesheet>
In the parent template I have the following:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:include href="templates/generic_template.xsl" />
...
<xsl:call-template name="test-group">
<xsl:with-param name="category" select="'animals'" />
<xsl:with-param name="title" select="'DOGS'" />
</xsl:call-template>
...
</xsl:stylesheet>
However, when the transform completes title and category are blank. I'm using FOP 2.0 so I'm not sure if this is a known shortcoming.
When defining a xsl:template that takes parameters, the parameter names used within the xsl:template should be declared using xls:param elements nested within.
<xsl:template name="test-group">
<xsl:param name="category" />
<xsl:param name="title" />
...
</xsl:template>
The parameters being attached to the xsl:template and not the xsl:stylesheet.
This is similar to when calling the template with xsl:call-template, except you are specifying the values instead using xsl:with-param.

Passing a parameter does not seem to work

I need a parameter to be send to a template to help with processing the right nodes in an external second xml file (this to keep the amount of templates small).
I've found that the following code send the parameter and processes it correctly:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
This is text1
<xsl:apply-templates mode="my-mode">
<xsl:with-param name="testParam" select="'TEST_PARAMETER'"/>
</xsl:apply-templates>
This is text2
</xsl:template>
<xsl:template match="DialStats" mode="my-mode">
<xsl:param name="testParam" />
<br></br>
This is text 3<br></br>
<xsl:value-of select="$testParam" /><br></br>
This is text 4<br></br>
</xsl:template>
</xsl:stylesheet>
When I implement the second file I've found that the following no longer passes the parameter:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
This is text1
<xsl:apply-templates select="document('file://D:/DATA/Marquee/DATA/MarqueeStats.xml')" mode="my-mode">
<xsl:with-param name="testParam" select="'TEST_PARAMETER'"/>
</xsl:apply-templates>
This is text2
</xsl:template>
<xsl:template match="DialStats" mode="my-mode">
<xsl:param name="testParam" />
<br></br>
This is text 3<br></br>
<xsl:value-of select="$testParam" /><br></br>
This is text 4<br></br>
</xsl:template>
It calls the template correctly, as I can see both the lines with 'This is text x". So the parameter is gone. I did find some articles suggesting the match= needs to be set, so I matched that with the xml root:
<DialStats>
<Countries Country="Denmark">
<Products Product="MR">
. . .
Any suggestions on how to resolve this?
BTW this is just the basics I need to get right as a lot more code need to be added.

How do I get xsl to return just a portion of a field to the right of the hyphen?

I'm working on an xsl template in shipworks. I was able to make this simple code to create a text file 'pick list' that I could use.
<!DOCTYPE xsl:stylesheet [ <!ENTITY nl "
"> ]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sw="http://www.interapptive.com/shipworks" extension-element-prefixes="sw">
<xsl:import href="ShipWorks2\System\Common" />
<xsl:output method="text" encoding="utf-8" />
<!-- -->
<!-- Start of processing -->
<!-- -->
<xsl:template match="/">
<xsl:for-each select="//Order/Item">
<xsl:sort order="descending" select="../Total" data-type="number" />
<xsl:sort order="descending" select="../Number" data-type="number" />
<xsl:value-of select="SKU" />,<xsl:value-of select="Quantity" /><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
Yeah, I know, really great huh!? Hey, that took me forever to get right! As you can see, it's making a list of SKUs. Well, each of my SKU's looks something like this:
BLAH-BLAH-PRODUCTSKU
I was wondering if there was a way to get ONLY the 'PRODUCTSKU' out of that value, and cut off everything before the last - character.
In looking around online, I saw the substring-after-last function. I'm wondering if I could use that? I tried putting that in there, but I think I'm missing something in regards to a declaration or whatnot.
In the place of
<xsl:value-of select="SKU" />
Replace the below code. It should work,
<xsl:variable name="input">
<xsl:value-of select="SKU" />
</xsl:variable>
<xsl:value-of select="replace($input,'^(.)+?(PRODUCTSKU)$','$2')"/>