Testing conditions in XSLT with xsl:when - xslt

I have the below xsl tag in which i am fetching out the values of fpml:periodMultiplier and fpml:period as shown below ...
tags in xml are :-
<fpml:periodMultiplier>1</fpml:periodMultiplier>
<fpml:period>Y</fpml:period>
extracting in xsl as shown below
<Payindextenor>
<xsl:value-of select="../fpml:calculationPeriodDates
/fpml:calculationPeriodFrequency
/fpml:periodMultiplier" />
<xsl:value-of select="../fpml:calculationPeriodDates
/fpml:calculationPeriodFrequency
/fpml:period" />
</Payindextenor>
so the value of Payindextenor is 1Y
now i want to put the null check in this tag since it may happen that in coming xml there can be no value for fpml:periodMultiplier and fpml:period also.
so i have come up with below xsl implementation in which i have tried that if any of the value is null then it should print null pls advise is it correct one :-
<xsl:choose>
<xsl:when test="../fpml:calculationPeriodDates
/fpml:calculationPeriodFrequency
/fpml:periodMultiplier
!= ' '
and
../fpml:calculationPeriodDates
/fpml:calculationPeriodFrequency
/fpml:period
!= ' '">
<xsl:value-of select="../fpml:calculationPeriodDates
/fpml:calculationPeriodFrequency
/fpml:periodMultiplier" />
<xsl:value-of select="../fpml:calculationPeriodDates
/fpml:calculationPeriodFrequency
/fpml:period" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'null'" />
</xsl:otherwise>
</xsl:choose>

This is exactly the same situation as in your previous question - you're comparing against the (non-empty) string ' ' containing a single space, when what you actually want is to check for empty strings. And you can use the same solution as I suggested for that question, and test using normalize-space (which treats empty strings and strings containing only whitespace as "false" and anything else as "true"):
<xsl:choose>
<xsl:when test="normalize-space(../fpml:calculationPeriodDates
/fpml:calculationPeriodFrequency
/fpml:periodMultiplier)
and
normalize-space(../fpml:calculationPeriodDates
/fpml:calculationPeriodFrequency
/fpml:period)">
<xsl:value-of select="../fpml:calculationPeriodDates
/fpml:calculationPeriodFrequency
/fpml:periodMultiplier" />
<xsl:value-of select="../fpml:calculationPeriodDates
/fpml:calculationPeriodFrequency
/fpml:period" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'null'" />
</xsl:otherwise>
</xsl:choose>
This will handle cases both where fpml:periodMultiplier or fpml:period elements are absent, and where they're present but empty.

As Ian Roberts has said, comparing a node to a single space is very different from checking for "null", but assuming you want to display "null" when both periodMultiplier and period are blank, you can do this:
<xsl:variable name="freq"
select="../fpml:calculationPeriodDates/fpml:calculationPeriodFrequency" />
<xsl:choose>
<xsl:when test="$freq/fpml:periodMultiplier != '' or
$freq/fpml:period != ''">
<xsl:value-of select="$freq/fpml:periodMultiplier" />
<xsl:value-of select="$freq/fpml:period" />
</xsl:when>
<xsl:otherwise>
<xsl:text>null</xsl:text>
</xsl:otherwise>
</xsl:choose>

Related

XSLT determining if a parameter is null or empty then do something

I'm trying to determine if ../../coupon_code is null or empty. I've tried the methods in this thread Check if a string is null or empty in XSLT to no avail. Maybe I'm doing something wrong?
<!--Coupon Code Name and Code-->
<xsl:choose>
<xsl:when test="not(../../coupon_code)">
<xsl:if test="../../coupon_code != ''">
<xsl:value-of select="../../coupon_rule_name" /> <xsl:value-of select="../../coupon_code" /><xsl:value-of select="$sepend" />D.PROMOTION<xsl:value-of select="$sepend" />
</xsl:if>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
<!--End Coupon Code Name and Code-->
I'm doing
<xsl:when test="not(../../coupon_code)">
To determine if it's null. Then, I'm doing
<xsl:if test="../../coupon_code != ''">
To determine if it's empty.
However, I'm looking at data where this is clearly populated, and it's not entering the when/if to display the data at all. So it's failing somewhere and I don't know where.
Sometimes, ../../coupon_code will have a coupon code in it, like COUPON122. Sometimes it won't have anything in it.
You can check
<xsl:if test="normalize-space(../../coupon_code)">
<xsl:value-of select="../../coupon_rule_name" /> <xsl:value-of select="../../coupon_code" /><xsl:value-of select="$sepend" />D.PROMOTION<xsl:value-of select="$sepend" />
</xsl:if>
to have the xsl:value-ofs processed only if the ../../coupon_code element has some non-whitespace content.

xsl:choose - Hardcoded 0's being added to end of XSLT string

I am having an issue that I cannot figure out in XLST where there are hardcoded 0's being added to the end of a string that I am not calling for. I am using a choose element to prompt placement of the string or to otherwise pick three 0's.
Can anyone tell in my code what I am doing wrong? See below:
<xsl:for-each select="Export/Record">
<xsl:if test="DebitAmount!=0 and DebitAmount!=''">
<xsl:value-of select="ChargedCorpLedgerCode" /><xsl:text>,</xsl:text>
<xsl:value-of select="DepartmentLedgerCode" /><xsl:text>,</xsl:text>
<xsl:value-of select="CategoryJournalNumber" /><xsl:text>,</xsl:text>
<xsl:value-of select="PFAM" /><xsl:text> 0000,</xsl:text>
<xsl:value-of select="LOC" /><xsl:text> 0000,</xsl:text>
<xsl:value-of select="ACTV" /><xsl:text> 0000,</xsl:text>
<xsl:value-of select="CLIENT"/><xsl:text> 0000000,</xsl:text>
<xsl:choose>
<xsl:when test="ProjectLedgerCode=null">
<xsl:value-of select="ProjectLedgerCode" /><xsl:text>,</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="ProjectLedgerCode" /><xsl:text> 000,</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="DebitAmount" /><xsl:text>,</xsl:text>
<xsl:value-of select="''" /><xsl:text>,</xsl:text>
<xsl:value-of select="CategoryDesc" /><xsl:text>,</xsl:text>
<xsl:text>
</xsl:text>
</xsl:if>
my outcome looks like the below where the 000's are adding correctly when the column is blank, but when it is not, it adds the ProjectLedgerCode + 000
This test:
<xsl:when test="ProjectLedgerCode=null">
will return true if the string-value of ProjectLedgerCode is equal to the string-value of a sibling element named null.
If you want to test for ProjectLedgerCode not having a string-value, use:
<xsl:when test="ProjectLedgerCode=''">
or:
<xsl:when test="not(string(ProjectLedgerCode))">
In addition, I believe your results are mixed up.

Below condition is not working in xsl

I have the below condition that I have wriiten in xsl below..
as i have getting an value in xml of fpml:tradeId now it can be there in xml or it can not be there for example as shown below..
<fpml:tradeId tradeIdScheme=""></fpml:tradeId>
or it can contain value also
<fpml:tradeId tradeIdScheme="">10381159400</fpml:tradeId>
as advise i have change the implementation to but still not working ..
<xsl:if test = "fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId = ' '">
<xsl:value-of select="'null'" />
</xsl:if>
</ContractID>
so to deal with this problem i have come up with this below xsl , now the problem comes is that for the cases where tradeId is not there in xml it is not putting null string please advise is my below implementation of
xsl is correct
<xsl:if test = "fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId != ' '">
<xsl:choose>
<xsl:when test="contains(fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId,'-')">
<xsl:value-of select="substring-before(fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId,'-')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:if test = "fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId = ' '">
<xsl:value-of select="'null'" />
</xsl:if>
</ContractID>
as advise new implemetaion done but it is still not working..
<xsl:if test="normalize-space(fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId)">
<xsl:choose>
<xsl:when test="contains(fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId,'-')">
<xsl:value-of select="substring-before(fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId,'-')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:if test = "fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId = ' '">
<xsl:value-of select="'null'" />
</xsl:if>
</ContractID>
You're testing for
fpml:tradeId = ' '
which it doesn't in your example - the value of your fpml:tradeId is the empty string, not a string containing a space. Instead of comparing the value against ' ' for the first case you could use a test of
<xsl:if test="normalize-space(
fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId)">
In XPath a string coerces to a boolean by treating it as false if the string is empty and true if it isn't, so the test will pass if the tradeId value contains at least one non-whitespace character, and fail if it is completely empty or contains only whitespace.
For the second case
<xsl:if test="not(normalize-space(
fpml:dataDocument/*/*/fpml:partyTradeIdentifier[2]/fpml:tradeId))">
would give you the reverse test - true if the string is all whitespace or empty, false if it isn't (or just use a choose with an otherwise clause instead of two opposing if tests).

Using Multiple Nested Attributes in XSLT

What is the best way to construct nested attributes in XSL?
My issue is that onmouseover is an attribute and the src of img is an attribute. The current error given by the builder is:
An item of type 'Element' cannot be constructed within a node of type 'Attribute'.
I used to have an issue of multiple attributes which would have been my preferred route but throws an error:
Attribute and namespace nodes cannot be added to the parent element after a text, comment, pi, or sub-element node has already been added.
I have since attempted the following as a workaround but with no luck
<xsl:template name="Item3">
<xsl:param name="ItemID" />
<xsl:variable name="IMGSRC">
<xsl:choose>
<xsl:when test="$ItemID = 'ST-18/NM/NM/36'">
<xsl:value-of select="concat('imagesCategories/','ST-18-NM-NM-36','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/NM/NM/48'">
<xsl:value-of select="concat('imagesCategories/','ST-18-NM-NM-48','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/NM/NM/72'">
<xsl:value-of select="concat('imagesCategories/','ST-18-NM-NM-72','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/12'">
<xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-12','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/24'">
<xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-24','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/36'">
<xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-36','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/48'">
<xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-48','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/60'">
<xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-60','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/72'">
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('imagesCategories/',$ItemID,'.jpg')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="Items/Item[#ItemID=$ItemID]">
<xsl:attribute name="onmouseover">
<xsl:text>ddrivetip('</xsl:text>
<img src="{$IMGSRC}"/>
<br />
<b>
<xsl:value-of select="Items/Item[#ItemID=$ItemID]/#ItemID" />
</b>
<br />
<b>
<xsl:value-of select="Items/Item[#ItemID=$ItemID]/#ItemDescription" />
</b>
<br />
<br />
<xsl:text>Price (01-09): </xsl:text>
<xsl:value-of select="Items/Item[#ItemID=$ItemID]/#PriceLevel1" />
<br/>
<xsl:text>Price (10-24): </xsl:text>
<xsl:value-of select="Items/Item[#ItemID=$ItemID]/#PriceLevel2" />
<br/>
<xsl:text>Price (25-49): </xsl:text>
<xsl:value-of select="Items/Item[#ItemID=$ItemID]/#PriceLevel3" />
<br/>
<xsl:text>Qty In Stock: </xsl:text>
<xsl:value-of select="Items/Item[#ItemID=$ItemID]/#QtyOnHand" />
<br />
<br />
<xsl:text>Click </xsl:text>
<b>
<xsl:text>"BUY!"</xsl:text>
</b>
<xsl:text> to add this item to your shopping cart</xsl:text>
<xsl:text>', '', '300')</xsl:text>
</xsl:attribute>
There is some additional code and then the proper closing tags.
Thanks everyone!
It looks like you are trying to pass html as a string to your ddrivetip function. However, you are adding these as nodes instead of text, and nodes cannot be added added to attributes, so one solution is to make the nodes text (You'll have to escape the brackets and double quotes too).
However, you are putting a lot of information into the onmouseover event, which is not recommended. Instead of what you are currently doing, I would make a hidden element with an id that incorporates your itemId with the contents of your html and then show that as needed in your onmouseover event.
Use CDATA sections so that the XSLT Processor interpret your img tags as a part of text nodes and not as an attempt to insert element nodes into an attribute (it is forbidden by the XML specification)
<xsl:attribute name="onmouseover">
<xsl:text><![CDATA[ddrivetip('<img src="]]></xsl:text>
<xsl:value-of select="$IMGSRC" />
<xsl:text><![CDATA["/>
<br />
...

XSLT Paging - default to current Date

I am using an xslt transform to show a long list of events.
It has paging, but what I would like is for it to default to the first events that are closest to the current date.
I'm assuming you have a useful date format (YYYY-MM-DD).
<xsl:param name="currentDate" select="''" /><!-- fill this from outside! -->
<xsl:template name="isPageSelected"><!-- returns true or false -->
<xsl:param name="eventsOnPage" /><!-- expects a node-set of events -->
<xsl:choose>
<xsl:when test="$eventsOnPage">
<!-- create a string "yyyy-mm-dd,YYYY-MM-DD-" (note the trailing dash) -->
<xsl:variable name="dateRange">
<xsl:for-each select="$eventsOnPage">
<xsl:sort select="date" />
<xsl:if test="position() = 1">
<xsl:value-of select="concat(date, ',')" />
</xsl:if>
<xsl:if test="position() = last()">
<xsl:value-of select="concat(date, '-')" />
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!-- trailing dash ensures that the "less than" comparison succeeds -->
<xsl:value-of select="
$currentDate >= substring-before($dateRange, ',')
and
$currentDate < substring-after($dateRange, ',')
" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="false()" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
So in your paging routine, to find out if the current page is the selected one, invoke
<xsl:variable name="isPageSelected">
<xsl:call-template name="isPageSelected">
<xsl:with-param name="eventsOnPage" select="event[whatever]" />
</xsl:call-template>
</xsl:variable>
<!-- $isPageSelected now is true or false, proceed accordingly -->
Since sorting in XSLT 1.0 is horribly, horribly broken, your best bet is to find an extension or include a Unix-style time somewhere in your source XML so you can sort on that (although an ISO-formatted string might also work).