Thursday, April 28, 2011

How to write xsd file (Enrich mediator) and use it with eclipse IDE

In general, a schema is an abstract representation of an object's characteristics and relationship. XSD specifies how to formally describe the elements in an Extensible Markup Language (XML) document.
Here i will briefly describe how to write simple XSD and use it with eclipse ide.

first we will consider simple xml file

<enrich xmlns:p="http://ws.apache.org/ns/synapse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<p:source clone="false" type="custom" xpath="/default/xpath"/>
<p:target action="replace" type="custom" xpath="/default/xpath"/>
</p:enrich>

Here is the XSd file to describe that XML file. Save this file as enrich.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
           targetNamespace="http://ws.apache.org/ns/synapse"
           xmlns="http://ws.apache.org/ns/synapse">
<xs:element name="enrich">
    <xs:complexType>
        <xs:sequence>       
    <xs:element name="source">
        <xs:annotation>
            <xs:documentation source="description">
                Source Configuration
            </xs:documentation>
        </xs:annotation>
                <xs:complexType>         
                    <xs:attribute name="clone" minOccurs="1" maxOccurs="1">
                        <xs:annotation>
                        <xs:documentation>
                             Weather message can be cloned or use as a reference during the enriching
                         </xs:documentation>
                        </xs:annotation>
                        <xs:restriction base="xs:string">
                     <xs:enumeration value="true"/>
                    <xs:enumeration value="false"/>
                 </xs:restriction>
                         </xs:attribute>
             <xs:attribute name="type" minOccurs="1" maxOccurs="1">
                         <xs:annotation>
                         <xs:documentation>
                            Type that the mediator use from the original message to enrich the modified message that pass through the mediator
                         </xs:documentation>
                         </xs:annotation>
                 <xs:restriction base="xs:string">
             <xs:enumeration value="custom"/>
                         <xs:enumeration value="envelope"/>
                        <xs:enumeration value="body"/>
                 <xs:enumeration value="property"/>
                        <xs:enumeration value="inline"/>
                 </xs:restriction>
                         </xs:attribute>
            <xs:attribute name="xpath" type="xs:string" use="optional"/>
            <xs:attribute name="property" type="xs:string" use="optional"/>
         </xs:complexType>          
        </xs:element>      
    <xs:element name="target">
            <xs:annotation>
                <xs:documentation source="description">
                Target Configuration
                </xs:documentation>
            </xs:annotation>
            <xs:complexType>
        <xs:attribute name="action" minOccurs="1" maxOccurs="1">
                    <xs:annotation>
                        <xs:documentation>
                            The relevant action can be applied to outgoing message.
                        </xs:documentation>                    </xs:annotation>
                <xs:restriction base="xs:string">
                        <xs:enumeration value="replace"/>
                       <xs:enumeration value="child"/>
                <xs:enumeration value="sibiling"/>
            </xs:restriction>
                </xs:attribute>
        <xs:attribute name="type" minOccurs="1" maxOccurs="1">
                    <xs:annotation>
                        <xs:documentation>
                           Type of enriching the outgoing message.
                        </xs:documentation>
                    </xs:annotation>
                <xs:restriction base="xs:string">
                        <xs:enumeration value="envelope"/>
                       <xs:enumeration value="body"/>
                <xs:enumeration value="property"/>
                       <xs:enumeration value="custom"/>
            </xs:restriction>
                </xs:attribute>
        <xs:attribute name="xpath" type="xs:string" use="optional"/>
        <xs:attribute name="property" type="xs:string" use="optional"/>
    </xs:complexType>
    </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>  
</xs:schema>
If you need to find more about enrich mediator you can visit
http://wso2.org/project/esb/java/3.0.1/docs/mediators/enrich.html


If you need more examples of xsd's written for various uses visit
https://svn.apache.org/repos/asf/synapse/trunk/java/repository/schema/mediators/filter/

Then we will see how we can use this with eclipse IDE
01.Open eclipse IDE
02.From window menu goto preferences
   Window>Preferences
   Then you will see image shown below



03.from the menu appearing you must select xml catalog as shown below


04.Then add xml catalog entry as shown in the figure shown in figure
05.browse your xsd file and save it
06.create new eclipse project and add new XML file
   from menu New > XML
07.Create new xml as shown in the below images.

   create xml from xml schema

select xml category and then point the xml



  Than you will get new xml file as shown below

Now you have created it successfully.

No comments:

Post a Comment