dynamic xpath in xslt?

Dynamic XPath evaluation is not possible in pure XSLT 1.0 or 2.0. There are at least three ways to do this in a “hybrid” solution: I. Use the EXSLT function dyn:evaluate() Unfortunately, very few XSLT 1.0 processors implement dyn:evaluate(). II. Process the XML document with XSLT and generate a new XSLT file that contains the … 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

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

XSLT generate UUID

Here’s a good example. Basically you set up an extension that points to the java UUID class, and then reference it in the XSL: <xsl:stylesheet version=”2.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:uuid=”java:java.util.UUID”> <xsl:template match=”https://stackoverflow.com/”> <xsl:variable name=”uid” select=”uuid:randomUUID()”/> <xsl:value-of select=”$uid”/> </xsl:template>