生成随机且唯一的字符串xslt

时间:2021-09-24 20:34:18

How can I generate a unique and random string with xslt in order to associate it to an attribute of a tag. for eg I want to add a unique id to this tag

如何使用xslt生成唯一且随机的字符串,以便将其与标记的属性相关联。例如,我想为此标记添加唯一ID

<generalization xmi:id="unique ID">

2 个解决方案

#1


0  

You can use generate-id() to create unique identifiers. To quote the standard, "This function returns a string that uniquely identifies a given node."

您可以使用generate-id()创建唯一标识符。引用标准,“此函数返回唯一标识给定节点的字符串。”

Consider this stylesheet:

考虑这个样式表:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Replace <xyzzy> with <generalization xml:id="unique ID"> -->
<xsl:template match="xyzzy">
  <generalization>
    <xsl:apply-templates select="@*"/>
    <xsl:attribute name="xml:id"><xsl:value-of select="generate-id()"/></xsl:attribute>
    <xsl:apply-templates/>
  </generalization>
</xsl:template>

<!-- Copy everything else straight thru -->
<xsl:template match="node( ) | @*">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>

</xsl:stylesheet>

applied to this input:

适用于此输入:

<?xml version='1.0' encoding='ASCII'?>
<root>
 <xyzzy/>
 <xyzzy a="b">
  <xyzzy xml:id="non-unique-id"/>
 </xyzzy>
</root>

With this result:

有了这个结果:

<?xml version="1.0"?>
<root>
 <generalization xml:id="idp28972496"/>
 <generalization a="b" xml:id="idp28945920">
  <generalization xml:id="idp28946416"/>
 </generalization>
</root>

Notice how the value of generate-id() is unique across the document.

请注意generate-id()的值在整个文档中是唯一的。

#2


0  

You can use generate-id() with a temporary XML node to create random strings of any length:

您可以将generate-id()与临时XML节点一起使用来创建任意长度的随机字符串:

<!-- language: lang-xml -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:kh="https://github.com/kohsah"
    exclude-result-prefixes="xs xd"
    version="2.0">
    <xsl:function name="kh:shortRandom">
        <xsl:variable name="mxml">
            <node/>
        </xsl:variable>
        <xsl:sequence select="generate-id($mxml//node)"/>
    </xsl:function>

    <xsl:function name="kh:longRandom">
        <xsl:sequence select="concat(kh:shortRandom(), kh:shortRandom(), kh:shortRandom(), kh:shortRandom())"></xsl:sequence>
    </xsl:function>

    <xsl:template match="/">
        <test>
            <randomId><xsl:value-of select="kh:shortRandom()"/></randomId>
            <guid><xsl:value-of select="kh:longRandom()"/></guid>
        </test>
    </xsl:template>

</xsl:stylesheet>

There are two XSL functions here:

这里有两个XSL函数:

  • kh:shortRandom() which generates short 4 letter random strings like this one: "d2e1"
  • kh:shortRandom()生成短4个字母的随机字符串,如下所示:“d2e1”

  • kh:longRandom() which generates longer 16 letter random strings like this one: "d3e1d4e1d5e1d6e1"
  • kh:longRandom()生成更长的16个字母随机字符串,如下所示:“d3e1d4e1d5e1d6e1”

#1


0  

You can use generate-id() to create unique identifiers. To quote the standard, "This function returns a string that uniquely identifies a given node."

您可以使用generate-id()创建唯一标识符。引用标准,“此函数返回唯一标识给定节点的字符串。”

Consider this stylesheet:

考虑这个样式表:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Replace <xyzzy> with <generalization xml:id="unique ID"> -->
<xsl:template match="xyzzy">
  <generalization>
    <xsl:apply-templates select="@*"/>
    <xsl:attribute name="xml:id"><xsl:value-of select="generate-id()"/></xsl:attribute>
    <xsl:apply-templates/>
  </generalization>
</xsl:template>

<!-- Copy everything else straight thru -->
<xsl:template match="node( ) | @*">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>

</xsl:stylesheet>

applied to this input:

适用于此输入:

<?xml version='1.0' encoding='ASCII'?>
<root>
 <xyzzy/>
 <xyzzy a="b">
  <xyzzy xml:id="non-unique-id"/>
 </xyzzy>
</root>

With this result:

有了这个结果:

<?xml version="1.0"?>
<root>
 <generalization xml:id="idp28972496"/>
 <generalization a="b" xml:id="idp28945920">
  <generalization xml:id="idp28946416"/>
 </generalization>
</root>

Notice how the value of generate-id() is unique across the document.

请注意generate-id()的值在整个文档中是唯一的。

#2


0  

You can use generate-id() with a temporary XML node to create random strings of any length:

您可以将generate-id()与临时XML节点一起使用来创建任意长度的随机字符串:

<!-- language: lang-xml -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:kh="https://github.com/kohsah"
    exclude-result-prefixes="xs xd"
    version="2.0">
    <xsl:function name="kh:shortRandom">
        <xsl:variable name="mxml">
            <node/>
        </xsl:variable>
        <xsl:sequence select="generate-id($mxml//node)"/>
    </xsl:function>

    <xsl:function name="kh:longRandom">
        <xsl:sequence select="concat(kh:shortRandom(), kh:shortRandom(), kh:shortRandom(), kh:shortRandom())"></xsl:sequence>
    </xsl:function>

    <xsl:template match="/">
        <test>
            <randomId><xsl:value-of select="kh:shortRandom()"/></randomId>
            <guid><xsl:value-of select="kh:longRandom()"/></guid>
        </test>
    </xsl:template>

</xsl:stylesheet>

There are two XSL functions here:

这里有两个XSL函数:

  • kh:shortRandom() which generates short 4 letter random strings like this one: "d2e1"
  • kh:shortRandom()生成短4个字母的随机字符串,如下所示:“d2e1”

  • kh:longRandom() which generates longer 16 letter random strings like this one: "d3e1d4e1d5e1d6e1"
  • kh:longRandom()生成更长的16个字母随机字符串,如下所示:“d3e1d4e1d5e1d6e1”