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.
Related
I found this piece of code which wraps the data inside a cell ,but it doesn't take into account the word i.e if the complete word cannot be fitted in the line ,It doesn't change the line .It writes few alphabets before changing the line .
What should I modify in this code so that If a particular word is not getting adjusted in the line , it changes the line .
Also , I am new to xsl -fo .I have understood most of the code ,but would appreciate if a little could be explained .
<xsl:template name="zero_width_space_1">
<xsl:param name="data"/>
<xsl:param name="counter" select="0"/>
<xsl:choose>
<xsl:when test="$counter < string-length($data)">
<xsl:value-of select='concat(substring($data,$counter,1),"")'/>
<xsl:call-template name="zero_width_space_2">
<xsl:with-param name="data" select="$data"/>
<xsl:with-param name="counter" select="$counter+1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="zero_width_space_2">
<xsl:param name="data"/>
<xsl:param name="counter"/>
<xsl:value-of select='concat(substring($data,$counter,1),"")'/>
<xsl:call-template name="zero_width_space_1">
<xsl:with-param name="data" select="$data"/>
<xsl:with-param name="counter" select="$counter+1"/>
</xsl:call-template>
</xsl:template>
and then placing a call to zero_width_space_1 like following-
<xsl:call-template name="zero_width_space_1">
<xsl:with-param name="data" select="span"/>
</xsl:call-template>
for ex - "I found this piece of code which wraps the data inside a cell "..It shows as in a cell as
I found th
is piece o
f code whi
ch wraps t
he data in
side a cel
l
Wrapping is provided by the cells automatically . Zero_width_space includes a zero-width space after every character ,so better to not call this template if the requirement is not filling the cells character wise .
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.
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?
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