Skip to content

Instantly share code, notes, and snippets.

@codyherring
Created September 16, 2013 21:18
Show Gist options
  • Save codyherring/6586742 to your computer and use it in GitHub Desktop.
Save codyherring/6586742 to your computer and use it in GitHub Desktop.
XSLT HELL
Data:
a > b > c > d > e
XSD:
<xs:element name="department">
<xs:annotation>
<xs:documentation>Merchant defined department hierarchy.</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="first_level_department">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="255"/>
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element minOccurs="0" name="second_level_department">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="255"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element minOccurs="0" name="third_level_department">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="255"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
XSLT:
<xsl:template name="parseMerchantCategory">
<xsl:param name="flatName" select="."/>
<xsl:param name="levelNumber" select="1"/>
<xsl:param name="levelName">_level_department</xsl:param>
<xsl:choose>
<xsl:when test="contains($flatName, '&gt;')">
<xsl:choose>
<xsl:when test="$levelNumber = 3">
<xsl:element name="{concat($levelName, $levelNumber)}">
<xsl:value-of select="normalize-space($flatName)"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{concat($levelName, $levelNumber)}">
<xsl:value-of select="normalize-space(substring-before($flatName, '&gt;'))"/>
</xsl:element>
<xsl:call-template name="parseMerchantCategory">
<xsl:with-param name="flatName" select="substring-after($flatName, '&gt;')"/>
<xsl:with-param name="levelNumber" select="$levelNumber + 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{concat($levelName, $levelNumber)}">
<xsl:copy-of select="normalize-space($flatName)"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment