How to read plain text content with XSLT 1.0

It is not possible for the input document to be plain text because the input to an XSLT 1.0 transformation must be well-formed XML. Here are some alternative ways to access plain text in an XSLT transformation: Use unparsed-text in XSLT 2.0. Pass the plain text in via top-level parameters (xsl:param). Preprocess the text file … Read more

Multiple groupings of XML nodes

It’s difficult to see how exactly the output relates to the input. Try this as your starting point: XSLT 1.0 <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:strip-space elements=”*”/> <xsl:key name=”transports-by-destination” match=”transports” use=”destination” /> <xsl:key name=”transports-by-assortment” match=”transports” use=”concat(destination, ‘|’, assortment)” /> <xsl:template match=”/*”> <xsl:copy> <!– for each unique destination –> <xsl:for-each select=”transports[count(. | key(‘transports-by-destination’, … Read more

Create node set and pass as a parameter

There is a way (non-standard) in XSLT 1.0 to create temporary trees dynamically and evaluate XPath expressions on them, however this requires using the xxx:node-set() function. Whenever nodes are dynamically created inside the body of an xsl:variable or an xsl:param, the type of that xsl:variable / xsl:param is RTF (Result Tree Fragment) and the W3 … Read more

Multiply 2 numbers and then sum

Here are three possible solutions: Solution1 XSLT2: <xsl:stylesheet version=”2.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:output method=”text”/> <xsl:template match=”https://stackoverflow.com/”> <xsl:sequence select=”sum(/*/*/(rate * quantity))”/> </xsl:template> </xsl:stylesheet> When this transformation is applied on the following XML document: <parts> <part> <rate>0.37</rate> <quantity>10</quantity> </part> <part> <rate>0.03</rate> <quantity>10</quantity> </part> </parts> The wanted result is produced: 4 The XSLT 2.0 solution uses the fact that in … Read more

XSL xsl:template match=”/”

The value of the match attribute of the <xsl:template> instruction must be a match pattern. Match patterns form a subset of the set of all possible XPath expressions. The first, natural, limitation is that a match pattern must select a set of nodes. There are also other limitations. In particular, reverse axes are not allowed … Read more

For loops vs. apply-templates

This question is a bit argumentative, but here is my take on it. I can’t see a clear line when to use loops and when to use templates. I’d say that you shoud strive to avoid for-each as often as possible in favor of apply-templates. Using for-each makes your program more complex by adding nesting … Read more

Date operations on xsl 1.0

Adding/subtracting number of days to/from date in pure XSLT 1.0: <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:param name=”givenDate” select=”‘2014-05-13T00:00:00′”/> <xsl:param name=”daysDiff” select=”-3″/> <xsl:variable name=”JDN”> <xsl:call-template name=”JDN”> <xsl:with-param name=”date” select=”$givenDate” /> </xsl:call-template> </xsl:variable> <xsl:variable name=”newDate”> <xsl:call-template name=”GD”> <xsl:with-param name=”JDN” select=”$JDN + $daysDiff” /> </xsl:call-template> </xsl:variable> <xsl:template match=”https://stackoverflow.com/”> <output> <GivenDate><xsl:value-of select=”$givenDate”/></GivenDate> <NewDate><xsl:value-of select=”$newDate”/></NewDate> </output> </xsl:template> … Read more

Conver EDT to GMT in XSLT 1.0

To convert between two timezones with a known offset between them in pure XSLT 1.0, you can use the following example: XML <input>2017-09-12T15:03:22.0000000</input> XSLT 1.0 <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=”input”> <output> <xsl:call-template name=”add-hours-to-dateTime”> <xsl:with-param name=”dateTime” select=”.”/> </xsl:call-template> </output> </xsl:template> <xsl:template name=”add-hours-to-dateTime”> <xsl:param name=”dateTime”/> <xsl:param name=”hours” select=”4″/> <xsl:variable name=”dateTime-in-seconds”> <xsl:call-template name=”dateTime-to-seconds”> … Read more

Tokenizing and sorting with XSLT 1.0

Here’s an inefficient pure version 1 solution: <!– Sort the tokens –> <xsl:template name=”sortTokens”> <xsl:param name=”tokens” select=”””/> <!– The list of tokens –> <xsl:param name=”separator” select=”‘ ‘”/> <!– What character separates the tokens? –> <xsl:param name=”pivot” select=”””/> <!– A pivot word used to divide the list –> <xsl:param name=”lessThan” select=”””/> <!– Accumulator for tokens less … Read more

Finding the difference between two dateTimes in XSLT

I am getting the error: … xsl:version: only 1.0 features are supported Here’s a purely XSLT 1.0 solution: <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:strip-space elements=”*”/> <xsl:template match=”event”> <xsl:variable name=”start”> <xsl:call-template name=”dateTime-to-seconds”> <xsl:with-param name=”dateTime” select=”start/@time” /> </xsl:call-template> </xsl:variable> <xsl:variable name=”end”> <xsl:call-template name=”dateTime-to-seconds”> <xsl:with-param name=”dateTime” select=”end/@time” /> </xsl:call-template> </xsl:variable> <xsl:variable name=”duration” select=”$end – $start” … Read more