Showing posts with label xsd. Show all posts
Showing posts with label xsd. Show all posts

Saturday, 30 December 2017

Convert xsd:anyType data into XML Data

Recently I encountered this requirement to Accept AnyType Data and convert it into a valid xml data.

Few important things to consider is: The AnyType data payload should be a valid one with the namespaces defined.The type to be converted to should be declared in an xsd.

In my process I had to get this anyType data and convert it to a format with which I had to invoke a service.
Though it sounds very simple I felt its better to document it in a very easily accessible way. I followed the following steps for the same in SOA 11.1.1.4:

  1. Create 2 variables to which you want to convert the anytype Data to.
  2. Use the following function to Parse the value:oraext:parseXML(bpws:getVariableData('inputVariable','request','/ns2:Request/ns2:Details/ns2:xmlData')) where xmlData is of datatype Any.Please note that when you are selecting the anyType data, in Assign activity this will not get copied in the expression builder, hence you need to manually add this data.
  3. Once this Assign activity is completed, use Assign or Transform activity to transform the values within the xml generated in the above mentioned step to another variable which is used to invoke the service.
I hope this helps for people looking for usage of oraext:parseXML.

Saturday, 2 September 2017

Creating a WSDL File From XSD File

In this post, lets learn how we can create services interface - WSDL from schema file using Oracle JDeveloper 11g. Here, we attempt to create a service which when provided with an Employee Id returns the Employee Name. Incase no matching records are found for an Id, it simply returns the string value - "Unknown".


# We start with opening the JDeveloper 11g


# Create an application - "MyWSDLApplication" using the Generic Application template.


# Name the project as "MyWSDLProject" and select "Web Services" as the project technologies.


# The Application Navigator view should look like this


# Lets create the schema file first. For this, right click on the project and from the context menu, select New. In the Gallery that opens, select General -> XML -> XML Schema


# Name the schema as Employee.xsd

# The schema editor window opens as shown in the figure below.

# We will be adding two elements - EmpId and EmpName here. Right click on the root schema element and from the context menu, select Insert inside schema -> element 

# Once the two elements are specified, we provide them types by right click on the element and from context menu - select Set Type as shown below

# As depicted in the figure below, EmpId type is taken as long while EmpName type is string.

# With the schema defined, we move onto creating the WSDL document. Right click on the project and from context Menu, select New. In the gallery, choose Business Tier -> Web Services -> WSDL Document. Press OK button.
# Name WSDL document as EmployeeWSDL


# Once OK button is presses, the JDeveloper editor view would look like this

# Arrange the schema and WSDL editor windows in a vertical split arrangement like as shown below. This can be achieved by dragging the Employee.xsd tab below.


# Now, we begin creating the WSDL document. Drag and Drop the EmpId element (input for our case) onto the Port Type window. This opens the 'Create Port Type' window. Lets name it as EmployeeDetail.

# After our previous action, the WSDL editor should look like this

# We have a problem here as can be seen from the image. The output is shown as EmpId while we expect it to be EmpName.
# To correct this, we select the output node in the Port Types window. Drag and Drop EmpName element from the schema onto this node. With this, the WSDL document appears like 

# Next we change the name of the messages to EmployeeRequestMessage and EmployeeResponseMessage using the property inspector window.
# With the changes in message name, we need to remap these to the operation messages as else these appear with errorneous icon like we have output icon below. To correct this, drag and drop the relevant messages onto the correct nodes. Example : EmployeeRequestMessage is dropped onto the input node of EmpIdOperation and EmployeeResponseMessage is dropped onto the output of EmpIdOperation

# Here, we make the operation name more meaningful using property Inspector - getEmployeeNameOperation.

# The complete WSDL view should appear like this

# Now, we drag and drop the EmployeeDetail node onto the Bindings/Partner Link Types window. In the Create Binding window, we check the SOAP12.

# This should result in the view as shown below :

# Next, drag and drop the root node from the Bindings window onto the Services window. This results in the view as shown below

# At this point, the structure window should look like this. This completes our WSDL document creation process.


# We can validate our WSDL document by doing right click on the EmployeeWSDL (in the Applications Navigator Window) and from context menu, choose Validate WSDL. Confirm the message in the Messages- Log window.


This completes our exercise to create WSDL document using the schema file. Next, we will create Web Service using this WSDL document.

Saturday, 3 December 2016

XSD

XML
XML stands for extensible markup language. XML is designed for transport and store data. XML tags are case sensitive. <Message>This is incorrect</message> but <message>This is correct</message>
HTML was designed for display data.

XML Schema
An XML schema describes the structure of an XML document. An XML schema is also called XML schema definition.
An XML schema:
·         Defines elements that can appear in a document.
·         Defines attributes that can appear in a document.
·         Defines which element are child elements.
·         Defines the order of child elements.

XML schemas are Extensible because they are written in XML:
·         Reuse your schema in other schema.
·         Create your own data types derived from the standard types.
·         Reference multiple schemas in the same document


Sample XSD:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Payment" xmlns:tns="http://www.example.org/Payment" elementFormDefault="qualified" xmlns:Q1="http://xmlns.telenor.mm/Schema/Common/1.0/Common.xsd">

<import schemaLocation="common.xsd" namespace="http://xmlns.telenor.mm/Schema/Common/1.0/Common.xsd">
</import>
              <element name="ReversalReq" type="string"></element>
                <element name="ReversalRes" type="string"></element>
                <complexType name="ReqType">
                                <sequence>
                                                <element ref="Q1:TransactionReference"></element>
                                                <element ref="Q1:CustomRef"></element>
                                </sequence>
                </complexType>
                <complexType name="ResType">
                <sequence>
                                                <element ref="Q1:TransactionReference"></element>
                                                <element ref="Q1:CustomRef"></element>
                                </sequence>
                </complexType>
</schema>


XSD Element
A simple element is an XML Element that contains only text. It can’t contain any other elements or attributes.
Simple XML elements
       Corresponding simple element definations
<lastname>Pramanik</lastname>         
<firstname>Sandip</ firstname >
<age>29</age>
<city>Kolkata</ city >
 <xs:element name=”lastname” type=”xs:string”/>
<xs:element name=” firstname” type=”xs:string”/>
<xs:element name=” age” type=”xs:integer”/>
<xs:element name=” city” type=”xs:string”/>



XSD Attribute
Attribute can contain other elements and mixture of elements.
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
  <title lang="en">Harry Potter</title>
  <price>29.99</price>
</book>
<book>
  <title lang="en">Learning XML</title>
  <price>39.95</price>
</book>
</bookstore>
                                                                                                                   lang- Attribute

                                                                                                                   price-Element


What is XML Namespace?
XML namespace provide a method to avoid element name conflicts.
<table>
        <tr>
                <td>Apple</td>
                <td>Banana</td>
        </tr>
</table>
<table>
        <tr>
                <name>Apple</td>
                <type>Fruit</td>
        </tr>
</table>
Solving the name conflict using a prefix
<h:table>
        <h:tr>
                <h:td>Apple</h:td>
                <h:td>Banana</h:td>
        </h:tr>
</h:table>
<f:table>
        <f:tr>
                <f:name>Apple</f:td>
                <f:type>Fruit</f:td>
        </f:tr>
</f:table>


Default Namespace:
Defining a default namespace for an element saves us from using prefixes in all the child elements.
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
</schema>


Target Namespace:
The schema will be assigned to the namespace http://www.example.org/Payment
If different teams start working on different files, then you have the possibility of name clashes, and it would not always be obvious where a definition had come from. The solution is to place the definition for each schema file within a distinct namespace.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace=http://www.example.org/Payment
 xmlns:tns="http://www.example.org/Payment" elementFormDefault="qualified" xmlns:Q1="http://xmlns.telenor.mm/Schema/Common/1.0/Common.xsd">

In WSDL we are to call that targetNamespace.
<types>
                <schema>
                                <import namespace=”http://www.example.org/Payment” schemaLocation=”xyz.xsd”/>
                </schema>
</types>



elementFormDefault="qualified" indicates that any elements used by the XML instance document which were declared in the schema must be namespace qualified.



Simple Type VS Complex Type
<element name=”email”>
                <simpleType>
                                <restriction base=”xs:string”/>
                </simpleType>
</element>
<element name=”Preferences”>
                <complexType>
                                <sequence>
                                                <element name=”email” type=”email”/>
                                </sequence>
                </ complexType >
</element>

#Simple types can only have content directly contained between the elements opening and closing tags.
#Complex types can have attributes, can contain other elements, and can contain a mixture of elements.
#Simple types will not contain other elements, they only contain data.
#In Complex types, allow elements in their content and may carry attributes.
#In Simple types cannot have element content and cannot carry attributes.


Including brings in definitions that belong to the same target namespace as the enclosing schema element.

Importing brings in definitions that belong to a different target namespace from the enclosing schema element.

Monday, 28 November 2016

Understand "elementFormDefault" and "attributeFormDefault"

elementFormDefault="qualified"
 
Here, i am providing an example to create an xsd with 
elementFormDefault="qualified"
NOTE: observe qualifiers in xml (blue color text).
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
               targetNamespace="http://example.org/calcSchemaDoc"
               xmlns:tns="http://aimtech.org/calcSchemaDoc"
               elementFormDefault="qualified">
  
      <element name="response">           
            <complexType>
            <sequence>       
                  <element name="fieldOne" type="integer"/> 
                  <element name="fieldTwo" type="integer"/> 
                  <element name="result" type="integer"/> 
                  <element name="typeOfOperation" type="string"/>
            </sequence> 
            </complexType>     
      </element>
</schema>

By Following above XSD if we create a valid XML, then it looks as below
valid XML:
<
ns:response xmlns:ns="http://example.org/calcSchemaDoc">
      <ns:fieldOne>100</
ns:fieldOne>
      <
ns:fieldTwo>200</ns:fieldTwo>
      <
ns:result>300</ns:result>
      <
ns:typeOfOperation>add</ns:typeOfOperation>
</
ns:response>

XPath Requirement:
a) create Xpath to get the value of "fieldTwo" element.
XPath: /ns:response/ns:result/text()
result: 300
*****************************************************************************************************
elementFormDefault="unqualified"


Here, i am providing an example to create an xsd with elementFormDefault="unqualified" 

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://aimtech.org/calcSchemaDoc"
              xmlns:tns="http://example.org/calcSchemaDoc"
              elementFormDefault="unqualified">

      <element name="response">
            <complexType>
            <sequence> 
                  <element name="fieldOne" type="integer"/>
                  <element name="fieldTwo" type="integer"/> 
                  <element name="result" type="integer"/> 
                  <element name="typeOfOperation" type="string"/> 
             </sequence> 
            </complexType>      </element>
</schema>


valid XML:

<?xml version="1.0" encoding="UTF-8"?> 
<ns:response xmlns:ns="http://example.org/calcSchemaDoc">
      <fieldOne>100</fieldOne>
      <fieldTwo>200</fieldTwo>
      <result>300</result>
      <typeOfOperation>add</typeOfOperation>
</ns:response>
NOTE: No need to use qualifiers in xml sub elements, because as per XSD we set elementFormDefault="unqualified"
 
XPath Requirement:

a) create Xpath to get the value of "fieldTwo" element.
XPath: /ns:response/result/text()
result: 300