Xml 基于当前日期时间在XSLT中创建唯一编号

Xml 基于当前日期时间在XSLT中创建唯一编号,xml,xslt,Xml,Xslt,下面是我正在使用的XSLT <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output method="text" /> <xsl:variable name="nl"> <xsl:text>&#xa;</xsl:text> </xsl:variable&g

下面是我正在使用的XSLT

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="text" />
    <xsl:variable name="nl">
        <xsl:text>&#xa;</xsl:text>
    </xsl:variable>
    <xsl:variable name="tb">
        <xsl:text>&#x9;</xsl:text>
    </xsl:variable>
    <xsl:template match="/*">
        <!-- Open the root array -->
        <!--<xsl:text>[{</xsl:text>-->
    <xsl:text>{</xsl:text>
        <xsl:value-of select="$nl" />
        <!-- Process all the child nodes of the root -->
        <xsl:apply-templates select="*" mode="subitem" >
            <xsl:with-param name="indent" select="$tb" />
        </xsl:apply-templates>
        <!-- Close the root array -->
        <xsl:value-of select="$nl" />
        <!--<xsl:text>}]</xsl:text>-->
  <xsl:text>}</xsl:text>
    </xsl:template>
    <xsl:template match="*" mode="subitem" >
        <!-- child element at any level. The indent parameter allows for better layout of the output JSON -->
        <xsl:param name="indent"/>
        <!-- newindent is used as the indent for children of this node -->
        <xsl:variable name="newindent">
            <xsl:value-of select="$indent" />
            <xsl:value-of select="$tb"/>
        </xsl:variable>
        <!-- output the name of this node in quotes, ready for the content to follow -->
        <xsl:value-of select="$indent" />
        <xsl:text>"</xsl:text>
        <xsl:value-of select="name()" />
        <xsl:text>" :</xsl:text>
        <!-- check if this node has children, if not, simply output the text value, otherwise outoput an array -->
        <xsl:choose>
            <xsl:when test=" count( ./* ) = 0 ">
                <!-- This is a text value only -->
                <xsl:text> "</xsl:text>
                <!-- Make sure that any embedded quotes are escaped -->
                <xsl:call-template name="string-replace-all">
                    <xsl:with-param name="text" select="." />
                    <xsl:with-param name="replace">"</xsl:with-param>
                    <xsl:with-param name="by">\"</xsl:with-param>
                </xsl:call-template>
                <xsl:text>"</xsl:text>
                <!-- check if we need a comma and a new line, not required if this is the last output value -->
                <xsl:if test=" position() != last() ">
                    <xsl:text>,</xsl:text>
                    <xsl:value-of select="$nl" />
                </xsl:if>
            </xsl:when>
            <xsl:otherwise>
                <!-- This node has children, so we need to process them as an array -->
                <!-- Array opening -->
                <xsl:text>[</xsl:text>
                <xsl:value-of select="$nl" />
                <xsl:value-of select="$newindent" />
                <xsl:text>{</xsl:text>
                <xsl:value-of select="$nl" />
                <!-- Process all the elements in the array (recursive call to this template) -->
                <xsl:apply-templates select="*" mode="subitem" >
                    <xsl:with-param name="indent">
                        <xsl:value-of select="$newindent" />
                    </xsl:with-param>
                </xsl:apply-templates>
                <!-- Close the array -->
                <xsl:value-of select="$nl" />
                <xsl:value-of select="$newindent" />
                <xsl:text>}</xsl:text>
                <xsl:value-of select="$nl" />
                <xsl:value-of select="$indent" />
                <xsl:text>]</xsl:text>
                <!-- If this is not the last node then we need a comma and line feed -->
                <xsl:if test=" position() != last() ">
                    <xsl:text>,</xsl:text>
                    <xsl:value-of select="$nl" />
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="string-replace-all">
        <!-- This code provided thanks to @codesling on stackoverflow -->
        <xsl:param name="text" />
        <xsl:param name="replace" />
        <xsl:param name="by" />
        <xsl:choose>
            <xsl:when test="contains($text, $replace)">
                <xsl:value-of select="substring-before($text,$replace)" />
                <xsl:value-of select="$by" />
                <xsl:call-template name="string-replace-all">
                    <xsl:with-param name="text"
                        select="substring-after($text,$replace)" />
                    <xsl:with-param name="replace" select="$replace" />
                    <xsl:with-param name="by" select="$by" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
在输出中,我希望在ReferenceNumber标记中传递唯一的引用号。相同的格式将为UniqU000000DDMMYYYYHMMSS MM-月 YYYY年 小时 嗯-分钟 SS-秒。
我只是想知道,XSLT中是否有任何选项可以生成这样的唯一引用号。我有一个想法,就是生成唯一的引用,比如C#,但不是在XSLT中。

使用下面的XSLT获得预期的输出


XSLT是完全确定的,它本身不能生成随机数。最好的方法是编写一个扩展函数来完成所需的操作,或者使用

<xsl:param name="datetime"/>


作为
元素的孩子。

我不理解你的问题。首先,XSLT1.0(您似乎正在使用)不知道当前的日期或时间,除非您的处理器可以从它支持的一些扩展函数获得它。接下来,如果您的“唯一参考号”是基于当前日期和时间,那么将两者都包含有什么意义?如果您想生成一个随机数来添加到日期和时间中,那么答案也是否定的,XSLT1.0无法做到这一点。
<msxsl:script language="C#" implements-prefix="user">
    <msxsl:assembly name="System.Collections"/>
    <msxsl:using namespace="System.Collections.Generic" />
    <msxsl:assembly name="System.Data"/>
    <msxsl:using namespace="System.IO" />
    <msxsl:using namespace="System.Xml"/>
    <msxsl:using namespace="System.Security.Cryptography" />
    <msxsl:using namespace="System.Web"/>
    <msxsl:assembly name="System.Web" />
    <msxsl:using namespace="System.Text" />

    <![CDATA[
 public string GetJson(string ReferenceNumber = null,
        string ProductCode = null,
        string AccountNumber = null,
        string TransactionNumber = null,
        string FromDate = null,
        string ToDate = null,
        string FromAmount = null,
        string ToAmount = null)
    {

        string currentDateTime = string.Format("UNIQU000000{0}{1}{2}{3}{4}{5}", DateTime.Now.Day.ToString("00"), DateTime.Now.Month.ToString("00"), DateTime.Now.Year.ToString("0000"), DateTime.Now.Hour.ToString("00"), DateTime.Now.Minute.ToString("00"), DateTime.Now.Second.ToString("00"));
        string jsonOutput = string.Format("{{\"ReferenceNumber\" :\"{0}\",\"ProductCode\" : \"{1}\",\"AccountNumber\" : \"{2}\",\"TransactionNumber\" : \"{3}\",\"FromDate\" : \"{4}\",\"ToDate\" : \"{5}\",\"FromAmount\" : \"{6}\",\"ToAmount\" : \"{7}\"}}",
            currentDateTime, ProductCode, AccountNumber, TransactionNumber, FromDate, ToDate, FromAmount, ToAmount);

        return jsonOutput;
    }
]]>
</msxsl:script>
<xsl:output method="text"/>

<xsl:template match="/Request">
    <xsl:variable name="referenceNumber" select="ReferenceNumber" />
    <xsl:variable name="productCode" select="ProductCode" />
    <xsl:variable name="accountNumber" select="AccountNumber" />
    <xsl:variable name="transactionNumber" select="TransactionNumber" />

    <xsl:variable name="fromDate" select="FromDate" />
    <xsl:variable name="toDate" select="ToDate" />
    <xsl:variable name="fromAmount" select="FromAmount" />
    <xsl:variable name="toAmount" select="ToAmount" />
    <xsl:variable name="jsonRequest" select="user:GetJson($referenceNumber,$productCode,$accountNumber,$transactionNumber,$fromDate,$toDate,$fromAmount,$toAmount)" />
    <xsl:value-of select="$jsonRequest"/>
</xsl:template>
{"ReferenceNumber" :"UNIQU00000019072017162234","ProductCode" : "","AccountNumber" : "","TransactionNumber" : "","FromDate" : "","ToDate" : "","FromAmount" : "","ToAmount" : ""}
<xsl:param name="datetime"/>