In following template call
<xsl:call-template name="My_Class">
<xsl:with-param name="className" select="getClassName()"/>
<xsl:with-param name="baseClassName" select="??????"/>
</xsl:call-template>
i have to call My_Class template with value of second parameter i.e. baseClass as user defined. i.e. suppose i want call this template by passing value of second argument(shown as ???? in above code) as "balaji".
Any suggestion on above? Thanks in advance.
If you want to pass a parameter as a fixed, you can just do something like this:
<xsl:call-template name="My_Class">
<xsl:with-param name="className" select="getClassName()"/>
<xsl:with-param name="baseClassName" select="'balaji'"/>
</xsl:call-template>
Alternatively, you could specify the value as a default value in the template itself
<xsl:call-template name="My_Class">
<xsl:with-param name="className" select="getClassName()"/>
</xsl:call-template>
<xsl:template name="My_Class">
<xsl:param name="className" />
<xsl:param name="baseClassName" select="'Balaji'" />
<xsl:value-of select="$baseClassName" />
</xsl:template>
Is this what you are were looking for?
Related
I have an XSLT template which defines a parameter like this:
<xsl:param name="weight"/>
I call it like this:
<xsl:call-template name="MakeBlock">
<xsl:with-param name="weight" select="normal"/>
[...]
Inside my template, I try to use the $weight parameter like this:
<xsl:attribute name="font-weight">
<xsl:copy-of select="$weight"/>
</xsl:attribute>
I have also tried this:
<fo:block font-weight="$weight"/>
In both cases, it's not rendering to the output.
When I debug in Visual Studio and stop on a breakpoint, then show the value of the $weight parameter, I get this as its value:
{Dimension:[0]}
My understanding of this is that the parameter is a nodeset of some kind. So, I have tried this to "get at" its value:
exsl:node-set($weight)/
I have Googled quite a bit and found a bunch of examples which tell me that should work. And yes, I have handled the namespace like this:
<xsl:stylesheet xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl" [other stuff...] />
How do I use this parameter inside my template? (I am using the default XSLT 1.0 transform inside C#, if that matters.)
Edit:
Here is the full template:
<xsl:template name="MakeBlock">
<xsl:param name="font-size"/>
<xsl:param name="font-family"/>
<xsl:param name="weight"/>
<xsl:param name="space-after"/>
<xsl:param name="content"/>
<fo:block font-weight="{$weight}" font-size="{$font-size}pt" font-family="{$font-family}" space-after="{$space-after}pt">
<xsl:value-of select="$content"/>
</fo:block>
</xsl:template>
And here is how it's called:
<xsl:template match="p">
<xsl:call-template name="MakeBlock">
<xsl:with-param name="font-size" select="11"/>
<xsl:with-param name="font-family">PT Sans</xsl:with-param>
<xsl:with-param name="weight" select="normal"/>
<xsl:with-param name="space-after" select="11"/>
<xsl:with-param name="content">
<xsl:value-of select="."/>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
Note that the space-after and font-size params work fine, but the font-weight and font-family params both have the problem I note above. I'm guessing it has something to do with how string params are handled vs. numeric params?
If you want to pass a literal string value then you need <xsl:with-param name="weight" select="'normal'"/>.
Suppose the following template call generates and outputs a result of $20,000 and given that I have another element called sCost which occurs only once and has a value of 385, how could I add to the result of the template call?
<xsl:call-template name="totalCost">
<xsl:with-param name="list" select="/delivery/manifest/item" />
</xsl:call-template>
When I try to do the following I get NaN...
<xsl:variable name="myVar">
<xsl:call-template name="totalCost">
<xsl:with-param name="list" select="/delivery/manifest/item" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$myVar + ../sCost" />
If the result contains the ‘$‘ character, then it is not considered to be a number. Perform the currency formatting only as the last step in the process.
In the following piece of code, I want to understand what is "$type" here and how it is being used.
How this if condition is being applied using "$type".
<xsl:template name="CodValue">
<xsl:param name="type"/>
<xsl:param name="nodeNM">category</xsl:param>
<xsl:element name="{$nodeNM}">
<xsl:if test="$type">
<xsl:attribute name="xsi:type">
<xsl:value-of select="$type"/>
</xsl:attribute>
</xsl:if>
</xsl:element>
</xsl:template>
For this template, you need a parameter, another one is optional nodeNM. You can call the parameter like this:
<xsl:call-template name="CodValue">
<xsl:with-param name="type" select="123" />
</xsl:call-template>
or
<xsl:call-template name="CodValue">
<xsl:with-param name="type">123</xsl:with-param>
</xsl:call-template>
type is a variable in CodValue, so you can print it via
<xsl:value-of select="$type" />
or via {$type} in attributes.
Suggestions:
$nodeNM looks like the name of a tag (html-tags if you produce HTML-Code).
$type (if returns true from xpath, ie. if not empty) will create a xsi:type-Tag-Attribute.
So if you call
<xsl:call-template name="CodValue">
<xsl:with-param name="type" select="123" />
</xsl:call-template>
Your XML will be transformed into
<category xsi:type="123" />
$ is used to reference a variable inside an XPath expression.
In this particular case, $type is declared earlier by <xsl:param name="type"/>. However, it has not been given a value so you will need to use <xsl:with-param> when calling the template to so that you can provide a value.
n.b. The variable $nodeNM was given a default value, so you don't need to specify that when calling the template.
I have the following XSL file from the DCM4CHEE DICOM project and I was trying to adjust it slightly. The code I'm actually trying to get working is commented out, but even the variable assignment seems to actually be returning null. The DCM4CHEE logs are throwing Java exceptions with a 'null' seeming to be coming from the XSL template when it compiles it.
<xsl:call-template name="attr">
<xsl:with-param name="tag" select="'00100040'"/>
<xsl:with-param name="vr" select="'CS'"/>
<xsl:variable name="testing" select="string(field[8]/text())" />
<xsl:with-param name="val" select="$testing" />
<!--
<xsl:variable name="sexString" select="string(field[8]/text())" />
<xsl:variable name="sex">
<xsl:choose>
<xsl:when test="$sexString='1'">M</xsl:when>
<xsl:when test="$sexString='2'">F</xsl:when>
<xsl:when test="$sexString='9'">U</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$sexString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:with-param name="val" select="$sex" /> -->
</xsl:call-template>
The normal XSL is just one simple line:
<xsl:with-param name="val" select="string(field[8]/text())" />
I'm probably doing something very wrong, but can someone explain why I'm not able to assign field[8]/text() to a variable and then pass it to the with-param?
<xsl:call-template name="attr">
<xsl:with-param name="tag" select="'00100040'"/>
<xsl:with-param name="vr" select="'CS'"/>
<xsl:variable name="testing" select="string(field[8]/text())" />
<xsl:with-param name="val" select="$testing" />
</xsl:call-template>
I'm probably doing something very wrong, but can someone explain why
I'm not able to assign field[8]/text() to a variable and then pass it
to the with-param?
Yes, the code is so wrong that the XSLT processor should throw an error message without compiling/executing it.
According to the W3C XSLT 1.0 specification, the only allowed element as child of xsl:call-template is xsl:with-param.
The presented code clearly violates this syntactic rules by placing other elements (xsl:variable) as children of xsl:call-template).
Solution: Move the variables out (before) the xsl:call-template:
<xsl:variable name="testing" select="string(field[8]/text())" />
<xsl:call-template name="attr">
<xsl:with-param name="tag" select="'00100040'"/>
<xsl:with-param name="vr" select="'CS'"/>
<xsl:with-param name="val" select="$testing" />
</xsl:call-template>
The code above is syntactically correct.
Ok, I'm stumped. I would like test if a parameter sent to an XSLT template contains a period and to print out quotes if it does not. The parameter I would like to test is "value" in the template below. It seems the contains function should work, but for some reason the quotes always get outputted regardless the contents of "value". What am I doing wrong? Thanks
<!-- Add a JSON property -->
<xsl:template name="addProperty">
<xsl:param name="name" />
<xsl:param name="value" />
<xsl:value-of select="$name" />
<xsl:text>:</xsl:text>
<xsl:if test="not(contains($value,'.'))">'</xsl:if>
<xsl:value-of select="$value" />
<xsl:if test="not(contains($value,'.'))">'</xsl:if>
<xsl:text>,</xsl:text>
</xsl:template>
When I call your template, it works fine. How are you calling it? This is what I used:
<xsl:call-template name="addProperty">
<xsl:with-param name="name" select="'abc'"/>
<xsl:with-param name="value" select="'123'"/><!-- quoted number -->
</xsl:call-template>
<xsl:call-template name="addProperty">
<xsl:with-param name="name" select="'abc'"/>
<xsl:with-param name="value" select="123"/><!-- NOT quoted number -->
</xsl:call-template>
<xsl:call-template name="addProperty">
<xsl:with-param name="name" select="'xyz'"/>
<xsl:with-param name="value" select="'456.789'"/><!-- quoted number -->
</xsl:call-template>
<xsl:call-template name="addProperty">
<xsl:with-param name="name" select="'xyz'"/>
<xsl:with-param name="value" select="456.789"/><!-- NOT quoted number -->
</xsl:call-template>
And this is what I got as output:
abc:'123',abc:'123',xyz:456.789,xyz:456.789,
Could you not be passing in the values to the named template that you think you are passing in? What XSLT engine are you using?
A good way to test this is to add something like this to your named template and see what it produces, if you don't have a good debugger handy:
XXX<xsl:value-of select="$value"/>XXX
YYY<xsl:value-of select="contains($value, '.')"/>YYY
ZZZ<xsl:value-of select="not(contains($value, '.'))"/>ZZZ