Created
September 16, 2013 21:18
-
-
Save codyherring/6586742 to your computer and use it in GitHub Desktop.
XSLT HELL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '>')"> | |
<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, '>'))"/> | |
</xsl:element> | |
<xsl:call-template name="parseMerchantCategory"> | |
<xsl:with-param name="flatName" select="substring-after($flatName, '>')"/> | |
<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