Unable to pass variable value in XSLT within a template - xslt

The below is my sgml file structure:
<em>
<pgblk revdate="20130901">
<task revdate='20140901'>
<p>random texts and few more inner tags</p>
</task>
<task revdate='20150901'>
<p>random texts and few more inner tags</p>
</task>
<task revdate='20160901'>
<p>random texts and few more inner tags</p>
</task>
</pgblk>
I have many tags with same name (task tag) and am trying to get the greatest revdate here. My XSLT is as follows:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text" />
<xsl:template match="/pgblk">
<xsl:variable name="updatedrevdate">
<xsl:for-each select="task">
<xsl:sort select="#revdate" data-type="number" order="descending" />
<xsl:if test="position() = 1">
<xsl:value-of select="#revdate" />
</xsl:if>
</xsl:for-each>
</xsl:variable>
</xsl:template>
</xsl:stylesheet>
Now i am getting the greatest task tag value..but when i use the condition inside the same template to compare the greatest tag and the pageblock value
"Operations to PRINT THE GREATEST REVDATE"
The comparison is always getting the first revdate from task and comparing with pageblock revdate. It is not getting the greatest from the task.
Any help is much appreciated. Thanks.

Please check if the following XSLT code helps you make changes to your code. Need to change the template matching to
<xsl:template match="pgblk">
The solution does the comparison and outputs the dates.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text" />
<xsl:strip-space elements="*"/>
<xsl:template match="pgblk">
<xsl:variable name="updatedrevdate">
<xsl:for-each select="task">
<xsl:sort select="#revdate" data-type="number" order="descending" />
<xsl:if test="position() = 1">
<xsl:value-of select="#revdate" />
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!-- output the dates -->
<xsl:value-of select="concat('Page Block Date: ', #revdate)" />
<xsl:text>
</xsl:text>
<xsl:value-of select="concat('Max Task Date: ', $updatedrevdate)" />
<xsl:text>
</xsl:text>
<xsl:choose>
<xsl:when test="#revdate > $updatedrevdate">
<xsl:value-of select="concat('Max date after comparison: ', #revdate)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('Max date after comparison: ', $updatedrevdate)" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Output
Page Block Date: 20130901
Max Task Date: 20160901
Max date after comparison: 20160901

Related

Creating a JSON from xml with date and time logic templates

I'm trying to create a JSON from a XML which has messages and each message has its date/time.
Below is the XML
<message>
<messageText heading="Temporary Maintenance Message 1">test message1</messageText>
<displayScheduleContainer>
<startDate>22/05/2019</startDate>
<startTimeHrs>12</startTimeHrs>
<startTimeMins>45</startTimeMins>
<noEndDate>true</noEndDate>
</displayScheduleContainer>
</message>
<message>
<messageText heading="Temporary Maintenance Message 1">test message2</messageText>
<displayScheduleContainer>
<startDate>22/06/2019</startDate>
<startTimeHrs>12</startTimeHrs>
<startTimeMins>45</startTimeMins>
<noEndDate>true</noEndDate>
</displayScheduleContainer>
</message>
The logic inside XSLT reads the date and time to activate the message
<xsl:for-each select="xalan:nodeset($messageData)/activeMessage/message">
<xsl:variable name="variableN">
<xsl:call-template name="jsonMsg" />
</xsl:variable>
<xsl:choose>
<xsl:when test="$variableN = 'true'">
<xsl:copy-of select="messageText/text()" />
<xsl:if test="position() < last()">,</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:for-each>
<xsl:template name="jsonMsg">
<xsl:choose>
<xsl:when test="displayScheduleContainer/noEndDate = 'true'">
<xsl:variable name="messageInDateTime">
<xsl:call-template name="noEndDateTemplate">
<xsl:with-param name="startDateTime"
select="concat(displayScheduleContainer/startDate, ' ', displayScheduleContainer/startTimeHrs, ':', displayScheduleContainer/startTimeMins)" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$messageInDateTime" />
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="noEndDateTemplate">
<xsl:param name="startDateTime" />
<xsl:variable name="sdf"
select="java:text.SimpleDateFormat.new('dd/MM/yyyy hh:mm')" />
<xsl:variable name="currentDateTime" select="java:util.Date.new()" />
<xsl:choose>
<xsl:when
test="java:compareTo(java:parse($sdf, $startDateTime), $currentDateTime) < 0">
<xsl:text>true</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>false</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
The problem i'm facing here is if the last value is false, I end up getting the comma at the end. As i'm checking for the last position and adding the comma. Due to this the whole JSON is broken. In this case it adds the comma because i'm displaying the text only if it is true.
"message": ["test message1", ]
I'm using XSLT 1.0
Instead of xsl:choose, append a predicate to your select expression. Here's a simplified example:
XML
<messages>
<message>
<messageText>test message1</messageText>
<displayScheduleContainer>
<noEndDate>true</noEndDate>
</displayScheduleContainer>
</message>
<message>
<messageText>test message2</messageText>
<displayScheduleContainer>
<noEndDate>true</noEndDate>
</displayScheduleContainer>
</message>
<message>
<messageText>test message3</messageText>
<displayScheduleContainer>
<noEndDate>false</noEndDate>
</displayScheduleContainer>
</message>
</messages>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/messages">
<xsl:for-each select="message[displayScheduleContainer/noEndDate = 'true']">
<xsl:value-of select="messageText" />
<xsl:if test="position() < last()">,</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Result
"test message1,test message2"
Added:
If the test is too complex to fit in a predicate, do the transformation in two passes. Here, again, a simplified example:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/messages">
<!-- first pass -->
<xsl:variable name="eligible-messages">
<xsl:for-each select="message">
<xsl:if test="displayScheduleContainer/noEndDate = 'true'">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!-- output -->
<xsl:for-each select="exsl:node-set($eligible-messages)/message">
<xsl:value-of select="messageText" />
<xsl:if test="position() < last()">,</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Replace the test in:
<xsl:if test="displayScheduleContainer/noEndDate = 'true'">
with the test/s you want to perform.

How to pass as variable into .xslt 1.0 key expressions

Following on from my original question:
Filtering, Grouping, Counting and Selecting specific nodes in XML using XSLT 1.0
I've now run into another problem. Using this code that was kindly provided by michael.hor257k
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:key name="vehicle-by-series" match="Vehicle[Model='KA']" use="Series" />
<xsl:template match="/Dealer">
<xsl:for-each select="Vehicle[Model='KA'][count(. | key('vehicle-by-series', Series)[1]) = 1]">
<xsl:value-of select="Model"/>
<xsl:text>: </xsl:text>
<xsl:value-of select="Series"/>
<xsl:text>, </xsl:text>
<xsl:variable name="grp" select="key('vehicle-by-series', Series)" />
<xsl:value-of select="count($grp)"/>
<xsl:text> in stock, starting from </xsl:text>
<xsl:for-each select="$grp">
<xsl:sort select="Price" data-type="number" order="ascending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="Price"/>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I'm passing in the parameter model:
<xsl:param name="model" />
From an asp.NET URL variable.
I want to substitute the currently hardcoded KA for the model parameter. From the searching I've done you can't use parameters or variables in a key expression in XSLT 1.0. And I don't have the option to upgrade to 2.0. Mores the pity.
Would really appreciate some more help please.
Thanks in advance.
I would suggest you do:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:param name="model"/>
<xsl:key name="vehicle-by-series" match="Vehicle" use="concat(Model, '|', Series)" />
<xsl:template match="/Dealer">
<xsl:for-each select="Vehicle[Model=$model][count(. | key('vehicle-by-series', concat(Model, '|', Series))[1]) = 1]">
<xsl:value-of select="Model"/>
<xsl:text>: </xsl:text>
<xsl:value-of select="Series"/>
<xsl:text>, </xsl:text>
<xsl:variable name="grp" select="key('vehicle-by-series', concat(Model, '|', Series))" />
<xsl:value-of select="count($grp)"/>
<xsl:text> in stock, starting from </xsl:text>
<xsl:for-each select="$grp">
<xsl:sort select="Price" data-type="number" order="ascending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="Price"/>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

How to apply a function to a sequence of nodes in XSLT

I need to write an XSLT function that transforms a sequence of nodes into a sequence of strings. What I need to do is to apply a function to all the nodes in the sequence and return a sequence as long as the original one.
This is the input document
<article id="4">
<author ref="#Guy1"/>
<author ref="#Guy2"/>
</article>
This is how the calling site:
<xsl:template match="article">
<xsl:text>Author for </xsl:text>
<xsl:value-of select="#id"/>
<xsl:variable name="names" select="func:author-names(.)"/>
<xsl:value-of select="string-join($names, ' and ')"/>
<xsl:value-of select="count($names)"/>
</xsl:function>
And this is the code of the function:
<xsl:function name="func:authors-names">
<xsl:param name="article"/>
<!-- HELP: this is where I call `func:format-name` on
each `$article/author` element -->
</xsl:function>
What should I use inside func:author-names? I tried using xsl:for-each but the result is a single node, not a sequence.
<xsl:sequence select="$article/author/func:format-name(.)"/> is one way, the other is <xsl:sequence select="for $a in $article/author return func:format-name($a)"/>.
I am not sure you would need the function of course, doing
<xsl:value-of select="author/func:format-name(.)" separator=" and "/>
in the template of article should do.
If only a sequence of #ref values should be generated there is no need for a function or xsl version 2.0.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />
<xsl:template match="article">
<xsl:apply-templates select="author" />
</xsl:template>
<xsl:template match="author">
<xsl:value-of select="#ref"/>
<xsl:if test="position() !=last()" >
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:template>
</xsl:styleshee
This will generate:
#Guy1,#Guy2
Update:
Do have the string join by and and have a count of items. Try this:
<xsl:template match="article">
<xsl:text>Author for </xsl:text>
<xsl:value-of select="#id"/>
<xsl:apply-templates select="author" />
<xsl:value-of select="count(authr[#ref])"/>
</xsl:template>
<xsl:template match="author">
<xsl:value-of select="#ref"/>
<xsl:if test="position() !=last()" >
<xsl:text> and </xsl:text>
</xsl:if>
</xsl:template>
With this output:
Author for 4#Guy1 and #Guy20

Using max in a variable in XSLT 1.0

can I use max function in a variable in XSLT 1?
I need to find a maximum value inside some nodes and I'll need to call this from more places.
So I tried to create a template:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:essox="urn:essox-scripts">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template name="Field001_max_dluznych_splatek">
<xsl:param name="CrRep"/>
<xsl:variable name="PocetDluznychSplatekSplatky">
<xsl:value-of
select="max($CrRep
/Response
/ContractData
/Installments
/InstDetail
/NrOfDueInstalments)" />
</xsl:variable>
<xsl:variable name="PocetDluznychSplatekKarty">
<xsl:value-of
select="max($CrRep
/Response
/ContractData
/Cards
/CardDetail
/NrOfDueInstalments)" />
</xsl:variable>
<xsl:choose>
<xsl:when test="$PocetDluznychSplatekSplatky
>= $PocetDluznychSplatekKarty">
<xsl:value-of select="$PocetDluznychSplatekSplatky"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$PocetDluznychSplatekKarty"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
In XML Spy I get this error:
Error in XPath expression Unknown
function - Name and number of
arguments do not match any function
signature in the static context -
'max'.
What is wrong?
Thanks a lot,
Peter
Use the well known maximum idiom:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:essox="urn:essox-scripts">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template name="Field001_max_dluznych_splatek">
<xsl:param name="CrRep"/>
<xsl:variable name="PocetDluznychSplatekSplatky">
<xsl:call-template name="maximun">
<xsl:with-param name="pSequence"
select="$CrRep
/Response
/ContractData
/Installments
/InstDetail
/NrOfDueInstalments"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="PocetDluznychSplatekSplatky">
<xsl:call-template name="maximun">
<xsl:with-param name="pSequence"
select="$CrRep
/Response
/ContractData
/Cards
/CardDetail
/NrOfDueInstalments"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$PocetDluznychSplatekSplatky
>= $PocetDluznychSplatekKarty">
<xsl:value-of select="$PocetDluznychSplatekSplatky"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$PocetDluznychSplatekKarty"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="maximun">
<xsl:param name="pSequence"/>
<xsl:for-each select="$pSequence">
<xsl:sort select="." data-type="number" order="descending"/>
<xsl:if test="position()=1">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Note: In a named template for reuse.
There is no max function in XSLT 1.0. You can work around this by sorting your elements in descending order and then taking the value of the first one.
Here's another (slower) way to do it:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="PocetDluznychSplatekSplatky"
select="/test/PocetDluznychSplatekSplatky/val[not(../val > .)][1]" />
<xsl:variable name="PocetDluznychSplatekKarty"
select="/test/PocetDluznychSplatekKarty/val[not(../val > .)][1]" />
<xsl:template match="/">
<xsl:choose>
<xsl:when
test="$PocetDluznychSplatekSplatky >=
$PocetDluznychSplatekKarty">
<xsl:value-of select="$PocetDluznychSplatekSplatky" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$PocetDluznychSplatekKarty" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Sample source document:
<test>
<PocetDluznychSplatekSplatky>
<val>22</val>
<val>3241</val>
<val>13</val>
</PocetDluznychSplatekSplatky>
<PocetDluznychSplatekKarty>
<val>1</val>
<val>3234341</val>
<val>13</val>
</PocetDluznychSplatekKarty>
</test>
Output:
3234341
The XPath in each variable's select looks like this:
/test/PocetDluznychSplatekSplatky/val[not(../val > .)][1]
Or, select the val element having no val siblings with a greater value (i.e. the max).
(Obviously, you'll need to adjust the XPath to fit your source document.)
Note: The sort solution performs much better (assuming an n*log(n) sort implementation). The second approach needs to compare each val to every one of its siblings and is therefore quadratic.

XSLT concat string, remove last comma

I need to build up a string using XSLT and separate each string with a comma but not include a comma after the last string. In my example below I will have a trailing comma if I have Distribution node and not a Note node for instance. I don't know of anyway to build up a string as a variable and then truncate the last character in XSLT. Also this is using the Microsoft XSLT engine.
My String =
<xsl:if test="Locality != ''">
<xsl:value-of select="Locality"/>,
</xsl:if>
<xsl:if test="CollectorAndNumber != ''">
<xsl:value-of select="CollectorAndNumber"/>,
</xsl:if>
<xsl:if test="Institution != ''">
<xsl:value-of select="Institution"/>,
</xsl:if>
<xsl:if test="Distribution != ''">
<xsl:value-of select="Distribution"/>,
</xsl:if>
<xsl:if test="Note != ''">
<xsl:value-of select="Note"/>
</xsl:if>
[Man there's gotta be a better way to enter into this question text box :( ]
This is very easy to accomplish with XSLT (No need to capture the results in a variable, or to use special named templates):
I. XSLT 1.0:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*/*">
<xsl:for-each select=
"Locality/text() | CollectorAndNumber/text()
| Institution/text() | Distribution/text()
| Note/text()
"
>
<xsl:value-of select="."/>
<xsl:if test="not(position() = last())">,</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document:
<root>
<record>
<Locality>Locality</Locality>
<CollectorAndNumber>CollectorAndNumber</CollectorAndNumber>
<Institution>Institution</Institution>
<Distribution>Distribution</Distribution>
<Note></Note>
<OtherStuff>Unimportant</OtherStuff>
</record>
</root>
the wanted result is produced:
Locality,CollectorAndNumber,Institution,Distribution
If the wanted elements should be produced not in document order (something not required in the question, but raised by Tomalak), it is still quite easy and elegant to achieve this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="porderedNames"
select="' CollectorAndNumber Locality Distribution Institution Note '"/>
<xsl:template match="/*/*">
<xsl:for-each select=
"*[contains($porderedNames, concat(' ',name(), ' '))]">
<xsl:sort data-type="number"
select="string-length(
substring-before($porderedNames,
concat(' ',name(), ' ')
)
)"/>
<xsl:value-of select="."/>
<xsl:if test="not(position() = last())">,</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Here the names of the wanted elements and their wanted order are provided in the string parameter $porderedNames, which contains a space-separated list of all wanted names.
When the above transformation is applied on the same XML document, the wanted result is produced:
CollectorAndNumber,Locality,Distribution,Institution
II. XSLT 2.0:
In XSLT this task is even simpler (again, no special function is necessary):
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*/*">
<xsl:value-of separator="," select=
"(Locality, CollectorAndNumber,
Institution, Distribution,
Note)[text()]" />
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the same XML document, the same correct result is produced:
Locality,CollectorAndNumber,Institution,Distribution
Do note that the wanted elements will be produced in any desired order, because we are using the XPath 2.0 sequence type (vs the union in the XSLT 1.0 solution), which by definition contains items in any desired (specified) order.
I would prefer a short call-template to join the node values together. This also works if a node in the middle of your concatenated list, e.g. Institution, is missing:
<xsl:template name="join">
<xsl:param name="list" />
<xsl:param name="separator"/>
<xsl:for-each select="$list">
<xsl:value-of select="." />
<xsl:if test="position() != last()">
<xsl:value-of select="$separator" />
</xsl:if>
</xsl:for-each>
</xsl:template>
Here is a short example how to use it:
Sample input document:
<?xml version="1.0" encoding="utf-8"?>
<items>
<item>
<Locality>locality1</Locality>
<CollectorAndNumber>collectorAndNumber1</CollectorAndNumber>
<Distribution>distribution1</Distribution>
<Note>note1</Note>
</item>
<item>
<Locality>locality2</Locality>
<CollectorAndNumber>collectorAndNumber2</CollectorAndNumber>
<Institution>institution2</Institution>
<Distribution>distribution2</Distribution>
<Note>note2</Note>
</item>
<item>
<Locality>locality3</Locality>
<CollectorAndNumber>collectorAndNumber3</CollectorAndNumber>
<Institution>institution3</Institution>
<Distribution>distribution3</Distribution>
</item>
</items>
XSL transformation:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<summary>
<xsl:apply-templates />
</summary>
</xsl:template>
<xsl:template match="item">
<item>
<xsl:call-template name="join">
<xsl:with-param name="list" select="Locality | CollectorAndNumber | Institution | Distribution | Note" />
<xsl:with-param name="separator" select="','" />
</xsl:call-template>
</item>
</xsl:template>
<xsl:template name="join">
<xsl:param name="list" />
<xsl:param name="separator"/>
<xsl:for-each select="$list">
<xsl:value-of select="." />
<xsl:if test="position() != last()">
<xsl:value-of select="$separator" />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Generated output document:
<?xml version="1.0" encoding="utf-8"?>
<summary>
<item>locality1,collectorAndNumber1,distribution1,note1</item>
<item>locality2,collectorAndNumber2,institution2,distribution2,note2</item>
<item>locality3,collectorAndNumber3,institution3,distribution3</item>
</summary>
NB: If you were using XSLT/XPath 2.0 then there would be fn:string-join
fn:string-join**($operand1 as string*, $operand2 as string*) as string
which could be used as follows:
fn:string-join({Locality, CollectorAndNumber, Distribution, Note}, ",")
Supposing you have something like the following input XML:
<root>
<record>
<Locality>Locality</Locality>
<CollectorAndNumber>CollectorAndNumber</CollectorAndNumber>
<Institution>Institution</Institution>
<Distribution>Distribution</Distribution>
<Note>Note</Note>
<OtherStuff>Unimportant</OtherStuff>
</record>
</root>
Then this template would do it:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="text" />
<xsl:template match="record">
<xsl:variable name="values">
<xsl:apply-templates mode="concat" select="Locality" />
<xsl:apply-templates mode="concat" select="CollectorAndNumber" />
<xsl:apply-templates mode="concat" select="Institution" />
<xsl:apply-templates mode="concat" select="Distribution" />
<xsl:apply-templates mode="concat" select="Note" />
</xsl:variable>
<xsl:value-of select="substring($values, 1, string-length($values) - 1)" />
<xsl:value-of select="'
'" /><!-- LF -->
</xsl:template>
<xsl:template match="Locality | CollectorAndNumber | Institution | Distribution | Note" mode="concat">
<xsl:value-of select="." />
<xsl:text>,</xsl:text>
</xsl:template>
</xsl:stylesheet>
Output on my system:
Locality,CollectorAndNumber,Institution,Distribution,Note
I think it might be useful to mention,
position() doesn't work right when I use a complicated select
that filters some nodes,
in that case I came up which this trick:
you can define a string variable that hold value of nodes, separated
by a specific character, then by using str:tokenize()
you can create a complete node list which position works fine with it.
something like this:
<!-- Since position() doesn't work as expected(returning node position of current
node list), I got round it by a string variable and tokenizing it in which
absolute position is equal to relative(context) position. -->
<xsl:variable name="measObjLdns" >
<xsl:for-each select="h:measValue[#measObjLdn=$currentMeasObjLdn]/h:measResults" >
<xsl:value-of select="concat(.,'---')"/> <!-- is an optional separator. -->
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="str:tokenize($measObjLdns,'---')" ><!-- Since position() doesn't
work as expected(returning node position of current node list),
I got round it by a string variable and tokenizing it in which
absolute position is equal to relative(context) position. -->
<xsl:value-of select="."></xsl:value-of>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
Do you not have a value that is always going to be there? If you do then you can turn it around and put commas infront of everything apart from the first item (which would be your value that's always there).
This would be a bit messy but might do the trick if there's only a few elements like in your example:
<xsl:if test="Locality != ''">
<xsl:value-of select="Locality"/>
<xsl:if test="CollectorAndNumber != '' or Institution != '' or Distribution != '' or Note != ''">
<xsl:value-of select="','"/>
</xsl:if>
</xsl:if>
<xsl:if test="CollectorAndNumber != ''">
<xsl:value-of select="CollectorAndNumber"/>
<xsl:if test="Institution != '' or Distribution != '' or Note != ''">
<xsl:value-of select="','"/>
</xsl:if>
</xsl:if>
<xsl:if test="Institution != ''">
<xsl:value-of select="Institution"/>
<xsl:if test="Distribution != '' or Note != ''">
<xsl:value-of select="','"/>
</xsl:if>
</xsl:if>
<xsl:if test="Distribution != ''">
<xsl:value-of select="Distribution"/>
<xsl:if test="Note != ''">
<xsl:value-of select="','"/>
</xsl:if>
</xsl:if>
<xsl:if test="Note != ''">
<xsl:value-of select="Note"/>
</xsl:if>