Only element in xslt result set is not the last element? - xslt-2.0

I have the following snippet
<xsl:for-each select="book">
<xsl:value-of select="title"/><xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
I want it to be a list of comma-separated book titles.
The output is correct when I have no books in the list. It is also correct when I have more than one book.
However, if I have exactly one book, it prints a comma at the end.
When I change the condition to only print a comma if it is the last element, the case where there is only one book doesn't print a comma.
So it seems like when there is only one element it is not treated as the last element. How can I deal with this?
Sample XML: looks something like this
<booklist>
<book>
<title>My book</title>
<author>Some author</author>
</book>
<book>
<title>Another book</title>
<author>Another author</author>
</book>
</booklist>
When there are two books in the list, I get
My book, Another book
When I delete the second entry, I get
My book,
EDIT:
I found the issue. I was using an <xsl:apply-templates> to select a set of data, and then inside another template for each book, I was doing the position check. However, last() was referring to the original set of data and not the filtered subset.

The following works fine for me in Firefox (I get no commas for no books, and n-1 commas for n books if n>0). I suspect your XSLT processor is broken.
The XSLT ("x.xsl"):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="iso-8859-1" media-type="text/xhtml" doctype-public="-//W3C//DTD HTML 4.0//EN"/>
<xsl:template match="/">
<xsl:for-each select="root/book">
<xsl:value-of select="title"/><xsl:if test="position() != last()">;</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="x.xsl" type="text/xsl"?>
<root>
<book>
<title>A</title>
</book>
<book>
<title>B</title>
</book>
</root>

What's "test()"? I think you want "last()-1".

Related

XSLT 3.0 Convert String data type to Date type and apply translate to remove characters

Here is my source XML
<?xml version="1.0" encoding="UTF-8"?>
<Compensation>
<Salary>
<BasePay>$18600.1299</BasePay>
<PayDate>15-Mar-2022</PayDate>
<Bonus>$3500.99</Bonus>
<Gym>$670</Gym>
<Tax>$30,000.9912</Tax>
</Salary>
<Salary>
<BasePay>$28600.12</BasePay>
<PayDate>15-Mar-2022</PayDate>
<Bonus>$1500.99</Bonus>
<Gym/>
<Tax>$50,000</Tax>
</Salary>
</Compensation>
I am trying do following on my XML document
Format date to YYYY-MM-DD format. Currently date on my source XML is of string data type
Remove all currency and commas from whole document.
Here is my XSLT 3.0 solution which is working fine.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:this="urn:this-stylesheet"
exclude-result-prefixes="xs this"
version="3.0">
<xsl:output method="xml" indent="yes"/>
<xsl:function name="this:fromatDate" as="xs:string">
<xsl:param name="dateString"/>
<xsl:variable name="month"
select="('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')"/>
<xsl:variable name="dd" select="substring($dateString,1,2)"/>
<xsl:variable name="mm"
select="format-number(index-of($month,substring($dateString,4,3)),'00')"/>
<xsl:variable name="yy" select="substring($dateString,8,4)"/>
<xsl:value-of select="format-date(xs:date( string-join(($yy, $mm, $dd), '-')),'[Y0001]-[M01]-[D01]')"/>
</xsl:function>
<xsl:mode on-no-match="shallow-copy"/>
<!-- Removes $ symbol and commas -->
<xsl:template match="text()">
<xsl:value-of select="translate(.,'$,', '')"/>
</xsl:template>
<!-- Convert string to date -->
<xsl:template match="PayDate">
<FormattedPayDate>
<xsl:value-of select="this:fromatDate(.)"/>
</FormattedPayDate>
</xsl:template>
</xsl:stylesheet>
I'm getting the expected result as below. However, I'd like to use anyone's help to know if there is any efficient way to write this code since I want to use XSLT 3.0.
I'm not sure if there is any functions in xpath 3.0 to handle string conversions to date and character removal.
<?xml version="1.0" encoding="UTF-8"?>
<Compensation>
<Salary>
<BasePay>18600.1299</BasePay>
<FormattedPayDate>2022-03-15</FormattedPayDate>
<Bonus>3500.99</Bonus>
<Gym>670</Gym>
<Tax>30000.9912</Tax>
</Salary>
<Salary>
<BasePay>28600.12</BasePay>
<FormattedPayDate>2022-03-15</FormattedPayDate>
<Bonus>1500.99</Bonus>
<Gym/>
<Tax>50000</Tax>
</Salary>
</Compensation>

How do I split and print values in xslt?

I want to create an xslt(version 2) where value can be split with delimeter ':' and print only first part of split and store the second part into a variable. The values have to be passed to a 'student' tag. Following are the values that are fetched from db
Adam:101
Brad:110
Chad:111
Expected output:
Adam
Brad
Chad
and values 101, 110 and 111 have to stored into a variable.
Please also provide a link where xslt2.0 tutorial is available in detail.
Simply you can use fn:tokenize() to achieve the output:
Assume input:
<student>Adam:101 Brad:110 Chad:111</student>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<students>
<xsl:apply-templates/>
</students>
</xsl:template>
<xsl:template match="student">
<xsl:for-each select="tokenize(., ' ')">
<student variable="{substring-after(., ':')}">
<xsl:value-of select="substring-before(., ':')"/>
</student>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
OUTPUT:
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student variable="101">Adam</student>
<student variable="110">Brad</student>
<student variable="111">Chad</student>
</students>
Link: https://xsltfiddle.liberty-development.net/gVrvcxx

Changing values that are the same from different nodes

I need to localize values within siblings that are the same. If they are the same I need to alter them.
I think I need to use following-sibling and preceding-sibling and group-by in some way. First group-by the value I am looking for so that I get the one's that are the same in the position after each other. Then using the sibling functions to find out if they are equal.
Sample:
<programs>
<event>
<start>2018-11-25T13:55:00</start>
</event>
<event>
<start>2018-11-27T17:00:00</start>
</event>
<event>
<start>2018-11-25T13:55:00</start>
</event>
<event>
<start>2018-11-25T13:55:00</start>
</event>
</programs>
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
<output>
<xsl:for-each select="/programs/event">
<xsl:variable name="starttime" select="./start"/>
<startOfProgram><xsl:value-of select="$starttime"/></startOfProgram>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Desired results:
<output>
<startOfProgram>2018-11-25T13:55:00</startOfProgram>
<startOfProgram>2018-11-25T13:56:00</startOfProgram>
<startOfProgram>2018-11-25T13:57:00</startOfProgram>
<startOfProgram>2018-11-27T17:00:00</startOfProgram>
</output>
I know this is a long shot so if anyone could point me in the right direction or help me with one part of the problem I'd be very grateful.
There is lots of other elements in the sample that I have taken out that is also carried though to the output. If it matters I can include a variety of them.
Ps. Note that the value could easily be 2018-11-25T18:30:00, which would then need to be 2018-11-25T18:30:00 and the consecutive 2018-11-25T18:31:00 if there are more of the same.
The result you have shown looks as if you want to group the values as xs:dateTime values and then simply add one minute to each item in the group depending on the position:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="programs">
<output>
<xsl:for-each-group select="event/start/xs:dateTime(.)" group-by=".">
<xsl:for-each select="current-group()">
<startOfProgram>{. + (position() - 1) * xs:dayTimeDuration('PT1M')}</startOfProgram>
</xsl:for-each>
</xsl:for-each-group>
</output>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/pPqsHUv/1 and the above is XSLT 3 but for an XSLT 2 processor I think you only need to change the text value template I have used to an xsl:value-of:
<startOfProgram><xsl:value-of select=". + (position() - 1) * xs:dayTimeDuration('PT1M')"/></startOfProgram>
See http://xsltransform.hikmatu.com/6qVRKvJ

XSLT 2.0: Overriding nodes with grandchild nodes

I'm trying to find a way to replace a node with one that has the same name deeper down in the tree. For example, with the following input:
<root>
<foo>
<a>1</a>
<b>2</b>
<c>3</c>
<bar>
<a>100</a>
<c>5000</c>
</bar>
</foo>
</root>
I'd like to produce something like this:
<root>
<foo>
<a>100</a>
<b>2</b>
<c>5000</c>
</foo>
</root>
I need to be able to replace any number of nodes, and I'd also like to figure out the list dynamically, rather than spell out all the possibilities because there's a chance that things will change in the future. One other requirement is that order of the parent nodes must remain intact. (To be specific, my final output is going to be a CSV file so the columns need to line up with the headers.)
This is my first attempt at learning XSLT and I'm totally stumped on this one! Any help would be greatly appreciated. I'm using XSLT 2.0, BTW.
Thanks,
Mark
I apologize for the nasty SO bug which doesn't indent the formatted code!
They can't fix this for months...
This transformation:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"*[*]
[every $leaf in .//*[not(*)]
satisfies
name($leaf) = preceding::*/name()
]
"/>
<xsl:template match=
"*[not(*) and name() = following::*/name()]">
<xsl:sequence select=
"following::*[name() = name(current())][1]"/>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<root>
<foo>
<a>1</a>
<b>2</b>
<c>3</c>
<bar>
<a>100</a>
<c>5000</c>
</bar>
</foo>
</root>
produces the wanted, correct result:
<root>
<foo>
<a>100</a>
<b>2</b>
<c>5000</c>
</foo>
</root>

Difference in template rule processing XSLT 1.0 vs 2.0 (bis)

See Diff 1.0 vs 2.0. That one is solved, but it is still a bit of a mystery to me what caused the issue in the first place.
Now I may have found something, but need help understanding what is going on.
I simplified the input xml to
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Manager>
<Employee grade="9"/>
<Employee grade="8"/>
</Manager>
<Manager>
<Employee grade="9"/>
<Employee grade="8"/>
<Employee grade="4"/>
</Manager>
</root>
The stylesheet I apply on this input is
<?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" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:apply-templates select="root/Manager"/>
</root>
</xsl:template>
<xsl:template match="Manager">
<test><xsl:value-of select="Employee/#grade"/></test>
</xsl:template>
</xsl:stylesheet>
The output is
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>9</test>
<test>9</test>
</root>
But running the transformation in XSLT 2.0 mode (change stylesheet/#version to "2.0"), the output is
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>9 8</test>
<test>9 8 4</test>
</root>
I wonder what precise difference in XSLT 1.0 and XSLT 2.0 causes this.
Well for the first difference I did explain that with XSLT 2.0 the comparison operators like less than or greater than or less than or equal and so on by default compare strings while with XSLT 1.0 these operators are only defined for numbers and that way convert any operands to numbers.
For this post the difference is simply that with XSLT 1.0 xsl:value-of select="foo" outputs the string value of the first foo element in the selected node set of foo elements while with XSLT 2.0 this has changed, if a sequence is selected then a space separated list of the string value of item in the sequence is output. You can change the separator (i.e. space) used with the separator attribute of xsl:value-of in XSLT 2.0. See also http://www.w3.org/TR/xslt20/#incompatibilities.

Resources