Reputation: 161
I am newbie to xslt. i want to generate 32 digit long UUID using xslt and add it to the xml coming as an input. I tried to use the random function of math liberary but getting error.
Input XML
<users xmlns="ABC_Login">
<email>[email protected]</email>
</users>
XSLT Snippet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="ABC_Login" xmlns:math="http://exslt.org/math"
extension-element-prefixes="math" version="2.0">
<xsl:template match="/ns1:users">
<users>
<email>
<xsl:value-of select="ns1:email" />
</email>
<UUID>
<xsl:value-of select="(floor(math:random()*10) mod 10) + 1" />
</UUID>
</users>
</xsl:template>
</xsl:stylesheet>
i am using online editor but getting the exception as below. http://xslttest.appspot.com/
Error: Cannot find a matching 0-argument function named {http://exslt.org/math}random(). There is no Saxon extension function with the local name random
Actually i need to generate random token using xslt and add it in the input xml.
Expected Output
<users xmlns="ABC_Login">
<email>[email protected]</email>
<uuid>7B81A9B0D9-CA0E-E70F-ADFF-116EE7A1A980<</uuid>
</users>
Can anybody help me in this regard. Best Regards,
Upvotes: 0
Views: 14686
Reputation: 117073
The reason you are getting an error is that you are using an XSLT 2.0 processor (Saxon 9) which does not support the EXSLT math:random()) function.
Unfortunately, there is no native random() function in XSLT 2.0 either, but with Saxon you can call a Java method - for example:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:math="java.lang.Math"
exclude-result-prefixes="math">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:variable name="random" select="math:random()"/>
<xsl:value-of select="$random"/>
</output>
</xsl:template>
</xsl:stylesheet>
to generate a random number or:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:uuid="java.util.UUID"
exclude-result-prefixes="uuid">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:variable name="random" select="uuid:randomUUID()"/>
<xsl:value-of select="$random"/>
</output>
</xsl:template>
</xsl:stylesheet>
to generate a random UUID
Upvotes: 10