Skip to content

Instantly share code, notes, and snippets.

@vdcrea
Last active August 29, 2015 14:25
Show Gist options
  • Save vdcrea/26ba64b9167d262df2b4 to your computer and use it in GitHub Desktop.
Save vdcrea/26ba64b9167d262df2b4 to your computer and use it in GitHub Desktop.
xsl file to output json used for unsa-industrie.org
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:math="http://exslt.org/math"
extension-element-prefixes="math">
<xsl:output method="xml"
omit-xml-declaration="yes"
encoding="UTF-8"
indent="yes" />
<xsl:template match="/"><xsl:choose><xsl:when test="$request='tree' and $filtertype=''"><xsl:for-each select="data/xml/tree">{"name": "<xsl:value-of select="@name"/>",
"children": [<xsl:for-each select="secteur">
<xsl:call-template name="secteur"/>
</xsl:for-each>
]
}
</xsl:for-each></xsl:when><xsl:when test="$request='tree' and $filtertype='branche'"><xsl:for-each select="data/xml/tree/branche">{"name": "<xsl:value-of select="@name"/>",
"children": [<xsl:for-each select="syndicat">
<xsl:call-template name="syndicat"/>
</xsl:for-each>
]
}
</xsl:for-each></xsl:when><xsl:when test="$request='sections'">[
<xsl:for-each select="data/xml/sections/entreprise">
<xsl:choose><xsl:when test="count(section-syndicale)=1">{"value":"<xsl:value-of select="section-syndicale/@name"/>","text":"<xsl:value-of select="@name"/> (<xsl:value-of select="section-syndicale/@syndicat"/>)"}</xsl:when><xsl:otherwise><xsl:for-each select="section-syndicale">{"value":"<xsl:value-of select="@name"/>","text":"<xsl:value-of select="@name"/> (<xsl:value-of select="@syndicat"/>)"}<xsl:if test="position()!=last()">,
</xsl:if></xsl:for-each></xsl:otherwise></xsl:choose><xsl:if test="position()!=last()">,
</xsl:if></xsl:for-each>
]</xsl:when><xsl:when test="$request='syndicats'">[
<xsl:for-each select="data/xml/syndicats/syndicat">{"value":"<xsl:value-of select="@name"/>","text":"<xsl:value-of select="@name"/>"}<xsl:if test="position()!=last()">,
</xsl:if></xsl:for-each>
]</xsl:when><xsl:otherwise>Please specify your request:
<xsl:value-of select="$root"/>/api/request
ex: <xsl:value-of select="$root"/>/api/tree
Available requests:
tree
Available filter:
/branche
sections
Available filters:
/branche
/syndicat
syndicats
Available filter:
/branche
Filter value: slug
ex: <xsl:value-of select="$root"/>/api/tree/branche/metallurgie
ex: <xsl:value-of select="$root"/>/api/sections/branche/chimie
ex: <xsl:value-of select="$root"/>/api/sections/syndicat/unsa-chimie
</xsl:otherwise></xsl:choose></xsl:template>
<xsl:template name="secteur">
{"name":"<xsl:value-of select="@name"/>",<xsl:choose>
<xsl:when test="count(branche)!=1">
"children": [<xsl:for-each select="branche">
<xsl:call-template name="branche"/>
</xsl:for-each>
]
</xsl:when>
<xsl:when test="count(branche/syndicat)!=1">
"children": [<xsl:for-each select="branche/syndicat">
<xsl:call-template name="syndicat"/>
</xsl:for-each>
]
</xsl:when>
<xsl:when test="count(branche/syndicat/section)!=1">
"children": [<xsl:for-each select="branche/syndicat/section">
<xsl:call-template name="section"/>
</xsl:for-each>
]
</xsl:when>
<xsl:otherwise>"size":<xsl:value-of select="90 + (floor(math:random()*5) mod 5) + 1"/>, "url":"<xsl:value-of select="$root"/>/syndicat/<xsl:value-of select="branche/syndicat/section/@path"/>"</xsl:otherwise></xsl:choose>}<xsl:if test="position()!=last()">,</xsl:if>
</xsl:template>
<xsl:template name="branche">
{"name":"<xsl:value-of select="@name"/>",<xsl:choose>
<xsl:when test="count(syndicat)!=1">
"children": [<xsl:for-each select="syndicat">
<xsl:call-template name="syndicat"/>
</xsl:for-each>
]
</xsl:when>
<xsl:when test="count(syndicat/section)!=1">
"children": [<xsl:for-each select="syndicat/section">
<xsl:call-template name="section"/>
</xsl:for-each>
]
</xsl:when>
<xsl:otherwise>"size":<xsl:value-of select="30 + (floor(math:random()*5) mod 5) + 1"/>, "url":"<xsl:value-of select="$root"/>/syndicat/<xsl:value-of select="syndicat/section/@path"/>"</xsl:otherwise></xsl:choose>}<xsl:if test="position()!=last()">,</xsl:if>
</xsl:template>
<xsl:template name="syndicat">
{"name":"<xsl:value-of select="@name"/>",<xsl:choose>
<xsl:when test="count(section)!=1">
"children": [<xsl:for-each select="section">
<xsl:call-template name="section"/>
</xsl:for-each>
]
</xsl:when>
<xsl:otherwise>"size":<xsl:value-of select="10 + (floor(math:random()*5) mod 5) + 1"/>, "url":"<xsl:value-of select="$root"/>/syndicat/<xsl:value-of select="section/@path"/>"</xsl:otherwise></xsl:choose>}<xsl:if test="position()!=last()">,</xsl:if>
</xsl:template>
<xsl:template name="section">
{"name":"<xsl:value-of select="@name"/>","size":<xsl:value-of select="(floor(math:random()*5) mod 5) + 1"/>, "url":"<xsl:value-of select="$root"/>/syndicat/<xsl:value-of select="@path"/>"}<xsl:if test="position()!=last()">,</xsl:if>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment