Validate Documents Against an XSD Schema with the XML Module | MuleSoft Documentation
# Validate Documents Against an XSD Schema with the XML Module
The XML module **Validate schema** operation validates that the input content is compliant with a given XSD schema. The operation supports referencing many schemas, using a comma (`,`) as a separator. By default, this operation looks for the input document at the message payload level, but you can supply your own input as well. Note that you can use the **Validate schema** operation inside the Validation module **All** scope.
## Configure the Validation Schema Operation in Studio
In the following example, you validate XML schemas that contain a script:
  1. In Studio, drag the **Validate schema** operation to your flow. In the **Schemas** field, add your schema files, separating filenames by using a comma, for example, `schema1.xsd, schema2.xsd`.
  2. Drag a **Flow Reference** component to the right of the **Validate schema** operation.
  3. Set **Flow name** to `processValidDocument`.


In the **Configuration XML** editor, the `<xml-module:validate-schema>` configuration looks like this:
```
<flow name="process">
  <xml-module:validate-schema schemas="schema1.xsd, schema2.xsd" />
  <flow-ref name="processValidDocument" />
</flow>
```

In the following example, you configure the **Validate schema** operation by supplying your own input:
  1. In Studio, drag the File **Read** operation to your flow.
  2. In the configuration screen, set **File Path** to `document.xml`.
  3. In the **Advanced** tab, set **Target Variable** to `xmlDoc`.
  4. Drag the XML module **Validate schema** operation to the right of the **Read** operation.
  5. In the **Schemas** field, add your schemas and separate their names by using commas, for example, `schema1.xsd, schema2.xsd`.
  6. Set the **Content** field to the XML content to validate, for example, `#[vars.xmlDoc]`.
  7. Drag a **Flow Reference** component to the right of the **Validate schema** operation.
  8. Set **Flow name** to `processValidDocument`.


In the **Configuration XML** editor, the `<xml-module:validate-schema>` and `<xml-module:content>` configurations look like this:
```
<flow name="process">
  <file:read path="document.xml" target="xmlDoc" />
  <xml-module:validate-schema schemas="schema1.xsd, schema2.xsd">
    <xml-module:content>#[vars.xmlDoc]</xml-module:content>
  </xml:module:validate-schema>
  <flow-ref name="processValidDocument" />
</flow>
```

## Manage the Validation Error
If the validation is successful, the flow continues to the next operation. However, if the validation fails, an `XML-MODULE:SCHEMA_NOT_HONOURED` error is raised. Because the validation can fail for more than one reason, the error description contains a list of messages. Each message contains a `SchemaViolation` object that has the following structure:
```
{
 lineNumber: Number,
 columnNumber: Number,
 description: String
}
```

In the following example, you configure the **Validate schema** operation and the `XML-MODULE:SCHEMA_NOT_HONOURED` error in the Error Handler component:
  1. In Studio, drag the **Try** scope component to your flow.
  2. Drag the XML module **Validate schema** operation into the **Try** scope component.
  3. In the **Schemas** field, add your schema, for example, `schema.xsd`.
  4. In your flow, click the **Error handling** arrow to expand the error-handling section.
  5. Drag the **On Error Propagate** component into the **Error handling** section.
  6. Set the **Type** field to the Mule error handled by this on-error strategy, for example, `XML-MODULE:SCHEMA_NOT_HONOURED`.
  7. Deselect **Enable Notifications** and **Log Exception** fields.
  8. Drag a **For Each** scope component into the **On Error Propagate** component to iterate over each `errorMessage.payload`.
  9. Set the **Collection** field to the expression that splits the payload into individual pieces, for example, `#[error.errorMessage.payload]`.
  10. Drag a **Logger** component into the **For Each** scope component to log the XML module error.
  11. Set the **Message** field to the expression that contains the `SchemaViolation` objects that describe the message, for example, `#['At line: $(payload.lineNumber), column: $(payload.columnNumber) → $(payload.description)']`.
  12. Save your changes.
  13. Click the project name in **Package Explorer** and then click **Run > Run As > Mule Application**.
  14. Navigate to the Console view to read the logger message:
```
ERROR 2018-02-16 14:35:45,722 [[MuleRuntime].cpuIntensive.01: [SchemaValidationTestCase#extractErrorsUsingExpressions].extractErrorsFromException.CPU_INTENSIVE @411e886b] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: At line: -1, column: -1 -> cvc-complex-type.2.4.a: Invalid content was found starting with element 'fail'. One of '{used}' is expected.
```



In the **Configuration XML** editor, the `<on-error-propagate type="XML-MODULE:SCHEMA_NOT_HONOURED">` configuration looks like this:
```
 <flow name="extractErrorsFromException">
  <try>
    <xml-module:validate-schema schemas="schema.xsd" />
    <error-handler>
      <on-error-propagate type="XML-MODULE:SCHEMA_NOT_HONOURED">
        <foreach collection="#[error.errorMessage.payload]">
          <logger level="ERROR" message="#['At line: $(payload.lineNumber), column: $(payload.columnNumber) -> $(payload.description)']" />
        </foreach>
      </on-error-propagate>
    </error-handler>
  </try>
</flow>
```

## Configure Schema Text Content
In the following example, you configure the **Schema contents** field of the **Validate schema** operation. Enter the schema text into this field to perform a schema validation.
To add your schema content, you only provide either schema or schema content, but not both. You can either upload your file in the **Schemas** field, or manually add the direct schema text content in the **Schema contents** field.   
---  
  1. In Studio, select the **Validate schema** operation from your flow.
  2. In the **Validate schema** operation configuration screen, set **Schema contents** to **Edit inline**.


  3. Click the plus sign to add a schema name and text.
  4. In the **Schema content** window’s **Schema name** field, enter a schema name, for example, `schema1.xsd`.
  5. In the **Schema text** field, enter the schema text content, for example:


```
<?xml version = "1.0"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
  <xs:element name = 'class'>
   <xs:complexType>
     <xs:sequence>
       <xs:element name = 'student' type = 'StudentType' minOccurs = '0'
        maxOccurs = 'unbounded' />
     </xs:sequence>
   </xs:complexType>
  </xs:element>
  <xs:complexType name = "StudentType">
   <xs:sequence>
     <xs:element name = "firstname" type = "xs:string"/>
     <xs:element name = "lastname" type = "xs:string"/>
     <xs:element name = "nickname" type = "xs:string"/>
     <xs:element name = "marks" type = "xs:positiveInteger"/>
   </xs:sequence>
   <xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
  </xs:complexType>
</xs:schema>
```

  6. Click **Finish**.


## See Also
  * XML Module Examples
  * XML Module Overview