XML validation is the process of verifying that an XML document is well-formed and that it adheres to a defined XML schema. The XML documents must be well-formed in order to be processed by webMethods.
A XML schema is a collection of rules that define the structure, content, and syntax of an XML data.
XML validation involves checking for a syntax error in an XML document to ensure that it is well written and adheres to standard rules using either DTD or schema.
Sample XML document:
<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
Sample XML Schema:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
First, we will see how to create a XML document type based on XML Schema
Package> New > Document Type> <specify doc name> click on Next
select XML Schema and click on Next
DT_shipOrders and schema_DT_shipOrders are created.
Now, copy the fully qualified name of schema_DT_shipOrders and paste in validtate service (conformsTo) field..
Negative Response:
Positive Response:
Test the logic with the given xml file (pasted above)
isValid -> true, means that the given XML data is validated against XML Schema.
We need to implement further logic to process the XML document, i.e. to parse the XML to
IS Document and send it to DB or Salesforce etc.
Negative Response:
Intentionally, passed wrong values in quantity field to check negative scenario.
Note: errors document list contains list of errors where the issue occurs based on XML schema.
We need to implement further logic to handle these errors.
Thanks for reading :-)
No comments:
Post a Comment