如何使用 xslt 生成随机 UUID [英] How to generate random UUID using xslt
问题描述
我是 xslt 的新手.我想使用 xslt 生成 32 位长的 UUID 并将其添加到作为输入的 xml 中.我尝试使用数学库的随机函数但出现错误.
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.
输入 XML
<users xmlns="ABC_Login">
<email>ABC@gmail.com</email>
</users>
XSLT 代码段
<?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>
我正在使用在线编辑器,但出现如下异常.http://xslttest.appspot.com/
i am using online editor but getting the exception as below. http://xslttest.appspot.com/
错误:找不到名为 {http://exslt.org/math} 的匹配 0 参数函数随机的().没有本地名称random的Saxon扩展函数
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
实际上我需要使用 xslt 生成随机令牌并将其添加到输入 xml 中.
Actually i need to generate random token using xslt and add it in the input xml.
预期产出
<users xmlns="ABC_Login">
<email>ABC@gmail.com</email>
<uuid>7B81A9B0D9-CA0E-E70F-ADFF-116EE7A1A980<</uuid>
</users>
任何人都可以在这方面帮助我.最好的问候,
Can anybody help me in this regard. Best Regards,
推荐答案
出现错误的原因是您使用的 XSLT 2.0 处理器 (Saxon 9) 不支持 EXSLT math:random()) 函数.
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.
不幸的是,XSLT 2.0 中也没有原生的 random() 函数,但是使用 Saxon,您可以调用 Java 方法 - 例如:
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>
生成随机数或:
<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>
生成随机UUID
这篇关于如何使用 xslt 生成随机 UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!