user1096618
user1096618

Reputation: 45

xslt transforming xml to corresponding name value pairs

I am new to XSLT I am trying to transform a name value pair to its corresponding XML. This feature is primarily used in case of special extensions to a standard. The file I want to transform is the following.

<ODEventNotificationExtraField>
<callCode>1</callCode>
<callbackType>All </callbackType>
<callbackEmail>[email protected] </callbackEmail>
</ODEventNotificationExtraField>

to the following:

<?xml version="1.0" encoding="UTF-8"?>
<extensionList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="ExtensionItems.xsd">
<extensionsItem>
<extName> callCode</extName>
<extValue>1</extValue>
<extType>integer</extType>
</extensionsItem>
<extensionsItem>
<extName>callbackType</extName>
<extValue>All</extValue>
<extType>string</extType>
</extensionsItem>
<extensionsItem>
<extName>callbackEmail</extName>
<extValue>[email protected]</extValue>
<extType>string</extType>
</extensionsItem>
</extensionsList>

Based On the Field Name for example CallCode I will know it is of type integer( it needs to be hardcoded in xslt) the xmlschema for the incoming data is defined as

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:simpleType name="CallbackType">
<xs:restriction base="xs:string">
<xs:enumeration value="Restoration"/>
<xs:enumeration value="Estimated"/>
<xs:enumeration value="All"/>
<xs:enumeration value="None"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="ODEventNotificationExtraField">
<xs:complexType>
<xs:sequence>
<xs:element name="callCode" type="xs:integer" minOccurs="0"/>
<xs:element name="callbackType" type="CallbackType" minOccurs="0"/>
<xs:element name="callbackEmail" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Upvotes: 1

Views: 1199

Answers (1)

Wayne
Wayne

Reputation: 60414

The following stylesheet produces the intended result:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <!-- element list -->
    <xs:element name="callCode" type="xs:integer"/>
    <xs:element name="callbackType" type="xs:string"/>
    <xs:element name="callbackEmail" type="xs:string"/>
    <xsl:template match="/">
        <extensionItems xs:noNamespaceSchemaLocation="ExtensionItems.xsd">
            <extensionsList>
                <xsl:apply-templates select="/ODEventNotificationExtraField/*"/>
            </extensionsList>
        </extensionItems>
    </xsl:template>
    <xsl:template match="*">
        <extensionsItem>
            <extName><xsl:value-of select="local-name()"/></extName>
            <extValue><xsl:value-of select="."/></extValue>
            <extType>
                <xsl:value-of 
                    select="substring-after(document('')/*/xs:element[
                        @name=current()/local-name()]/@type, ':')"/>
                </extType>
        </extensionsItem>
    </xsl:template>
</xsl:stylesheet>

Notice the hard-coding of element types and use of document to retrieve them.

Output:

<extensionItems xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                xs:noNamespaceSchemaLocation="ExtensionItems.xsd">
   <extensionsList>
      <extensionsItem>
         <extName>callCode</extName>
         <extValue>1</extValue>
         <extType>integer</extType>
      </extensionsItem>
      <extensionsItem>
         <extName>callbackType</extName>
         <extValue>All</extValue>
         <extType>string</extType>
      </extensionsItem>
      <extensionsItem>
         <extName>callbackEmail</extName>
         <extValue>[email protected]</extValue>
         <extType>string</extType>
      </extensionsItem>
   </extensionsList>
</extensionItems>

Upvotes: 1

Related Questions