As good things are good to replicate, I would like to send my regards and
- <?xml version="1.0" encoding="UTF-8"?>
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:output method="text" />
- <xsl:template match="/">
- <xsl:call-template name="getValue">
- <xsl:with-param name="hashtable" select="'key:a,1,2,3,4,key:b,5,6,7,key:c,8,9'" />
- <xsl:with-param name="key" select="'b'" />
- <xsl:with-param name="delim" select="','" />
- </xsl:call-template>
- <xsl:text>--------------------------
- </xsl:text>
- <xsl:call-template name="getValue">
- <xsl:with-param name="hashtable" select="'key:1,1,2,3,4,key:2,5,6,7,key:3,8,9'" />
- <xsl:with-param name="key" select="'3'" />
- <xsl:with-param name="delim" select="','" />
- </xsl:call-template>
- <xsl:text>--------------------------
- </xsl:text>
- <xsl:call-template name="getValue">
- <xsl:with-param name="hashtable" select="'key:1,a,key:2,b,key:3,c'" />
- <xsl:with-param name="key" select="'3'" />
- <xsl:with-param name="delim" select="','" />
- </xsl:call-template>
- <xsl:text>--------------------------
- </xsl:text>
- <xsl:call-template name="enumerateKeys">
- <xsl:with-param name="hashtable" select="'key:1,1,2,3,4,key:2,5,6,7,key:3,8,9'" />
- <xsl:with-param name="delim" select="','" />
- </xsl:call-template>
- <xsl:text>--------------------------
- </xsl:text>
- <xsl:call-template name="enumerateKeys">
- <xsl:with-param name="hashtable" select="'key:1,qwert'" />
- <xsl:with-param name="delim" select="','" />
- </xsl:call-template>
- <xsl:text>--------------------------
- </xsl:text>
- <xsl:call-template name="enumerateKeyValuePairs">
- <xsl:with-param name="hashtable" select="'key:1,qwert'" />
- <xsl:with-param name="delim" select="','" />
- </xsl:call-template>
- <xsl:text>--------------------------
- </xsl:text>
- <xsl:call-template name="enumerateKeyValuePairs">
- <xsl:with-param name="hashtable" select="'key:fruits,apple,guava,key:flowers,rose,sunflower'" />
- <xsl:with-param name="delim" select="','" />
- </xsl:call-template>
- <xsl:text>--------------------------
- </xsl:text>
- </xsl:template>
- <xsl:template name="getValue">
- <xsl:param name="hashtable" />
- <xsl:param name="key" />
- <xsl:param name="delim" />
- <xsl:if test="contains(substring-after($hashtable, concat('key:', $key, $delim)), 'key:')">
- <xsl:value-of select="substring-before(substring-after($hashtable, concat('key:', $key, $delim)), concat($delim, 'key:'))" /><xsl:text>
- </xsl:text>
- </xsl:if>
- <xsl:if test="not(contains(substring-after($hashtable, concat('key:', $key, $delim)), 'key:'))">
- <xsl:value-of select="substring-after($hashtable, concat('key:', $key, $delim))" /><xsl:text>
- </xsl:text>
- </xsl:if>
- </xsl:template>
- <xsl:template name="enumerateKeys">
- <xsl:param name="hashtable" />
- <xsl:param name="delim" />
- <xsl:if test="contains($hashtable, $delim)">
- <xsl:if test="starts-with(substring-before($hashtable, $delim), 'key:')">
- <xsl:value-of select="substring-after(substring-before($hashtable, $delim), 'key:')" /><xsl:text>
- </xsl:text>
- </xsl:if>
- <xsl:call-template name="enumerateKeys">
- <xsl:with-param name="hashtable" select="substring-after($hashtable, $delim)" />
- <xsl:with-param name="delim" select="$delim" />
- </xsl:call-template>
- </xsl:if>
- <xsl:if test="not(contains($hashtable, $delim))">
- <xsl:if test="starts-with($hashtable, 'key:')">
- <xsl:value-of select="substring-after($hashtable, 'key:')" /><xsl:text>
- </xsl:text>
- </xsl:if>
- </xsl:if>
- </xsl:template>
- <xsl:template name="enumerateKeyValuePairs">
- <xsl:param name="hashtable" />
- <xsl:param name="delim" />
- <xsl:if test="contains($hashtable, 'key:')">
- <xsl:if test="starts-with(substring-before($hashtable, $delim), 'key:')">
- Key : <xsl:value-of select="substring-after(substring-before($hashtable, $delim), 'key:')" />
- <xsl:variable name="val">
- <xsl:call-template name="getValue">
- <xsl:with-param name="hashtable" select="$hashtable" />
- <xsl:with-param name="key" select="substring-after(substring-before($hashtable, $delim), 'key:')" />
- <xsl:with-param name="delim" select="$delim" />
- </xsl:call-template>
- </xsl:variable>
- Value: <xsl:value-of select="$val" />
- <xsl:text>
- </xsl:text>
- <xsl:call-template name="enumerateKeyValuePairs">
- <xsl:with-param name="hashtable" select="substring-after($hashtable, normalize-space(concat(substring-before($hashtable, $delim), $delim, substring($val, 1, string-length($val) - 1), $delim)))" />
- <xsl:with-param name="delim" select="$delim" />
- </xsl:call-template>
- </xsl:if>
- </xsl:if>
- </xsl:template>
- </xsl:stylesheet>
Want to dive into more of such advanced things done with xslt? Go ahead!
No comments:
Post a Comment