Mapping XML elements with enumeration

When you are mapping XML elements with enumeration in the XML mapping editor, an XSL Choose provides a way to conditionally set target elements. This topic uses an example to show you how to use XSL Choose to map XML elements that use enumeration.

An enumeration specifies a set of valid values for an element. For example, a ship order schema may contain an addressTypeIndicator element, which defines the choice of location for delivery. The element addressTypeIndicator is of type IndicatorTypes, which has three valid values defined. The schema looks like this:
	<xsd:complexType name="SourceBOwithEnum">
		<xsd:sequence>
			<xsd:element name="addressTypeIndicator"
				type="se:IndicatorTypes">
				<xsd:annotation>
					<xsd:documentation>
						Indicates the location for delivery
					</xsd:documentation>
				</xsd:annotation>
			</xsd:element>
		</xsd:sequence>
	</xsd:complexType>
	
	<xsd:simpleType name="IndicatorType">
		<xsd:annotation>
			<xsd:documentation>
				Type defining an indicator as 1 character
			</xsd:documentation>
		</xsd:annotation>
		<xsd:restriction base="xsd:string">
		</xsd:restriction>
	</xsd:simpleType>

	<xsd:simpleType name="IndicatorTypes">
		<xsd:restriction base="se:IndicatorType">
			<xsd:enumeration value="1" />
			<xsd:enumeration value="2" />
			<xsd:enumeration value="3" />
		</xsd:restriction>
	</xsd:simpleType>
The schema maps to a target shipToAddressTypeIndicator element, which contains the delivery address. The element shipToAddressTypeIndicator is of type shipToAddressTypes, which contains the valid addresses for delivery. The schema looks like this:
	<xsd:complexType name="TargetBOwithEnum">
		<xsd:sequence>
			<xsd:element name="shipToAddressTypeIndicator"
				type="te:shipToAddressTypes">
				<xsd:annotation>
					<xsd:documentation>
						Indicates that the mailing address is the same
						as the business address.
					</xsd:documentation>
				</xsd:annotation>
			</xsd:element>
		</xsd:sequence>
	</xsd:complexType>

	<xsd:simpleType name="shipToAddressType">
		<xsd:annotation>
			<xsd:documentation>
				Type defining an indicator as 1 character
			</xsd:documentation>
		</xsd:annotation>
		<xsd:restriction base="xsd:string">
		</xsd:restriction>
	</xsd:simpleType>

	<xsd:simpleType name="shipToAddressTypes">
		<xsd:restriction base="te:shipToAddressType">
			<xsd:enumeration value="Warehouse 1" />
			<xsd:enumeration value="Warehouse 2" />
			<xsd:enumeration value="default location" />
		</xsd:restriction>
	</xsd:simpleType>

The following image shows the source and target schema described above in the XML mapping editor.
Source and target schema in the XML mapping editor

Map the elements using the procedure described below. For step by step instructions on defining XSL choose, see Adding <xsl:choose>instructions to your mapping:

  1. First, use the Create Mapping function to map the source element addressTypeIndicator to the target element shipToAddressTypeIndicator
  2. Add an XSL Choose instruction on the shipToAddressTypeIndicator element, with the following content
    <xsl:choose>
      1  <xsl:when test="/se:invoke/se:addressTypeIndicator='1'">
      2    <xsl:value-of select="string('Warehouse 1')"/>
      </xsl:when>
      3  <xsl:when test="/se:invoke/se:addressTypeIndicator='2'">
        4  <xsl:value-of select="string('Warehouse 2')"/>
      </xsl:when>
       5 <xsl:otherwise>
       6   <xsl:value-of select="string('default location')"/>
      </xsl:otherwise>
    </xsl:choose>
    
    Let's examine the XSL Choose:
    •  1  Invokes statement  2  when the value of the source element addressTypeIndicator is 1.
    •  2  Sets the value of the shipToAddressTypeIndicator target element to Warehouse 1.
    •  3  Invokes statement  4  when the value of the source element addressTypeIndicator is 2.
    •  4  Sets the value of the shipToAddressTypeIndicator target element to Warehouse 2.
    •  5  Invokes statement  6  when the value of the source element addressTypeIndicator is anything other than 1 or 2..
    •  6 Sets the value of the shipToAddressTypeIndicator target element to default location.
Let's suppose this was your input to the XSL transform:
<?xml version="1.0" encoding="UTF-8"?>
<se:invoke xmlns:se="http://MappingEnum/SourcewithEnum" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://MappingEnum/SourcewithEnum SourcewithEnum.xsd ">
  <se:addressTypeIndicator>1</se:addressTypeIndicator>
</se:invoke>
Given the above input, this would be the output xml:
<?xml version="1.0" encoding="UTF-8"?>
<te:invoke xmlns:te="http://MappingEnum/TargetwithEnum" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <te:shipToAddressTypeIndicator>Warehouse 1</te:shipToAddressTypeIndicator>
</te:invoke>
For more information on XSL choose syntax, and enumerations, see the XSLT specification at http://www.w3.org/TR/xslt .

Feedback
(C) Copyright IBM Corporation 2005, 2006. All Rights Reserved.