2011-11-14 22 views
9

उत्पन्न करता है मैं शुद्ध एक्सएसएलटी के साथ यूयूआईडी कैसे उत्पन्न कर सकता हूं? मूल रूप से एक्सएसएलटी के साथ अद्वितीय अनुक्रम बनाने के लिए एक रास्ता तलाश रहे हैं। अनुक्रम किसी भी लंबाई हो सकता है।एक्सएसएलटी यूयूआईडी

मैं एक्सएसएलटी 2.0 का उपयोग कर रहा हूं।

+0

संभव डुप्लिकेट [XSLT में GUID उत्पन्न] (http://stackoverflow.com/questions/5494175/generate-guid-in-xslt) –

उत्तर

1

के बाद से XSLT एक कार्यात्मक भाषा है, यादृच्छिक संख्या पैदा भाषा का हिस्सा नहीं है। उस ने कहा, एक्सटेंशन पैकेज (EXSLT) और कुछ प्रोसेसर (सैक्सन) हैं जो यादृच्छिक संख्याओं की पीढ़ी का समर्थन करते हैं। यदि आप एक्सटेंशन या सैक्सन का उपयोग नहीं कर सकते हैं, तो मुझे विश्वास है कि आप भाग्य से बाहर हैं।

+2

सैक्सन यादृच्छिक संख्या की पीढ़ी का समर्थन कैसे करता है? – Ayyoudy

+0

सैक्सन अंतर्निहित EXSLT यादृच्छिक मॉड्यूल के साथ आता है। Http://saxonica.com/documentation/extensions/intro.xml –

+0

देखें धन्यवाद। एक्सएसएलटी मॉड्यूल में निर्मित बहुत खराब सैक्सन हे (होम संस्करण) के लिए उपलब्ध नहीं है। – Ayyoudy

10

यहां एक good example है। मूल रूप से आप एक विस्तार है जो जावा UUID वर्ग के लिए अंक की स्थापना की, और फिर XSL में इसे संदर्भ:

<xsl:stylesheet version="2.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:uuid="java:java.util.UUID"> 
<xsl:template match="/"> 
    <xsl:variable name="uid" select="uuid:randomUUID()"/> 
    <xsl:value-of select="$uid"/> 
</xsl:template> 
+0

क्षमा करें, स्पष्टीकरण के लिए नहीं। मैंने पहले उस उदाहरण को देखा लेकिन इसे शुद्ध एक्सएसएलटी में किया जाना चाहिए। कोई जावा नहीं – Ayyoudy

+1

'xsl: value-of'' xsl: स्टाइलशीट 'का बच्चा नहीं हो सकता है ... – Abel

+0

मैंने अपरिवर्तित किया है क्योंकि यह मेरे लिए काम करता है। हालांकि, यह ध्यान देने योग्य है कि इस तरह के रिफ्लेक्सिव जावा कॉल सैक्सन हे लाइसेंस के लिए भी अनुपलब्ध हैं (हालांकि जैसा कि अन्य ने ध्यान दिया है, यह करने के लिए अपने स्वयं के कार्यों को लिखने के लिए हल की गई समस्या कम या कम है)। –

4

किसी अन्य प्रश्न पर एक नज़र डालें Generate GUID in XSLT

शायद this लेख आपको मदद मिलेगी - वहाँ परिभाषित XSLT कार्यों GUID

2

उत्पन्न करने के लिए XSLT में यादृच्छिक संख्या पैदा करने के लिए, Casting the Dice with FXSL: Random Number Generation Functions in XSLT देखते हैं। इसका उपयोग करने वाला एकमात्र एक्सटेंशन फ़ंक्शन नोड-सेट() है, जो XSLT 2.0 में अब आवश्यक नहीं है।

इसके अलावा, यदि आवश्यकता केवल आईडी है तो अद्वितीय (आवश्यक रूप से यादृच्छिक नहीं), how to generate unique string पर एक नज़र डालें। उदाहरण के लिए यदि आप इनपुट XML दस्तावेज़ के प्रत्येक तत्व के लिए यूयूआईडी उत्पन्न कर रहे हैं, तो आप प्रत्येक दस्तावेज़ के लिए एक अद्वितीय स्ट्रिंग उत्पन्न करने के लिए इनपुट दस्तावेज़ के यूआरएल के संयोजन और <xsl:number> का उपयोग कर सकते हैं।

4

आप इस (: http://code.google.com/p/public-contracts-ontology/source/browse/transformers/GB-notices/uuid.xslt?r=66e1d39a1c140079a86d219df5b3e031007cc957 स्रोत): के लिए xslt टुकड़ा का उपयोग कर सकते

.Net के XslCompiledTransform अपने XSL को बदलने के लिए उपयोग कर रहे हैं
<xsl:stylesheet xmlns:uuid="http://www.uuid.org" xmlns:math="http://exslt.org/math" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

     <xsl:template match="/"> 
       <xsl:value-of select=" 
concat('First random ID:', uuid:get-id()), 
concat('Base timestamp: ', uuid:generate-timestamp()), 
concat('Clock id: ' ,uuid:generate-clock-id()), 
concat('Network node: ' ,uuid:get-network-node()), 
concat('UUID Version: ' ,uuid:get-uuid-version()), 
concat('Generated UUID: ' ,uuid:get-uuid()), 
concat('Generated UUID: ' ,uuid:get-uuid()), 
concat('Generated UUID: ' ,uuid:get-uuid()), 
concat('Generated UUID: ' ,uuid:get-uuid()) 
" separator="&#10;"/> 
     </xsl:template> 

    <!-- 
Functions in the uuid: namespace are used to calculate a UUID 
The method used is a derived timestamp method, which is explained 
here: http://www.famkruithof.net/guid-uuid-timebased.html 
and here: http://www.ietf.org/rfc/rfc4122.txt 
--> 
    <!-- 
Returns the UUID 
--> 
    <xsl:function name="uuid:get-uuid" as="xs:string*"> 
     <xsl:variable name="ts" select="uuid:ts-to-hex(uuid:generate-timestamp())"/> 
     <xsl:value-of separator="-" select=" 
      substring($ts, 8, 8), 
      substring($ts, 4, 4), 
      string-join((uuid:get-uuid-version(), substring($ts, 1, 3)), ''), 
      uuid:generate-clock-id(), 
      uuid:get-network-node()"/> 
    </xsl:function> 
    <!-- 
internal aux. fu 
with saxon, this creates a more-unique result with 
generate-id then when just using a variable containing a node 
--> 
    <xsl:function name="uuid:_get-node"> 
     <xsl:comment/> 
    </xsl:function> 
    <!-- generates some kind of unique id --> 
    <xsl:function name="uuid:get-id" as="xs:string"> 
     <xsl:sequence select="generate-id(uuid:_get-node())"/> 
    </xsl:function> 
    <!-- 
should return the next nr in sequence, but this can't be done 
in xslt. Instead, it returns a guaranteed unique number 
--> 
    <xsl:function name="uuid:next-nr" as="xs:integer"> 
     <xsl:variable name="node"> 
      <xsl:comment/> 
     </xsl:variable> 
     <xsl:sequence select=" 
      xs:integer(replace(
      generate-id($node), '\D', ''))"/> 
    </xsl:function> 
    <!-- internal fu for returning hex digits only --> 
    <xsl:function name="uuid:_hex-only" as="xs:string"> 
     <xsl:param name="string"/> 
     <xsl:param name="count"/> 
     <xsl:sequence select=" 
      substring(replace(
      $string, '[^0-9a-fA-F]', '') 
      , 1, $count)"/> 
    </xsl:function> 
    <!-- may as well be defined as returning the same seq each time --> 
    <xsl:variable name="_clock" select="uuid:get-id()"/> 
    <xsl:function name="uuid:generate-clock-id" as="xs:string"> 
     <xsl:sequence select="uuid:_hex-only($_clock, 4)"/> 
    </xsl:function> 
    <!-- 
returns the network node, this one is 'random', but must 
be the same within calls. The least-significant bit must be '1' 
when it is not a real MAC address (in this case it is set to '1') 
--> 
    <xsl:function name="uuid:get-network-node" as="xs:string"> 
     <xsl:sequence select="uuid:_hex-only('09-17-3F-13-E4-C5', 12)"/> 
    </xsl:function> 
    <!-- returns version, for timestamp uuids, this is "1" --> 
    <xsl:function name="uuid:get-uuid-version" as="xs:string"> 
     <xsl:sequence select="'1'"/> 
    </xsl:function> 
    <!-- 
Generates a timestamp of the amount of 100 nanosecond 
intervals from 15 October 1582, in UTC time. 
--> 
    <xsl:function name="uuid:generate-timestamp"> 
     <!-- 
date calculation automatically goes 
correct when you add the timezone information, in this 
case that is UTC. 
--> 
     <xsl:variable name="duration-from-1582" as="xs:dayTimeDuration"> 
      <xsl:sequence select=" 
       current-dateTime() - 
       xs:dateTime('1582-10-15T00:00:00.000Z')"/> 
     </xsl:variable> 
     <xsl:variable name="random-offset" as="xs:integer"> 
      <xsl:sequence select="uuid:next-nr() mod 10000"/> 
     </xsl:variable> 
     <!-- do the math to get the 100 nano second intervals --> 
     <xsl:sequence select=" 
      (days-from-duration($duration-from-1582) * 24 * 60 * 60 + 
      hours-from-duration($duration-from-1582) * 60 * 60 + 
      minutes-from-duration($duration-from-1582) * 60 + 
      seconds-from-duration($duration-from-1582)) * 1000 
      * 10000 + $random-offset"/> 
    </xsl:function> 
    <!-- simple non-generalized function to convert from timestamp to hex --> 
    <xsl:function name="uuid:ts-to-hex"> 
     <xsl:param name="dec-val"/> 
     <xsl:value-of separator="" select=" 
      for $i in 1 to 15 
      return (0 to 9, tokenize('A B C D E F', ' ')) 
      [ 
      $dec-val idiv 
      xs:integer(math:power(16, 15 - $i)) 
      mod 16 + 1 
      ]"/> 
    </xsl:function> 
    <xsl:function name="math:power"> 
     <xsl:param name="base"/> 
     <xsl:param name="power"/> 
     <xsl:choose> 
      <xsl:when test="$power &lt; 0 or contains(string($power), '.')"> 
       <xsl:message terminate="yes"> 

        The XSLT template math:power doesn't support negative or 

        fractional arguments. 

       </xsl:message> 
       <xsl:text>NaN</xsl:text> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:call-template name="math:_power"> 
        <xsl:with-param name="base" select="$base"/> 
        <xsl:with-param name="power" select="$power"/> 
        <xsl:with-param name="result" select="1"/> 
       </xsl:call-template> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:function> 
    <xsl:template name="math:_power"> 
     <xsl:param name="base"/> 
     <xsl:param name="power"/> 
     <xsl:param name="result"/> 
     <xsl:choose> 
      <xsl:when test="$power = 0"> 
       <xsl:value-of select="$result"/> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:call-template name="math:_power"> 
        <xsl:with-param name="base" select="$base"/> 
        <xsl:with-param name="power" select="$power - 1"/> 
        <xsl:with-param name="result" select="$result * $base"/> 
       </xsl:call-template> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet>:choose> 
    </xsl:template> 
</xsl:stylesheet> 
+0

अपवृत ... धन्यवाद आप :) – Ayyoudy

+0

साझा करने के लिए धन्यवाद। स्रोत (सार्वजनिक अनुबंध-ऑटोलॉजी) एक मृत लिंक है :( –

+0

इस कोड के साथ 4 अलग यूयूआईडी प्राप्त करना संभव है "": 23CD00A6-8952-11E6-2114-09173F13E4C5 23CD00A5-8952-11E6-2114-09173F13E4C5 23CD00A3-8952-11E6-2114-09173F13E4C5 23CD00A4-8952-11E6-2114-09173F13E4C5 –

0

, आप, true को EnableScripts संपत्ति सेट कर सकते हैं तो नीचे के रूप में इस तरह के कोड का उपयोग :

<msxsl:script language="C#" implements-prefix="csharp"> 
    <![CDATA[ 
    public static string NewGuid() 
    { 
     return Guid.NewGuid().ToString(); 
    } 
    ]]> 
</msxsl:script> 

एनबी: मैं इस कस्टम कार्यक्षमता नाम/उपसर्ग csharp ऊपर में दिया है; लेकिन आप इसे जो कुछ भी पसंद कर सकते हैं उसे कॉल कर सकते हैं।

स्क्रिप्ट सक्षम करने के लिए, https://stackoverflow.com/a/1873265/361842 देखें। नीचे

पूर्ण XSLT फ़ाइल कुछ अतिरिक्त संदर्भ देने के लिए:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:csharp="urn:JohnLBevan/NewGuid" 
    exclude-result-prefixes="xsl msxsl csharp" 
> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="//*/text()"> 
     <!-- replaces all text nodes from input document with GUIDs --> 
     <xsl:value-of select="csharp:NewGuid()"/> 
    </xsl:template> 

    <msxsl:script language="C#" implements-prefix="csharp"> 
     <![CDATA[ 
     public static string NewGuid() 
     { 
      return Guid.NewGuid().ToString(); 
     } 
     ]]> 
    </msxsl:script> 

</xsl:stylesheet> 
की