生成一个32字符的随机字符串,使用XSLT。

huangapple go评论52阅读模式
英文:

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?

    &lt;xsl:stylesheet version=&quot;1.0&quot; 
xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
exclude-result-prefixes=&quot;xs&quot;&gt;
&lt;xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; indent=&quot;yes&quot;/&gt;

  &lt;xsl:template match=&quot;node()&quot;&gt;
      &lt;xsl:apply-templates/&gt;
  &lt;/xsl:template&gt;

&lt;xsl:template match=&quot;/&quot;&gt;

&lt;ABC&gt;
&lt;xsl:for-each select=&quot;ABC/ABC&quot;&gt;
&lt;DEF&gt;
 
&lt;externalCode&gt;&lt;xsl:value-of select=&quot;concat(generate-id(),generate-id(),generate-id(),generate-id())&quot;/&gt;&lt;/externalCode&gt;
&lt;userId&gt;&lt;xsl:value-of select=&quot;userId&quot;/&gt;&lt;/userId&gt;

&lt;/DEF&gt; 
&lt;/xsl:for-each&gt;
&lt;/ABC&gt; 

&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

Thanks

答案1

得分: 2

XSLT中有一个random-number-generator()函数,返回一个包含permute函数的映射,用于以随机顺序返回输入序列,因此,根据您的要求,从包含小写字母、大写字母和数字的序列中取32个字符(这些字符远远多于32个),可能足够调用如下:

string-join((random-number-generator(current-dateTime())?permute($symbol-seq))[position() le 32], &#39;&#39;)

由于另一个回答中已经使用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], &#39;&#39;)

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 = &#39;&#39;&#39;&lt;xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
	xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
	xmlns:mf=&quot;http://example.com/mf&quot;
	exclude-result-prefixes=&quot;#all&quot;
	expand-text=&quot;yes&quot;
	version=&quot;3.0&quot;&gt;
  
  &lt;xsl:param name=&quot;symbols&quot; as=&quot;xs:string&quot; expand-text=&quot;no&quot;&gt;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&lt;/xsl:param&gt;
  
  &lt;xsl:variable name=&quot;symbol-seq&quot; as=&quot;xs:string*&quot; select=&quot;($symbols =&gt; string-to-codepoints()) ! codepoints-to-string(.)&quot;/&gt;

  &lt;xsl:template name=&quot;xsl:initial-template&quot;&gt;
    &lt;test&gt;{string-join((random-number-generator(current-dateTime())?permute($symbol-seq))[position() le 32], &#39;&#39;)}&lt;/test&gt;
  &lt;/xsl:template&gt;
  
&lt;/xsl:stylesheet&gt;&#39;&#39;&#39;

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
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;test&gt;OQKMahpLesBCI3lUuGR6jEYf1yqZiWJw&lt;/test&gt;

答案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:

&lt;externalCode&gt;m4lrO26bujNjS1FopZ7jv31Di1zK7xdP&lt;/externalCode&gt;

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 &quot;import string,random; print(&#39;&#39;.join(random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits, k=32)))&quot;

Sample output: 4Ve3o1oURX3oVzn5954SnVLlqviWp9uN


Using the above implementation as part of an XSLT transform:

xsltproc --stringparam random $(python3 -c &quot;import string,random; print(&#39;&#39;.join(random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits, k=32)))&quot;) p.xslt p.xml

XSLT output:

&lt;externalCode&gt;m4lrO26bujNjS1FopZ7jv31Di1zK7xdP&lt;/externalCode&gt;

Contents of p.xslt:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;&gt;

  &lt;xsl:output method=&quot;xml&quot; omit-xml-declaration=&quot;yes&quot;/&gt;

  &lt;xsl:template match=&quot;/&quot;&gt;
      &lt;externalCode&gt;
      &lt;xsl:value-of select=&quot;$random&quot;/&gt;
      &lt;/externalCode&gt;
  &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;

Contents of p.xml:

&lt;data&gt;
  dummy_xml
&lt;/data&gt;

huangapple
  • 本文由 发表于 2023年2月14日 21:36:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定