英文:
Generate a 32 character random string using XSLT
问题
我有一个需求,需要使用XSLT生成一个包含大写字母、小写字母和数字的32字符随机字符串值。
我现在正在使用以下代码,但生成的值太相似,而且没有包括大写字母。
是否有其他方法可以实现这个需求?
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/">
<ABC>
<xsl:for-each select="ABC/ABC">
<DEF>
<externalCode><xsl:value-of select="concat(generate-id(),generate-id(),generate-id(),generate-id())"/></externalCode>
<userId><xsl:value-of select="userId"/></userId>
</DEF>
</xsl:for-each>
</ABC>
</xsl:template>
</xsl:stylesheet>
希望这对你有帮助。
英文:
I have a requirement wherein I need to generate a 32-character random string value using XSLT containing upper case letters, lower case letters & numbers.
I am using the below code for this right now, however, the values generated are too similar and upper case letters are not included.
Is there an alternate way to achieve this?
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/">
<ABC>
<xsl:for-each select="ABC/ABC">
<DEF>
<externalCode><xsl:value-of select="concat(generate-id(),generate-id(),generate-id(),generate-id())"/></externalCode>
<userId><xsl:value-of select="userId"/></userId>
</DEF>
</xsl:for-each>
</ABC>
</xsl:template>
</xsl:stylesheet>
Thanks
答案1
得分: 2
XSLT中有一个random-number-generator()
函数,返回一个包含permute
函数的映射,用于以随机顺序返回输入序列,因此,根据您的要求,从包含小写字母、大写字母和数字的序列中取32个字符(这些字符远远多于32个),可能足够调用如下:
string-join((random-number-generator(current-dateTime())?permute($symbol-seq))[position() le 32], '')
由于另一个回答中已经使用Python,现在SaxonC HE 12作为PyPi包可用,因此您可以完全在XSLT 3中执行,但如果需要的话也可以从Python运行:
from saxonche import *
xslt = 'XSLT样式表内容'
with PySaxonProcessor(license=False) as proc:
print(proc.version)
xslt30_processor = proc.new_xslt30_processor()
xslt30_transformer = xslt30_processor.compile_stylesheet(stylesheet_text=xslt)
if xslt30_processor.exception_occurred:
print(xslt30_processor.error_message)
else:
result = xslt30_transformer.call_template_returning_string()
print(result)
结果输出如下:
SaxonC-HE 12.0 from Saxonica
<?xml version="1.0" encoding="UTF-8"?><test>OQKMahpLesBCI3lUuGR6jEYf1yqZiWJw</test>
请注意,我只翻译了您提供的内容,没有包括问题的回答。
英文:
XSLT has a random-number-generator()
function returning a map with a permute
function to return an input sequence in random order so given that your requirement is 32 characters taken from a sequence of all letters in lower case and upper case and digits (which are much more than 32) it will probably suffice to call e.g.
string-join((random-number-generator(current-dateTime())?permute($symbol-seq))[position() le 32], '')
As the other answer uses Python anyway, SaxonC HE 12 is now available as a PyPi package so you can do it all in XSLT 3 but if wanted/needed run from Python:
from saxonche import *
xslt = '''<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
expand-text="yes"
version="3.0">
<xsl:param name="symbols" as="xs:string" expand-text="no">abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</xsl:param>
<xsl:variable name="symbol-seq" as="xs:string*" select="($symbols => string-to-codepoints()) ! codepoints-to-string(.)"/>
<xsl:template name="xsl:initial-template">
<test>{string-join((random-number-generator(current-dateTime())?permute($symbol-seq))[position() le 32], '')}</test>
</xsl:template>
</xsl:stylesheet>'''
with PySaxonProcessor(license=False) as proc:
print(proc.version)
xslt30_processor = proc.new_xslt30_processor()
xslt30_transformer = xslt30_processor.compile_stylesheet(stylesheet_text=xslt)
if xslt30_processor.exception_occurred:
print(xslt30_processor.error_message)
else:
result = xslt30_transformer.call_template_returning_string()
print(result)
Result output is e.g.
SaxonC-HE 12.0 from Saxonica
<?xml version="1.0" encoding="UTF-8"?><test>OQKMahpLesBCI3lUuGR6jEYf1yqZiWJw</test>
答案2
得分: 0
这是要翻译的部分:
One possibility might be to generate the 32 character random string external to the XSLT processing and pass that generated value as a --stringparam
to your XSLT transform.
Here is a python3 implementation for generating a random 32 character string with uppercase, lowercase, and digits:
Sample output: 4Ve3o1oURX3oVzn5954SnVLlqviWp9uN
Using the above implementation as part of an XSLT transform:
XSLT output:
<externalCode>m4lrO26bujNjS1FopZ7jv31Di1zK7xdP</externalCode>
Contents of p.xslt:
Contents of p.xml:
英文:
One possibility might be to generate the 32 character random string external to the XSLT processing and pass that generated value as a --stringparam
to your XSLT transform.
Here is a python3 implementation for generating a random 32 character string with uppercase, lowercase, and digits:
python3 -c "import string,random; print(''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits, k=32)))"
Sample output: 4Ve3o1oURX3oVzn5954SnVLlqviWp9uN
Using the above implementation as part of an XSLT transform:
xsltproc --stringparam random $(python3 -c "import string,random; print(''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits, k=32)))") p.xslt p.xml
XSLT output:
<externalCode>m4lrO26bujNjS1FopZ7jv31Di1zK7xdP</externalCode>
Contents of p.xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<externalCode>
<xsl:value-of select="$random"/>
</externalCode>
</xsl:template>
</xsl:stylesheet>
Contents of p.xml:
<data>
dummy_xml
</data>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论