以下是我正在使用的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>
</xsl:text>
</xsl:variable>
<xsl:variable name="tb">
<xsl:text>	</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 :(得分: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" : ""}
答案 1 :(得分:0)
XSLT是完全确定的,它不能自己产生随机数。您最好的计划是编写一个可以满足您需要的扩展功能,或使用
通过参数传递您需要的日期或随机值<xsl:param name="datetime"/>
作为<xsl:stylesheet>
元素的子元素。