0

I have written an application which receives many variation of XML requests. In our business we have to validate XMLs against XSD at the beginning of any request.

The problem:

As I said above I have to validate them at the beginning and those XMLs have almost the same schema and I need to write a general XSD for them.

I have provided some prototype XML for my question:

XML1:

<_9D94DEB4-7C2D-45A5-A4FB-89FB1CF20672>
<Param1>value</Param1>
<Type>Category</Type>    
</_9D94DEB4-7C2D-45A5-A4FB-89FB1CF20672>

XML2: Almost the same schema but root element name is different and it has an extra child element.

<_7603DCD1-F270-43EA-86E3-0FB3161478F6>
<Param1>value</Param1>
<Type>Page</Type>    
<SearchText>Sample</SearchText>
</_7603DCD1-F270-43EA-86E3-0FB3161478F6>

As you can see the root element names are different but their schema is almost the same, How could I write a general XSD for them?

Thanks in advance.

Ali. B
  • 65
  • 9

2 Answers2

0

All you need to do is write each of roots as direct children of your schema element and define types in your XSD.

For example:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="_9D94DEB4-7C2D-45A5-A4FB-89FB1CF20672">
        ...
      <xs:complexType>
        <xs:attribute name="Param2" type="Param2" use="required">
        </xs:attribute>
        <xs:attribute name="Type" type="Type" use="required">
        </xs:attribute>
        <xs:attribute name="SearchText" type="SearchText" use="required">
        </xs:attribute>
      </xs:complexType>
        ...
    </xs:element>
    <xs:element name="_7603DCD1-F270-43EA-86E3-0FB3161478F6">
         ...
      <xs:complexType>
        <xs:attribute name="Param1" type="Param1" use="required">
        </xs:attribute>
        <xs:attribute name="Type" type="Type" use="required">
        </xs:attribute>
      </xs:complexType>
        ...
    </xs:element>
</xs:schema>

<!-- Your defenition and restriction of types-->
<xs:simpleType name="Param1">
    <xs:restriction base="xs:string">
</xs:simpleType>
<xs:simpleType name="Param2">
    <xs:restriction base="xs:string">
</xs:simpleType>
<xs:simpleType name="Type">
    <xs:restriction base="xs:string">
</xs:simpleType>
<xs:simpleType name="SearchText">
    <xs:restriction base="xs:string">
</xs:simpleType>
Nikita. A.
  • 380
  • 2
  • 13
0

The only thing these two XML instances have in common is that both have a Type element whose value is a string. Calling that "almost the same schema" seems rather an exageration. But perhaps there is more commonality than you have shown us?

In principle XSD allows you to validate the instance against a global type in your schema, irrespective of the element name. Whether your particular schema processor provides an API to do that is another question.

Your schema could then simply define the top-level type:

<xs:complexType name="myTopLevelType">
  <xs:sequence>
    <xs:element name="Param1" type="xs:string"/>
    <xs:element name="Type" type="xs:string"/>
    etc
  </xs:sequence>
</xs:complexType>

If you choose Saxon as your schema validator then you can invoke "validation-by-type" from the Java API but not from the command line. In fact, probably the easiest way to do it is to invoke the validation from XSLT:

<xsl:import-schema schemaLocation="mySchema.xsd"/>

then:

<xsl:copy-of select="doc('instance.xml')/*" type="myTopLevelType"/>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • There were some mistakes in my question, for example the root element of my sample XML files were not closed. "Calling that "almost the same schema" seems rather an exageration", So you are saying those XML files don't look like the same? I used the term "almost the same" because I wanted to say the all structure is kinda the same but the root element name is different for each request. and thanks for your impressive answer. – Ali. B Apr 15 '15 at 10:24