Skip to content

Instantly share code, notes, and snippets.

@kelchm
Created January 26, 2017 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kelchm/1f35ec21b4191084b71fc16e48fc08bb to your computer and use it in GitHub Desktop.
Save kelchm/1f35ec21b4191084b71fc16e48fc08bb to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format "
xmlns:urldecoder="xalan://java.net.URLDecoder"
exclude-result-prefixes="urldecoder">
<xsl:output method="text" media-type="application/json" encoding="ISO-8859-1"/>
<!-- Load the column titles and fieldnames into a variable for later use. -->
<xsl:variable name="title" select="/fxf/report/table/thead/tr/td[@class='title']"/>
<xsl:variable name="fieldname" select="/fxf/report/field_desc/field/@fieldname"/>
<xsl:variable name="totalrows" select="count(/fxf/report/table/tbody/tr)"/>
<xsl:variable name="heading" select="/fxf/report/table/thead/tr[@linenum='0']/td[@class='heading']"/>
<xsl:variable name="subheading" select="/fxf/report/table/thead/tr[@linenum='1']/td[@class='heading']"/>
<xsl:variable name="xtitle" select="/fxf/report/table/thead/tr[@linenum='2']/td[@class='heading']"/>
<xsl:variable name="ytitle" select="/fxf/report/table/thead/tr[@linenum='3']/td[@class='heading']"/>
<!-- main template -->
<xsl:template match="/fxf/report">
{
"chart": {
"type": "bar"
},
"title": {
"text":
<xsl:choose>
<xsl:when test="$heading!=''">
"<xsl:value-of select="$heading" />"
</xsl:when>
<xsl:otherwise>
null
</xsl:otherwise>
</xsl:choose>
},
"subtitle": {
"text":
<xsl:choose>
<xsl:when test="$subheading!=''">
"<xsl:value-of select="$subheading" />"
</xsl:when>
<xsl:otherwise>
null
</xsl:otherwise>
</xsl:choose>
},
"xAxis": {
"categories": [
<xsl:apply-templates select="field_desc/field"/>
],
"title": {
"text":
<xsl:choose>
<xsl:when test="$xtitle!=''">
"<xsl:value-of select="$xtitle" />"
</xsl:when>
<xsl:otherwise>
null
</xsl:otherwise>
</xsl:choose>
}
},
"yAxis": {
"title": {
"text":
<xsl:choose>
<xsl:when test="$ytitle!=''">
"<xsl:value-of select="$ytitle" />"
</xsl:when>
<xsl:otherwise>
null
</xsl:otherwise>
</xsl:choose>
}
},
"series": [
<xsl:apply-templates select="table/tbody/tr"/>
]
}
</xsl:template>
<!-- title builder - use the field to loop, but he title variable when generating the value -->
<xsl:template match="field_desc/field">
<xsl:variable name="titlecount" select="position()"/>
<xsl:choose>
<xsl:when test="position()=1"/>
<xsl:when test="position()=last()">
"<xsl:value-of select="$title[$titlecount]"/>"
</xsl:when>
<xsl:otherwise>
"<xsl:value-of select="$title[$titlecount]"/>",
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- data builder w/ dynamic element names to match the fieldnames. -->
<xsl:template match="table/tbody/tr">
{
<xsl:for-each select="td">
<xsl:choose>
<xsl:when test="position()=1">
"name": "<xsl:value-of select="text()"/>",
</xsl:when>
<xsl:when test="position()=2">
"data": [ <xsl:value-of select="text()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="text()"/>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="position()=1" />
<xsl:when test="position() > 1 and position()!=last()">
,
</xsl:when>
<xsl:when test="position()=last()">
]
</xsl:when>
</xsl:choose>
</xsl:for-each>
}
<xsl:choose>
<xsl:when test="position() != $totalrows">,</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment