Sum field in condition (IF field = 'something' Then SUM (field) - BI Publisher - if-statement

I need to sum a value based on a condition on BI Publisher on RTF format. This is what's intended:
IF FIELD1 = 'THIS' THEN SUM(FIELD2)
So far, i have the current formula:
if FIELD = 'THIS' THEN 'N/A?
else sum(FIELD2)
end if
The problem is that the sum is not working. It returns 0 for every result when it should be other values. THe N/A part is ok.
How can i achieve this?

You could try either choose or if like here:
<xsl:choose>
<xsl:when test="your_test_node > 20">
<td>
<xsl:value-of select="sum(your_node)"/>
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:value-of select="sum(your_other_node)"/>
</td>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="your_test_node > 20">
<td>
<xsl:value-of select="sum(your_node)"/>
</td>
</xsl:if>

Related

XSLT: How we can choose multiple conditions in the same test in "choose, when, test" syntax?

I need to verify multiple conditions in XSLT and I don't find an exact already question on this topic. Can somebody help me with some examples, please? I want ot add in test to verify more situations, in the same test. My examples don't work.
<td align="center" style="font-size=8pt">
<xsl:choose>
<xsl:when test="esp:DocType[#v='T2']", test="esp:BusType[#v='44']">first</xsl:when>
<xsl:otherwise>
<xsl:value-of select="esp:AccPnt/#v"/>
</xsl:otherwise>
</xsl:choose>
</td>
<td align="center" style="font-size=8pt">
<xsl:choose>
<xsl:when test="esp:DocType[#v='T2'], esp:BusType[#v='44']">first</xsl:when>
<xsl:otherwise>
<xsl:value-of select="esp:AccPnt/#v"/>
</xsl:otherwise>
</xsl:choose>
</td>
Reading your question, it is not clear, whether every or any
of your conditions should be met in these xsl:when instructions.
In the first case you should put and between these conditions,
whereas in the second case or.
Apparently, Tomalak thought about the "every" case, so he wrote
condition_1 and condition_2, but I'm not so sure about that.
XPath has Boolean operators.
<td align="center" style="font-size=8pt">
<xsl:choose>
<xsl:when test="esp:DocType[#v='T2'] and esp:BusType[#v='44']">first</xsl:when>
<xsl:otherwise>
<xsl:value-of select="esp:AccPnt/#v"/>
</xsl:otherwise>
</xsl:choose>
</td>
The above works, but maybe in a way that's non-obvious at first: It compares two node-sets for emptiness. The empty node-set counts as false in a Boolean context, a filled one counts as true. So the left-hand side esp:DocType[#v='T2'] selects a node or not, i.e. it selects the esp:DocType only if the value of its #vattribute equals 'T2'. When the other half of the expression also selects a node, the overall result will be true.
The more obvious way to write the same thing would be this:
<td align="center" style="font-size=8pt">
<xsl:choose>
<xsl:when test="esp:DocType/#v = 'T2' and esp:BusType/#v = '44'">first</xsl:when>
<xsl:otherwise>
<xsl:value-of select="esp:AccPnt/#v"/>
</xsl:otherwise>
</xsl:choose>
</td>

How to avoid creating hyperlink in XSL on some values?

I have a report that is being generated from the following XSL code. The code generates hyperlink on each number, but I want to avoid creating hyperlink if the value of the variable is zero. Here is my code. Please let me know how can I avoid creating hyperlink when the value is zero
<xsl:choose>
<xsl:when test="Image = '' and not(ValueId = '0')">
<TD style='background-color:#FF6360'>
<xsl:variable name="value">
<xsl:value-of select="ValueId"/>
</xsl:variable>
<xsl:variable name="URL">
<xsl:value-of select="concat('https://google.com/imp/values.aspx?id=', $value)"/>
</xsl:variable>
<a href="{$URL}">
<xsl:value-of select="ValueId"/>
</a>
</TD>
</xsl:when>
</xsl:choose>
The following would test if the value of variable $value is 0 and only if it is not equal to 0 it would create the link:
<xsl:if test="$value!=0">
<a href="{$URL}">
<xsl:value-of select="ValueId"/>
</a>
</xsl:if>
If the type of $value is not a number, the test should be
test="$value!='0'"
Problem in your current code is that you do not generate a table cell if the value is zero - surely you'll want to generate the td even if the value is zero?
Als you seem to have some unnecessary code: first you make a variable for $value that gets initialized by ValueId. You could have done all that in one go directly with ValueId.

XSL stylesheet: creating Hyperlink based of query item_id

I am new to coding. Started XSL coding from past 1 month.
I want to creat a hyperlink according to the item_id.
But my concat is not working as desired.
My requirement is that i have to get create hyperlinks based on the variable item_id
For example:
https://xyz.com/webpr/webpr.php?objtype=frames&g_startlink=maintain&g_startdata=194970&g_userid=msbzzh&g_session_id=6017650`
https://xyz.com/webpr/webpr.php?objtype=frames&g_startlink=maintain&g_startdata=194971&g_userid=msbzzh&g_session_id=6017650
where the variable item_id comes inbetween the link. (194970, 194971 and so on)
So here is my code:
<xsl:when test ="$propName ='item_id'">
<td>
<xsl:variable name="itemId" select="$occRef/#*[local-name()=$propName]" />
<xsl:value-of select="$itemId" />
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
</td>
</xsl:when>
and i also tried like this.. But both of them didn't work out.
<xsl:value-of select="$itemId" />
UPDATED: You forgot to escape ampersands and indeed the variable was used incorrectly. See below the correct syntax.
<xsl:when test="$propName='item_id'">
<td>
<xsl:variable name="itemId" select="$occRef/#*[local-name()=$propName]"/>
<a href="{concat('https://xyz.com/webpr/webpr.php?objtype=frames&g_startlink=maintain&g_startdata=', $itemId, '&g_userid=msbzzh&g_session_id=6017650')}" target="_blank">
<xsl:value-of select="$itemId"/>
</a>
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
</td>
</xsl:when>

How to make a required field in XSL/XSLT

I have 3 radio button selection choices, and if the user selects the option "Forward" there is an input field for them to enter an email address. How can I make that input field required if they select the forward radio button? I can't seem to find any good information on XSLT required fields. The code is below:
<table cellpadding="0" id="allCategoryCheckboxes" >
<tr id="categoryRows">
<xsl:for-each select="form/categories/all/category">
<xsl:sort data-type="number" order="ascending"
select="((value='Available') * 1) +
((value='Unavailable') * 2) +
((value='Forward') * 3)"/>
<td>
<input type="radio" name="catUid">
<xsl:attribute name="value"><xsl:value-of select="uid"/></xsl:attribute>
<xsl:if test="uid = ../../current//category/uid"><xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
</input>
<xsl:value-of select="value"/>
<xsl:if test="value = 'Forward'">
<xsl:text> to: </xsl:text>
<xsl:variable name="someEmailAddress">
<xsl:if test="/bedework/formElements/form/xproperties/node()[name()='X-FORWARDING-ADDRESS']">
<xsl:value-of select="/bedework/formElements/form/xproperties/node()[name()='X-FORWARDING-ADDRESS']/values/text"/>
</xsl:if>
</xsl:variable>
<input type="text" name="someEmailForwardingAddress" value="{$someEmailAddress}" id="someEmailForwardingAddress"/>
</xsl:if>
</td>
</xsl:for-each>
</tr>
</table>

Having problems with an xsl:choose tag

I have an xsl question, I'm trying to use an xsl:choose. Below is the snippet. The problem is that the <xsl:otherwise> tag always fires, which makes me believe that the <xsl:when> isn't evaluating properly.
Any clues as to what I'm doing wrong?
<xsl:choose>
<xsl:when test="./Property[#Name ='RecoveryModel']='Full'">
<td align="left" bgcolor="#ff00ff">
<xsl:value-of select="./Property[#Name ='RecoveryModel']"/>
</td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="./Property[#Name ='RecoveryModel']"/></td>
</xsl:otherwise>
</xsl:choose>
Try adding [1] to the <xsl:when> test like this:
<xsl:choose>
<xsl:when test="./Property[#Name ='RecoveryModel'][1]='Full'">
<td align="left" bgcolor="#ff00ff">
<xsl:value-of select="./Property[#Name ='RecoveryModel']"/>
</td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="./Property[#Name ='RecoveryModel']"/></td>
</xsl:otherwise>
</xsl:choose>
Otherwise, the ./Property[#Name ='RecoveryModel'] selector will return (essentially) a list of matching elements (hopefully just one in your case. You need [1] to select the first matching Property element.
Also, I'm assuming your source element looks something like:
<node>
<Property Name="RecoveryModel">Full</Property>
<node>