2017-07-19 48 views
0

以下是我正在使用的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> 
    <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" : "", 
    "ProductCode" : "", 
    "AccountNumber" : "", 
    "TransactionNumber" : "", 
    "FromDate" : "", 
    "ToDate" : "", 
    "FromAmount" : "", 
    "ToAmount" : "" 
} 

在輸出中,我想通過在ReferenceNumber標籤唯一的參考號。相同的格式將是UNIQU000000DDMMYYYYHHMMSS MM - 月 YYYY - 年 HH - 小時 MM - 分 SS - 秒。 只是想知道,在XSLT中是否有任何選項可以生成這樣的唯一參考號。我有一個想法來生成唯一的引用,像這是C#但不是在XSLT中。

+0

我不明白你的問題。首先,XSLT 1.0(您似乎正在使用)不知道當前的日期或時間,除非您的處理器可以從它支持的某個擴展功能中獲得它。接下來,如果您的「唯一參考號碼」是基於當前的日期時間,那麼包括兩者在內的含義是什麼?如果你打算生成一個**隨機**數字來添加日期和時間,那麼答案是否定的,XSLT 1.0不能這樣做。 –

回答

0

使用以下XSLT獲取預期輸出。

<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" : ""} 
0

XSLT是完全確定性的,它不能由自身產生的隨機數。您的最佳方案是要麼寫的擴展功能,做你需要什麼,或者使用

<xsl:param name="datetime"/> 

<xsl:stylesheet>元素的子元素中日期或者你通過一個參數需要隨機值傳遞。

相關問題