Skip to content

Instantly share code, notes, and snippets.

@tf0054
Last active December 14, 2015 22:39
Show Gist options
  • Select an option

  • Save tf0054/5159785 to your computer and use it in GitHub Desktop.

Select an option

Save tf0054/5159785 to your computer and use it in GitHub Desktop.
xslt v2.0 file for Solr (producing json data)
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!--
* Start solr with saxon(I am using saxon9-1-0-8j).
* these jars should be stored lib/ext/ (seen from the dir containing start.jar).
java -Djavax.xml.transform.TransformerFactory=net.sf.saxon.TransformerFactoryImpl -jar start.jar
-->
<!--
* posted docs are like this.
https://gist.github.com/tf0054/4cf80965cf88852b658d
-->
<xsl:output media-type="application/json" encoding="UTF-8" method="text"/>
<xsl:template match="/">
<xsl:text>[</xsl:text>
<xsl:apply-templates select="response/lst[@name='facet_counts']/lst[@name='facet_pivot']/arr/lst" mode="active"/>
<xsl:apply-templates select="(response/lst[@name='facet_counts']/lst[@name='facet_pivot']/arr/lst)[last()]" mode="inactive"/>
<xsl:text>]</xsl:text>
</xsl:template>
<xsl:template match="lst" mode="active">
<xsl:text>"key":"</xsl:text>
<xsl:value-of select="str[@name='value']"/>
<xsl:text>","values":[</xsl:text>
<xsl:apply-templates select="arr[@name='pivot']/lst" mode="active"/>
<xsl:apply-templates select="(arr[@name='pivot']/lst)[last()]" mode="inactive"/>
<xsl:text>],</xsl:text>
</xsl:template>
<xsl:template match="lst" mode="inactive">
<xsl:text>"key":"</xsl:text>
<xsl:value-of select="str[@name='value']"/>
<xsl:text>","values":[</xsl:text>
<xsl:apply-templates select="arr[@name='pivot']/lst" mode="active"/>
<xsl:apply-templates select="(arr[@name='pivot']/lst)[last()]" mode="inactive"/>
<xsl:text>]</xsl:text>
</xsl:template>
<xsl:template match="arr[@name='pivot']/lst" mode="active">
<xsl:text>[</xsl:text>
<xsl:variable name="j" as="xs:dateTime" select="date" />
<xsl:value-of select="( $j - xs:dateTime('1970-01-01T00:00:00') ) div xs:dayTimeDuration('PT1S')"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="int[@name='count']"/>
<xsl:text>]</xsl:text>
<xsl:text>,</xsl:text>
</xsl:template>
<xsl:template match="arr[@name='pivot']/lst" mode="inactive">
<xsl:text>[</xsl:text>
<xsl:variable name="j" as="xs:dateTime" select="date" />
<xsl:value-of select="( $j - xs:dateTime('1970-01-01T00:00:00') ) div xs:dayTimeDuration('PT1S')"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="int[@name='count']"/>
<xsl:text>]</xsl:text>
</xsl:template>
<xsl:template match="*"/>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment