XSLT getting last element
You need to put the last() indexing on the nodelist result, rather than as part of the selection criteria. Try: (//element[@name=”D”])[last()]
You need to put the last() indexing on the nodelist result, rather than as part of the selection criteria. Try: (//element[@name=”D”])[last()]
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
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:if test=”xpath-expression”>…</xsl:if> so for example <xsl:if test=”/html/body”>body node exists</xsl:if> <xsl:if test=”not(/html/body)”>body node missing</xsl:if>
Array and other collection indexes represent memory offsets, so logically enough they begin at zero. XML and XPATH indexes represent positions and counts, so logically enough they begin at one (and zero is therefore representative of “empty”)
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
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
This is not at all simple. Basically, you are asking how to render an HTML table visually, by positioning each cell on a (equi-spaced) x-y grid of rows and columns. This is complex, because the position of each cell depends not only on the width (colspan) of the preceding cells in the same row, but … Read more
The general rules for escaping are: In 1.0: if you want the attribute delimiter in a string literal, use the XML escape form " or ' if you want the string delimiter in a string literal, you’re hosed In 2.0: if you want the attribute delimiter in a string literal, use the XML escape form … Read more
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>