简体   繁体   English

WSO2 ESB:基于XML的XSD验证

[英]WSO2 ESB: XSD validation on XML

I am using WSO2 ESB and trying to create XML dynamically and apply some validation on XML. 我正在使用WSO2 ESB,并尝试动态创建XML并在XML上应用一些验证。 But gets declaration in XML. 但是获取XML声明。 Please help me to correct below error. 请帮助我纠正以下错误。

Here is my sample code. 这是我的示例代码。

My Test API is 我的测试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 is 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>

Thanks 谢谢

This will work for you. 这将为您工作。 Replace any hard coded with your dynamic. 用您的动态代码替换所有硬编码。 I am assuming C# and .Net 我假设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;
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM