1

I went through several tutorials but I couldn't able to work as my XSD import as I want.. please help me on this.

This is my xml

    <?xml version="1.0" encoding="UTF-8"?>    
       <mail>
           <portalname>rezbase_v3</portalname>
           <portalcolor1>'#b0b8da'</portalcolor1>
           <portalcolor2>#0a1e75</portalcolor2>
           <portalcolor3> '#333333'</portalcolor3>
           <portalcolor4> '#e7eaf7'</portalcolor4>
           <portalcolorfont1> '#bcc9ff'</portalcolorfont1>
           <portalcolorfont2> '#000000'</portalcolorfont2>     
      </mail>

and I have seperate xsd for portal styles which looks like this (StyleApplyerDetailsBuilder.xsd)

        <?xml version="1.0" encoding="ISO-8859-1" ?>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"   targetNamespace="http://NamespaceTest.com/StyleApplyerDetailsBuilder" elementFormDefault="qualified" >
     <xs:group name="colors">
      <xs:sequence>
        <xs:element name="portalcolor1" type="xs:string"/>
        <xs:element name="portalcolor2" type="xs:string"/>
        <xs:element name="portalcolor3" type="xs:string"/>
        <xs:element name="portalcolor4" type="xs:string"/>
        <xs:element name="portalcolorfont1" type="xs:string"/>
        <xs:element name="portalcolorfont2" type="xs:string"/> 
      </xs:sequence>
    </xs:group>
             </xs:schema>

and my main xsd is something like this (main.xsd)

   <?xml version="1.0" encoding="ISO-8859-1" ?>
   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:include schemaLocation="../../xsdFactory/commons/StyleApplyerDetailsBuilder.xsd"/>
   <xs:element name="mail">
   <xs:complexType>
    <xs:sequence>
      <xs:element type="xs:string" name="portalname"></xs:element>
            <!-- I need to import my other xsd(StyleApplyerDetailsBuilder) to here so I can validate my xml -->
      <xs:group ref="colors" maxOccurs="unbounded" minOccurs="1"/>        
      </xs:sequence>
   </xs:complexType>
   </xs:element>
  </xs:schema>

what I need is import StyleApplyerDetailsBuilder.xsd to main.xsd and validate the above xml.

RezgHansa
  • 45
  • 11
  • 1
    Have you tried using `xsd:include`? See http://stackoverflow.com/questions/332792/can-i-have-one-xml-schema-xsd-include-another-xml-schema – Tim C Dec 09 '13 at 08:56

1 Answers1

1

You can use

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:include schemaLocation="StyleApplyerDetailsBuilder.xsd"/>
   <xs:element name="mail">
   <xs:complexType>
     <xs:sequence>
       <xs:element type="xs:string" name="portalname"></xs:element>
       <!-- I need to import my other xsd(StyleApplyerDetailsBuilder) to here so I can validate my xml -->
       <xs:element ref="portalcolor1"/>
       <xs:element ref="portalcolor2"/>
       <!-- reference the other elements here -->

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

Usually you would define a group and reference that e.g. in your stylesheet to be included you define

<xs:group name="colors">
  <xs:sequence>
    <xs:element name="portalcolor1" type="xs:string"/>
    <xs:element name="portalcolor2" type="xs:string"/>
    <xs:element name="portalcolor3" type="xs:string"/>
    <xs:element name="portalcolor4" type="xs:string"/>
    <xs:element name="portalcolorfont1" type="xs:string"/>
    <xs:element name="portalcolorfont2" type="xs:string"/> 
  </xs:sequence>
</xs:group>

then you use the include as shown but instead of all the references to the different elements you use

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:include schemaLocation="StyleApplyerDetailsBuilder.xsd"/>
   <xs:element name="mail">
   <xs:complexType>
     <xs:sequence>
       <xs:element type="xs:string" name="portalname"></xs:element>
       <!-- I need to import my other xsd(StyleApplyerDetailsBuilder) to here so I can validate my xml -->
       <xs:group ref="colors"/>        
      </xs:sequence>
    </xs:complexType>
</xs:element>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • is there any other way without giving all the references by providing single refrence access to sub nodes – RezgHansa Dec 10 '13 at 08:47
  • 1
    @RezgHansa, I have added sample code defining and referencing a group. – Martin Honnen Dec 10 '13 at 10:18
  • Hi marting I have changed the question and tried your way but it give me some error like this "org.xml.sax.SAXParseException; systemId: file:/rezsystem/rezsystem_hansa/jboss-4.0.3SP1/server/default/deploy/RezgEmailService.war/xsd/xsdFactory/commons/StyleApplyerDetailsBuilder.xsd; lineNumber: 2; columnNumber: 158; src-include.2.1: The targetNamespace of the referenced schema, currently 'http://NamespaceTest.com/StyleApplyerDetailsBuilder', must be identical to that of the including schema, currently 'null'. " – RezgHansa Dec 17 '13 at 05:50
  • 1
    @RezgHansa, well you have edited your samples considerably it seems. Now you have a target namespace on one schema but no one on the other schema. `xs:include` only works if both schemas have no target namespace or have the same one. Your XML sample with the `` element does not have any namespaces so it is not clear why you would want to put a namespace into one of the schemas. – Martin Honnen Dec 17 '13 at 11:45