WSO2 ESB:XML上的XSD验证

时间:2014-09-09 15:35:40

标签: xml validation xsd wso2esb

我正在使用WSO2 ESB并尝试动态创建XML并对XML应用一些验证。 但是在XML中获取声明。 请帮我纠正以下错误。

这是我的示例代码。

我的测试API是

<resource methods="GET" uri-template="/{id}/transactions?page={page}&limit={limit}&startDate={startDate}&endDate={endDate}" faultSequence="ReturnError">
  <inSequence>
     <property name="uri.var.page" expression="$ctx:uri.var.page" scope="default" type="INTEGER"></property>
     <property name="uri.var.limit" expression="$ctx:uri.var.limit" scope="default" type="INTEGER"></property>

     <enrich>
        <source type="inline" clone="true">
           <jsonObject><id>1</id><page>1</goalId><limit>1</limit></jsonObject>
        </source>
        <target type="body" />
    </enrich>
    <validate>
        <schema key="MyXsd"></schema>
        <on-fail>
           <property name="clientErrorCode" value="2000"></property>
           <property name="clientErrorMessage" value="Invalid Param."></property>
           <property name="HTTP_SC" value="400" scope="axis2"></property>
           <sequence key="RetrunErrorWithMessage"></sequence>
        </on-fail>
     </validate>

   <inSequence>
</resource>

MyXsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="jsonObject">
     <xs:complexType>
        <xs:sequence>

                    <xs:element type="xs:positiveInteger" name="id"></xs:element>
                    <xs:element type="xs:positiveInteger" name="page"></xs:element>
                    <xs:element type="xs:positiveInteger" name="limit"></xs:element>

        </xs:sequence>
     </xs:complexType>
  </xs:element>

由于

1 个答案:

答案 0 :(得分:0)

这对你有用。用您的动态替换任何硬编码。我假设C#和.Net

    static class XPathValidation
{
    public static void validate(string assignmentXML)
    {
        try
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.Schemas.Add("http://tempuri.com/CCC/assignment/v1", @"D:\my.xsd");
            settings.ValidationType = ValidationType.Schema;

            StringReader strm = new StringReader(assignmentXML);

            XmlReader reader = XmlReader.Create(strm, settings);
            XmlDocument document = new XmlDocument();
            document.Load(reader);

            ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationHandler);

            // call to Validate 
            document.Validate(eventHandler);

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    static void ValidationHandler(object sender, ValidationEventArgs e)
    {
        switch (e.Severity)
        {
            case XmlSeverityType.Error:
                Console.WriteLine("Error: {0}", e.Message);
                break;
            case XmlSeverityType.Warning:
                Console.WriteLine("Warning {0}", e.Message);
                break;
        }

    }
}