Creating Random string containing over 25 characters of numbers and letters (upper and lower case) - xslt-1.0

How to create a Random string longer than 25 characters consisting of of digits and letters with XSLT?
Example: Khb34KXQ23ib34KDNBBE342nQE
My XSLT is like this:
<xsl:function name="kh:shortRandom">
<xsl:sequence select="generate-id()"/>
</xsl:function>
<xsl:template match="/">
<test>
<randomId><xsl:value-of select="concat(kh:shortRandom(), kh:shortRandom(), kh:shortRandom(), kh:shortRandom())"/></randomId>
</test>
</xsl:template>
But the answer is always the same..(e1d1).. Because i call the function four times.. the answer is also four time. (e1d1e1d1e1d1e1d1)
I want to have a different character every time. A little bit like password generator but just with letters and numbers.
Tnx :)

In XSLT 3.0 (XPath 3.1) one can use the random-number-generator() function.
For XSLT 2.0 I would recommend using the random number functions of FXSL - see for example: "Casting the Dice with FXSL: Random Number Generation Functions in XSLT"
Using this, here is an implementation of the wanted random-string generation function:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="my">
<xsl:import href="C:/CVS-DDN/fxsl-xslt2/f/random.xsl"/>
<xsl:output method="text"/>
<xsl:variable name="vSeed" select=
"xs:integer(seconds-from-time(current-time()))
* xs:integer(minutes-from-time(current-time()))
* xs:integer(hours-from-time(current-time()))"/>
<xsl:template match="/">
<xsl:value-of select="my:randomStrings(25, 10, $vSeed)" separator="
"/>
</xsl:template>
<xsl:function name="my:randomStrings">
<xsl:param name="pRandomLength" as="xs:integer"/>
<xsl:param name="pNumResults" as="xs:integer" />
<xsl:param name="pSeed" as="xs:integer"/>
<xsl:variable name="vAlphaNum" select="'abcdefghijklmnopqrstuvwxyz0123456789'"/>
<xsl:variable name="vNums">
<xsl:call-template name="randomSequence">
<xsl:with-param name="pSeed" select="$pSeed"/>
<xsl:with-param name="pStart" select="1"/>
<xsl:with-param name="pEnd" select="36"/>
<xsl:with-param name="pLength" select="$pRandomLength*$pNumResults"/>
</xsl:call-template>
</xsl:variable>
<xsl:sequence select=
"for $vK in 1 to $pNumResults
return
string-join(for $i in
$vNums/*[position() gt ($vK -1)*$pRandomLength
and position() le $vK*$pRandomLength]
/xs:integer(.)
return substring($vAlphaNum, $i, 1),
'')
"/>
</xsl:function>
</xsl:stylesheet>
The function my:randomStrings(pRandomLength, pNumResults, pSeed) implemented above produces a sequence of random strings and has these three arguments:
pRandomLength - the wanted length of each generated random string
pNumResults - the wanted number of random strings to be generated
pSeed - a seed for the random generator. Calling the function with different seeds will produce different results.
The code above calls the function to produce 10 random strings each with length 25. The seed is calculated from the current time and thus the result will be different each time the transformation is performed.
Here is one result:
azdkex5yi5rm3suewa7bxazpc
qi2qsg7qvl7en4cx2c5s9vfrp
l8t0lv659uba500t6e7fea518
7bt80g6bpjtjltna7ru6e3t15
t90s62fvnex5yqcq2osv97n5z
hibzw8g95wv15x2s2wv8cobem
dqiubm165tp1pci34hparuqs7
5d0chkl85liaowx3v88isk4oo
6iw5iktzaqa7jnf4g9lakqdhk
insg7iggsc22fqd1jkhbrxo53
And here is another:
bstudsgn85xq7dncy9fubu8we
g9hkl0qf493u0x7xmaz0hunqd
9lyclhrp19iz33v0hdmt7txoh
b45t1t1xfves5fjn3syzilhjq
p5bh89iojemh7adb41suew20d
goznie54278vfb4968zx3n9o8
lmouaz8j7i033mtjx1t6ymbjn
jxgqajz7g9db0g6j4o8l6ukgw
2ge6nhv69emcqanc6f63yeoro
yws75ttmbnsbyxvwwch86wbe2
Note:
As noted, you need to have downloaded the FXSL library and you will need to set the href attribute of the <xsl:import> declaration above, to point to the exact location of the file system, where the imported stylesheet file resides.

const characters = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const charactersLength = characters.length;
const generate25letterrandomstring = () => {
let randomString = '';
for (let i = 0; i < 25; i++) {
randomString += characters.charAt(Math.floor(Math.random() * charactersLength));
}
console.log(randomString);
return randomString;
}
generate25letterrandomstring()

Related

Getting the failed-assert from the sch file

I have an .sch file provided by PEPPOL website: http://docs.peppol.eu/poacc/billing/3.0/files/PEPPOL-EN16931-UBL.sch and we need to convert it to .xsl. We have done the conversion using a tool called oXygen.
This is the snipped from .sch that generates the [BR-S-06]
<rule context="cac:AllowanceCharge[cbc:ChargeIndicator=false()]/cac:TaxCategory[normalize-space(cbc:ID)='S'][cac:TaxScheme/normalize-space(upper-case(cbc:ID))='VAT']">
<assert id="BR-S-06" flag="fatal" test="(cbc:Percent) > 0">[BR-S-06]-In a Document level allowance (BG-20) where the Document level allowance VAT category code (BT-95) is "Standard rated" the Document level allowance VAT rate (BT-96) shall be greater than zero.</assert>
</rule>
This is how I am expecting a rule to show as:
<!--ASSERT -->
<xsl:choose>
<xsl:when test="#listID = 'UNCL1001'"/>
<xsl:otherwise>
<svrl:failed-assert xmlns:svrl="http://purl.oclc.org/dsdl/svrl" test="#listID = 'BR-S-06'">
<xsl:attribute name="id">BR-S-06</xsl:attribute>
<xsl:attribute name="flag">fatal</xsl:attribute>
<xsl:attribute name="location">
<xsl:apply-templates select="." mode="schematron-select-full-path"/>
</xsl:attribute>
<svrl:text>[BR-S-06]-In a Document level allowance (BG-20) where the Document level allowance VAT category code (BT-95) is "Standard rated" the Document level allowance VAT rate (BT-96) shall be greater than zero.</svrl:text>
</svrl:failed-assert>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="#*|*" mode="M7"/>
This is how it is actually shown:
<!--ASSERT -->
<xsl:choose>
<xsl:when test="(cbc:Percent) > 0"/>
<xsl:otherwise>
<xsl:message xmlns:iso="http://purl.oclc.org/dsdl/schematron">
<xsl:text>[BR-S-06]-In a Document level allowance (BG-20) where the Document level allowance VAT category code (BT-95) is "Standard rated" the Document level allowance VAT rate (BT-96) shall be greater than zero.</xsl:text>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="#*|node()" mode="M10"/>
I am expecting to see the failed-assert element because it also contains the id/flag/location rather then what i currently get which is a message.
To run the validation using Saxon we have the following code:
public static Dictionary<string, List<ValidationResult>> ValidateXML(string xslTemplate, string xslName, XmlDocument document)
{
Dictionary<string, List<ValidationResult>> resultToReturn = new Dictionary<string, List<ValidationResult>>();
XmlNamespaceManager xmlNamespacesForDocument = GetAllNamespaces(document);
var transformAssertFailed = new List<ValidationResult>();
var processor = new Processor();
var compiler = processor.NewXsltCompiler();
var executable = compiler.Compile(new MemoryStream(Encoding.UTF8.GetBytes(xslTemplate)));
var destination = new DomDestination();
MemoryStream xmlStream = new MemoryStream();
document.Save(xmlStream);
xmlStream.Position = 0;
using (xmlStream)
{
var transformer = executable.Load();
transformer.SetInputStream(xmlStream, new Uri("file:///C:/"));
transformer.Run(destination);
}
return resultToReturn;
}
I am not sure what is wrong here, maybe the .sch file that I started with or maybe the .sch to .xsl converter.
I have posted the same question on the oXygen form here and I got my question answered.

UDF/User defined function libreoffice basic with built-in help/tip

I did a function in LibreOffice calc. It works, but I would like to have a conext help/tip capability, like the built in LibreOffice functions.
Example: when I type "=besselk(" it appaears a tip BESSELK(X; N). Using the function wizard a function and arguments description does also appear.
Besselk
besselk
Let's say I have a function to calculte a rectangle area
function arearect(a, b)
arearect = a * b
end function
I would like to have something like this:
function arearect(a, b)
FUNCTION DESCRIPTION "Compute rectangle area"
ARGUMENT DESCRIPTION "base length"
ARGUMENT DESCRIPTION "height"
arearect = a * b
end function
so, when I type "=arearect(" the argument description would appear and all descriptors would appear on Function Wizard.
thanks
Create the user-defined function as a Spreadsheet Add-In. Then enter the help text in the Description node in the .xcu file that defines the add-in.
For example, I created a function called REVERSE. Here is my CalcAddIns.xcu file:
<?xml version="1.0" encoding="UTF-8"?>
<oor:component-data xmlns:oor="http://openoffice.org/2001/registry"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
oor:name="CalcAddIns" oor:package="org.openoffice.Office">
<node oor:name="AddInInfo">
<node oor:name="name.JimK.ReverseStringImpl" oor:op="replace">
<node oor:name="AddInFunctions">
<node oor:name="reverse" oor:op="replace">
<prop oor:name="DisplayName"><value xml:lang="en">reverse</value></prop>
<prop oor:name="Description">
<value xml:lang="en">Flips a string backwards. For example "apple" becomes "elppa".</value>
</prop>
<prop oor:name="Category"><value>Add-In</value></prop>
<!-- This won't help, because there is no reverse() in Excel. -->
<prop oor:name="CompatibilityName"><value xml:lang="en">reverse</value></prop>
<node oor:name="Parameters">
<node oor:name="s" oor:op="replace">
<prop oor:name="DisplayName"><value xml:lang="en">s</value></prop>
<prop oor:name="Description"><value xml:lang="en">The string to reverse.</value></prop>
</node>
</node>
</node>
</node>
</node>
</node>
</oor:component-data>
An Add-In also requires some other files. Here is my XCalcFunctions.idl:
#include <com/sun/star/uno/XInterface.idl>
module name { module JimK { module CalcFunctions {
interface XCalcFunctions
{
string reverse( [in] string s );
};
}; }; };
The actual implementation was simple. I used python:
def reverseString(inString):
s = unicode(inString)
# This is extended slice syntax [begin:end:step]. With a step of -1,
# it will traverse the string elements in descending order.
return s[::-1]
The result:
EDIT:
There is another piece in Components.py for my extension:
class StringReverserAddIn(unohelper.Base, XCalcFunctions):
def __init__(self, ctx):
self.ctx = ctx
#staticmethod
def factory(ctx):
return StringReverserAddIn(ctx)
def reverse(self, inString):
from lingt.app.calcfunctions import reverseString
return reverseString(inString)
g_ImplementationHelper.addImplementation(
StringReverserAddIn.factory,
"name.JimK.LinguisticTools.ReverseStringImpl",
("com.sun.star.sheet.AddIn",),)
The file is declared in manifest.xml:
<!--- The Python code -->
<manifest:file-entry
manifest:full-path="Components.py"
manifest:media-type="application/vnd.sun.star.uno-component;type=Python"/>
The complete extension: https://extensions.libreoffice.org/extensions/lingtools.

ATL transformation rules not matching nested BPMN2 elements

I am writing an ATL translation from BPMN2 to another model. The problem is that the code does not detect any nested element.
I have posted the atl code and my input here at: https://github.com/behnaaz/BPMN2ATL.git
You can see from the output that the only executed rule is def2mod which has created a Reo module element in the output.
If I remove the first level element in the input bpmn file then the rule mapProcess is kicked in.
Also in the logs the command BPMN20!Process.allInstances() which should give a list of all the Processes only works in the mapProcess rule.
I think there is some issue with parsing my bpmn model. Help much appreciated!
=== ATL CODE ====
-- #path BPMN20=/atttl/BPMN2/BPMN20.ecore
create OUT: reo from IN: BPMN20;
rule def2mod {
from
b: BPMN20!Definitions
to
m: reo!Module
do {
b.debug('definition to module > ' + BPMN20!Process.allInstances());
}
}
rule mapProcess {
from
proc: BPMN20!Process
to
conn: reo!Connector
do {
proc.debug('process to connector ' + proc.name + proc.flowElements);
proc.debug( BPMN20!Process.allInstances());
}
}
=== BPMN input ===
<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:Definitions xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL-XMI">
<bpmn2:Process id="bpmnid-80c796ae-c11d-42d8-92ae-1d88bab84536" name="Process" isClosed="false" processType="None" xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL-XMI">
<bpmn2:serviceTask id="bpmnid-11047880-09d8-4147-8382-523145eeb8b6" name="Task 1"/>
<bpmn2:serviceTask id="bpmnid-2f925dd9-4ec8-45b7-936c-0c14597319a9" name="Task 2"/>
<bpmn2:serviceTask id="bpmnid-21a0fc44-3c02-4a00-9b6e-aa6c058992d3" name="Task 3"/>
<bpmn2:startEvent id="bpmnid-196c656e-baa2-4306-809b-56ba006057b9" name="Start Event"/>
<bpmn2:endEvent id="bpmnid-5cfcf354-ba3f-4b13-a5bf-bdf27ca70acc" name="End Event"/>
<bpmn2:sequenceFlow id="bpmnid-be0a37d4-8054-4367-82ae-b43430d5fc6f" name="Sequence Flow0" sourceRef="bpmnid-11047880-09d8-4147-8382-523145eeb8b6" targetRef="bpmnid-2f925dd9-4ec8-45b7-936c-0c14597319a9"/>
<bpmn2:sequenceFlow id="bpmnid-01d687a3-66ee-40d7-9e17-97aa5724eef7" name="Sequence Flow" sourceRef="bpmnid-196c656e-baa2-4306-809b-56ba006057b9" targetRef="bpmnid-11047880-09d8-4147-8382-523145eeb8b6"/>
<bpmn2:sequenceFlow id="bpmnid-b687d3ec-b6d7-480a-a1e1-57fbe220e579" name="Sequence Flow2" sourceRef="bpmnid-21a0fc44-3c02-4a00-9b6e-aa6c058992d3" targetRef="bpmnid-5cfcf354-ba3f-4b13-a5bf-bdf27ca70acc"/>
<bpmn2:sequenceFlow id="bpmnid-4596a8fb-f1dc-46b3-bc28-9a2e11c26f96" name="Sequence Flow1" sourceRef="bpmnid-2f925dd9-4ec8-45b7-936c-0c14597319a9" targetRef="bpmnid-21a0fc44-3c02-4a00-9b6e-aa6c058992d3"/>
</bpmn2:Process>
</bpmn2:Definitions>
The problem seems not to lie with the ATL transformation but with the input model. It seems like it's not conform to your metamodel.
E.g. Definitions has a relation "rootElements" to Process. This should in the XMI model look like this:
<bpmn2:Definitions
xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL-XMI">
<rootElements xsi:type="bpmn2:Process" id="bpmnid-80c796ae-c11d-42d8-92ae-1d88bab84536" />
</bpmn2:Definitions>
To quickly get a conform model you can right-click the Definitions element in your ecore metamodel and choose "Create Dynamic Instance". You can then model a quick sample and run your transformation again. I quickly tried it and got following output
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:reo="http://www.cwi.nl/reo">
<reo:Module/>
<reo:Connector/>
</xmi:XMI>

Eliminating the Occurence of junk Values in an NSString?

I am getting an XML in the below mentioned Format....
<META NAME="ColdFusionMXEdition" CONTENT="ColdFusion DevNet Edition - Not for Production Use.">
<?xml version="1.0" encoding="UTF-8"?>
<ParticipantService>
<Response>
<FileName>Alzheimers</FileName>
<playlist>
<question answer="t" qno="1" tin="113" title="Aide responds" tout="114"/>
<question answer="t" qno="2" tin="123" title="Receptionist reports problem" tout="126"/>
<question answer="t" qno="3" tin="127" title="Receptionist objects to restraint" tout="130"/>
.............
I am storing the above mentioned XML in the form of an NSString. Now i just want to capture the data starting from
<?xml version="1.0".......
From here i want to Parse the String which doesnt contain the META Tag characters. Can someone please post any alternative here?
You could try something like this:
NSString *parseString = [xmlString substringWithRange:NSMakeRange([xmlString rangeOfString: #"<?xml"].location, ([xmlString length] - [xmlString rangeOfString: #"<?xml"].location))];
NSString has plenty of methods to work with. Here's my solution which is similar to Peter's:
NSString *newString = [xmlString substringFromIndex:[xmlString rangeOfString:#"<?xml"].location];
or if you already have an NSMutableString:
[mutableXMLString deleteCharactersInRange:NSMakeRange(0, [mutableXMLString rangeOfString:#"<?xml"].location)];
Of course, neither solution tackles cases where there isn't any <?xml.

NSXMLParser foundCharacters returning "\n " characters

I have an xml parser and the foundCharacters call back method is returning strings like this:
"\n "
"\n "
Here the part of the XML that I am parsing and I don't see this string anywhere in it:
<GetChannelMessages xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
<element p2:type="ToonInDevModel.GetChannelMessagesTest_Result" xmlns:p2="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<messageid p2:type="Edm.Int32">15030</messageid>
<userid p2:type="Edm.Int32">1</userid>
<Handle xml:space="preserve">Henry </Handle>
<Distance p2:type="Edm.Double">16.845658889067568</Distance>
<MessageDateTime p2:type="Edm.DateTime">2011-07-18T03:14:43.17</MessageDateTime>
<Message>Posting this message again</Message>
</element>
<element p2:type="ToonInDevModel.GetChannelMessagesTest_Result" xmlns:p2="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<messageid p2:type="Edm.Int32">15021</messageid>
<userid p2:type="Edm.Int32">1</userid>
<Handle xml:space="preserve">Henry </Handle>
<Distance p2:type="Edm.Double">16.845658889067568</Distance>
<MessageDateTime p2:type="Edm.DateTime">2011-07-18T01:45:17.097</MessageDateTime>
<Message>Posting this message again</Message>
</element>
<element p2:type="ToonInDevModel.GetChannelMessagesTest_Result" xmlns:p2="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<messageid p2:type="Edm.Int32">15018</messageid>
<userid p2:type="Edm.Int32">1</userid>
<Handle xml:space="preserve">Henry </Handle>
<Distance p2:type="Edm.Double">16.845658889067568</Distance>
<MessageDateTime p2:type="Edm.DateTime">2011-07-18T01:33:18.1</MessageDateTime>
<Message>Posting this message again</Message>
</element>
Is there something wrong with this XML?
This is normal.
"\n" is a linebreak character. The XML parser will tell you about all characters it finds, even the spaces and lines between the elements themselves.