JavaDev
JavaDev

Reputation: 1

Apache Camel Validate input xml Order against XSD

I have to validate the order of elements from the incoming xml request against xsd in apache-Camel. The incoming request in most cases would be only part of the xsd, which should be considered as a valid request and only look for ordering of elements.

Code:

router.rest("/testing").id("ENDPOINT-TESTING").post().type(xyz.class).outType(xyz.class).consumes(MIME_TYPE).produces(MIME_TYPE).skipBindingOnErrorCode(true).to("direct:xyz");

router.from("direct:xyz").routeId("handle-xyz-request").to("validator:test.xsd").process("validationProcessor")
          .bean(xyzService.class, "processXYZ(${body})") ;

Issue

When I use validator:test.xsd in the route configuration, it does not look for order. However, it scans the request xml for presence of all elements in the xsd and throws exception. When I remove minoccurs from xsd for a particular node. This indicates that the validator gets called but it just doesn't look for the ordering of elements in xsd. (Note:I have used xs:sequence in my xsd)

XSD

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="y-ML">
<xs:complexType>
<xs:sequence>
<xs:element name="Header" minOccurs="1">
<xs:complexType>
<xs:sequence>
    <xs:element name="Product">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Supplier">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:maxLength value="12" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element type="xs:string" name="ID" />
                <xs:element type="xs:string" name="EID" />
                <xs:element name="ProductID">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                                <xs:attribute type="xs:string" name="type" />
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>                           
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Provisioning" minOccurs="0">
<xs:complexType>
<xs:sequence>
    <xs:element name="ServiceFlag" maxOccurs="unbounded"
        minOccurs="0">
        <xs:complexType>
            <xs:sequence>
                <xs:element type="xs:string" name="Service" />
                <xs:element type="xs:string" name="FlagValue" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element type="xs:string" name="Brand" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:float" name="version" />
</xs:complexType>
</xs:element>
</xs:schema>

Request xml

Have changed the ordering of element ProductID , which should get validated and throw an exception.

<y-ML version="1.0">
    <Header>
        <Product>
            <ProductID type="PID">658932145</ProductID>
            <Supplier>Samsung</Supplier>
            <ID>213517</ID>
            <EID>SEDT7888125</EID>
        </Product>      
    </Header>
</y-ML>

Upvotes: 0

Views: 793

Answers (2)

JavaDev
JavaDev

Reputation: 1

I Just had to turn off the RestBindingMode, which solved my issue

router.rest("/testing").id("ENDPOINT-TESTING").post().bindingMode(RestBindingMode.off).type(xyz.class).outType(xyz.class).consumes(mimetype).produces(mimetype).skipBindingOnErrorCode(true).to("direct:xyz");

Upvotes: 0

kjhughes
kjhughes

Reputation: 111696

The posted error message does not match the actual validation problem in the posted XML and XSD.

Actual validation problem:

[Error] try.xml:7:29: cvc-complex-type.2.4.a: Invalid content was found starting with element 'ProductID'. One of '{Supplier}' is expected.

Moving ProductID below EID where the XSD has it resolves the error:

<y-ML version="1.0">
  <Header>
    <Product>
      <Supplier>Samsung</Supplier>
      <ID>213517</ID>
      <EID>SEDT7888125</EID>
      <ProductID type="PID">658932145</ProductID>
    </Product>      
  </Header>
</y-ML>

Upvotes: 0

Related Questions