Skip to content

Instantly share code, notes, and snippets.

@ieb ieb/xsd2hbm.xsl
Created Aug 1, 2011

Embed
What would you like to do?
Transform to generate HBM files from a XSD
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xpdl="http://www.wfmc.org/2002/XPDL1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsd2hbm="http://www.tfd.co.uk/2006/xsd2hbm.xsd"
exclude-result-prefixes="xpdl xsd2hbm xsd xsl">
<xsl:output doctype-public="-//Hibernate/Hibernate Mapping DTD 3.0//EN"
doctype-system="http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" />
<xsl:param name="tableprefix">wf</xsl:param>
<xsl:param name="package">uk.co.tfd.wf.api.model.design</xsl:param>
<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<xsl:template match="/" >
<hibernate-mapping package="{$package}">
<xsl:apply-templates/>
</hibernate-mapping>
</xsl:template>
<xsl:template match="xsd:element|xsd:group">
<xsl:variable name="elname" select="@name"/>
<xsl:variable name="classname">
<xsl:call-template name="upperfirst">
<xsl:with-param name="name" select="$elname"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="lcelname" select="translate($elname,$ucletters,$lcletters)"/>
<xsl:choose>
<xsl:when test="@xsd2hbm:action='ignore'">
<xsl:comment> Ignored Element <xsl:value-of select="@name"/>
</xsl:comment>
</xsl:when>
<xsl:when test="@xsd2hbm:action='list'">
<xsl:comment> Ignored List Container <xsl:value-of select="@name"/>
</xsl:comment>
</xsl:when>
<xsl:when test="@xsd2hbm:action='set'">
<xsl:comment> Ignored Bag Container <xsl:value-of select="@name"/>
</xsl:comment>
</xsl:when>
<xsl:otherwise>
<class name="{$package}.{$classname}Model"
table="{$tableprefix}_{$lcelname}">
<id name="id" unsaved-value="null" type="string">
<column name="id" length="36" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<xsl:copy-of select="xsd:annotation/xsd2hbm:markup"/>
<xsl:apply-templates mode="contained"/>
</class>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="xsd:element" mode="contained">
<xsl:choose>
<xsl:when test="@ref">
<xsl:variable name="elname" select="substring-after(@ref,'xpdl:')"/>
<xsl:variable name="xlcelname">
<xsl:call-template name="lowerfirst">
<xsl:with-param name="name" select="$elname"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="lcelname">
<xsl:choose>
<xsl:when test="$xlcelname='id'">wfid</xsl:when>
<xsl:otherwise><xsl:value-of select="$xlcelname"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="classname">
<xsl:call-template name="upperfirst">
<xsl:with-param name="name" select="$elname"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="parentname" select="ancestor::xsd:element/@name" />
<xsl:variable name="lparentname">
<xsl:call-template name="lowerfirst">
<xsl:with-param name="name" select="$parentname"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="ref" select="substring-after(@ref,'xpdl:')" />
<xsl:variable name="type" select="/xsd:schema/xsd:element[@name=$ref]/@xsd2hbm:action" />
<xsl:variable name="targetname" select="/xsd:schema/xsd:element[@name=$ref]/@name" />
<xsl:variable name="targetlength" >
<xsl:choose>
<xsl:when test="/xsd:schema/xsd:element[@name=$ref]/@xsd2hbm:length"><xsl:value-of select="/xsd:schema/xsd:element[@name=$ref]/@xsd2hbm:length"/></xsl:when>
<xsl:otherwise>64</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="targettype" >
<xsl:choose>
<xsl:when test="/xsd:schema/xsd:element[@name=$ref]/@xsd2hbm:type"><xsl:value-of select="/xsd:schema/xsd:element[@name=$ref]/@xsd2hbm:type"/></xsl:when>
<xsl:otherwise>string</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="targetclass" >
<xsl:choose>
<xsl:when test="/xsd:schema/xsd:element[@name=$ref]/@xsd2hbm:targetclass"><xsl:value-of select="/xsd:schema/xsd:element[@name=$ref]/@xsd2hbm:targetclass"/></xsl:when>
<xsl:otherwise><xsl:value-of select="$classname"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="$type='set'">
<set
name="{$lparentname}{$lcelname}"
cascade="save-update"
>
<key column="{$lparentname}_id" not-null="true" />
<one-to-many class="{$package}.{$targetclass}Model" />
</set>
</xsl:when>
<xsl:when test="$type='list'">
<list
name="{$lcelname}"
cascade="save-update"
>
<key column="{$lparentname}_id" not-null="true" />
<one-to-many class="{$package}.{$targetclass}Model" />
</list>
</xsl:when>
<xsl:otherwise>
<property name="{$lcelname}" column="_{translate($lcelname,$ucletters,$lcletters)}" length="{$targetlength}" type="{$targettype}" />
<xsl:comment>Element <xsl:value-of select="$type"/></xsl:comment>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="elname" select="@name"/>
<xsl:variable name="xlcelname">
<xsl:call-template name="lowerfirst">
<xsl:with-param name="name" select="$elname"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="lcelname">
<xsl:choose>
<xsl:when test="$xlcelname='id'">wfid</xsl:when>
<xsl:otherwise><xsl:value-of select="$xlcelname"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="targetlength" >
<xsl:choose>
<xsl:when test="@xsd2hbm:length"><xsl:value-of select="@xsd2hbm:length"/></xsl:when>
<xsl:otherwise>64</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="targettype" >
<xsl:choose>
<xsl:when test="@xsd2hbm:type"><xsl:value-of select="@xsd2hbm:type"/></xsl:when>
<xsl:otherwise>string</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="required">
<xsl:choose>
<xsl:when test="@use='required'">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<property name="{$lcelname}" column="_{translate($lcelname,$ucletters,$lcletters)}" length="{$targetlength}" type="{$targettype}" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="xsd:attribute" mode="contained">
<xsl:variable name="elname" select="@name"/>
<xsl:variable name="xlcelname">
<xsl:call-template name="lowerfirst">
<xsl:with-param name="name" select="$elname"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="lcelname">
<xsl:choose>
<xsl:when test="$xlcelname='id'">wfid</xsl:when>
<xsl:otherwise><xsl:value-of select="$xlcelname"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="targetlength" >
<xsl:choose>
<xsl:when test="@xsd2hbm:length"><xsl:value-of select="@xsd2hbm:length"/></xsl:when>
<xsl:otherwise>64</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="targettype" >
<xsl:choose>
<xsl:when test="@xsd2hbm:type"><xsl:value-of select="@xsd2hbm:type"/></xsl:when>
<xsl:otherwise>string</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="required">
<xsl:choose>
<xsl:when test="@use='required'">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<property name="{$lcelname}" column="_{translate($lcelname,$ucletters,$lcletters)}" length="{$targetlength}" type="{$targettype}" not-null="{$required}"/>
<xsl:comment>Attribute</xsl:comment>
</xsl:template>
<xsl:template match="xsd:any" mode="contained">
<property name="contents" length="10000000" type="text" not-null="false" />
<xsl:comment>Any</xsl:comment>
</xsl:template>
<xsl:template name="lowerfirst">
<xsl:param name="name"/>
<xsl:variable name="f" select="substring($name, 1, 3)"/>
<xsl:variable name="s" select="substring($name, 4)"/>
<xsl:value-of select="concat(translate($f,$ucletters,$lcletters),$s)"/>
</xsl:template>
<xsl:template name="upperfirst">
<xsl:param name="name"/>
<xsl:variable name="f" select="substring($name, 1, 1)"/>
<xsl:variable name="s" select="substring($name, 2)"/>
<xsl:value-of select="concat(translate($f,$lcletters,$ucletters),$s)"/>
</xsl:template>
<xsl:template match="@*|text()" mode="contained"/>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.