Project

Profile

Help

Bug #3257 ยป random-spec.xsl

Michael Kay, 2017-06-11 00:46

 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="xs"
version="3.0">
<xsl:template match="/">
<xsl:variable name="words"
as="xs:string*"
select="tokenize(., '\s+')[matches(., '[a-zA-Z]*')]"/>
<xsl:variable name="histogram" as="map(*)">
<xsl:iterate select="tail($words)">
<xsl:param name="previous" select="head($words)"/>
<xsl:param name="histogram" select="map{}"/>
<xsl:on-completion select="$histogram"/>
<xsl:variable name="new-entry" select="if (map:contains($histogram, '.')) then map:get($histogram, .) else ()"/>
<xsl:next-iteration>
<xsl:with-param name="previous" select="."/>
<xsl:with-param name="histogram" select="map:put($histogram, $previous, $new-entry)"/>
</xsl:next-iteration>
</xsl:iterate>
</xsl:variable>
<xsl:iterate select="1 to 200">
<xsl:param name="word" select="'This'"/>
<xsl:value-of select="' '"/>
<xsl:variable name="next" select="(random-number-generator(.)?permute(map:get($histogram,$word)))[1]"/>
<xsl:value-of select="$next"/>
<xsl:next-iteration>
<xsl:with-param name="word" select="$next"/>
</xsl:next-iteration>
</xsl:iterate>
</xsl:template>
</xsl:stylesheet>
    (1-1/1)