adding attribute to the node

Part 1. So let’s say if the country id is equal to 32 then it should add attribute country=32 to Employee node. This transformation: <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <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=”Employee[countryid=32]”> <Employee countryid=”{countryid}”> <xsl:apply-templates select=”@*|node()”/> </Employee> </xsl:template> </xsl:stylesheet> when applied on the provided XML document: … Read more

How to parse string to date in xslt 2.0

Like Tomalak said, you can use substring() and concat() to build a string you can cast as an xs:date() (It doesn’t sound like you want a dateTime.) Example: <xsl:stylesheet version=”2.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:xs=”http://www.w3.org/2001/XMLSchema”> <xsl:output method=”text”/> <xsl:strip-space elements=”*”/> <xsl:variable name=”in” select=”‘30042013′”/> <xsl:template match=”https://stackoverflow.com/”> <xsl:variable name=”date” select=”xs:date(concat( substring($in,5,4),’-‘, substring($in,3,2),’-‘, substring($in,1,2)))”/> <xsl:value-of select=”format-date($date,'[MNn] [D], [Y]’)”/> </xsl:template> </xsl:stylesheet> produces (with … Read more

Use saxon with python

There are two possible approaches: set up an HTTP service that accepts tranformation requests and implements them by invoking Saxon from Java; you can then send the transformation requests from Python over HTTP use the Saxon/C product, currently available on prerelease: details here: http://www.saxonica.com/saxon-c/index.xml

Preserving entity references when transforming XML with XSLT?

If you know what entities will be used and how they are defined, you can do the following (quite primitive and error-prone, but still better than nothing): <xsl:stylesheet version=”2.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:xs=”http://www.w3.org/2001/XMLSchema” xmlns:my=”my:my”> <xsl:output omit-xml-declaration=”yes” indent=”yes”/> <xsl:character-map name=”mapEntities”> <xsl:output-character character=”&amp;” string=”&amp;”/> </xsl:character-map> <xsl:variable name=”vEntities” select= “‘stackoverflow’, ‘How can I preserve the entity reference when transforming with … Read more

What browsers support XSLT 2.0?

Browsers do not yet support XSLT 2.0, natively. Saxon 9 CE is a JavaScript-based XSLT 2.0 implementation. Frameless is another, more light-weight XSLT 2.0 implementation in the browser, supporting large parts of the XSLT 2.0 and XPath 2.0 functionality See also: How can I make XSLT work in chrome? https://developer.mozilla.org/en/docs/XSLT_2.0 http://blogs.msdn.com/b/dareobasanjo/archive/2004/05/13/131166.aspx

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

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

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