Link to home
Start Free TrialLog in
Avatar of Andyfungkl
Andyfungkl

asked on

Problem converting DTD to XML schema

Hi gurus,

I have a hard time converting the following DTD definitions to XML schema. First, the DTD(only the portion I have problems with):

<!ELEMENT OtherServer (URL | MSISDN | (URL, MSISDN))>
<!ELEMENT URL (#PCDATA)>
<!ELEMENT MSISDN (#PCDATA)>

Below is what XMLSpy or XMLWriter gives me when I use them to convert to XML schema(only the portion I have problems with):

        <xsd:element name="OtherServer">
               <xsd:complexType>
                       <xsd:choice>
                               <xsd:element ref="URL"/>
                               <xsd:element ref="MSISDN"/>
                               <xsd:sequence>
                                       <xsd:element ref="URL"/>
                                       <xsd:element ref="MSISDN"/>
                               </xsd:sequence>
                       </xsd:choice>
               </xsd:complexType>
       </xsd:element>
      <xsd:element name="MSISDN" type="xsd:string"/>
      <xsd:element name="URL" type="xsd:string"/>

This sounds simple enough. However, in actual usage, this fails. My test code is like the following:

SAXBuilder xmlParser = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);
xmlParser.setFeature("http://apache.org/xml/features/validation/schema", true);
xmlParser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", "WV-VERDISC.xsd");
// xmlStr holds the XML data
StringReader rdr = new StringReader(xmlStr);
Document xmlDoc = xmlParser.build(rdr);

Sample XML(only the portion I have problems with):

   <OtherServer>
      <URL>http://www.blah.com/XMLTest</URL>
      <MSISDN>+1234567890</MSISDN>
   </OtherServer>
   <OtherServer>
      <URL>http://www.blah.com/XMLTest</URL>
   </OtherServer>
   <OtherServer>
      <MSISDN>+1234567890</MSISDN>
   </OtherServer>

So, looking at the DTD, the above 3 OtherServer XML elements are all valid. But using the converted XML schema, during validation, if OtherServer has both URL and MSISDN child elements, the parser says it is NOT valid. After many trials and errors, I am wondering if the DTD definition can be converted to XML schema at all. I tried group declarations and it won't work. I am using  JDOM 1.0 and Xerces 2.6.2. Any clues is greatly appreciated! TIA!
Avatar of DitmarBehn
DitmarBehn

Hi,

it seems as if the xsd:choice is not the correct way of handling such a situation. Choice is really an either/xor without the possibility of having multiple/all child nodes available.

Try it with the following xsd snippet and see if it fits your need:

<xs:sequence>
  <xs:element name="OtherServer" maxOccurs="unbounded">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="URL" type="xs:anyURI" minOccurs="0"/>
        <xs:element name="MSISDN" type="xs:int" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:sequence>

Regards, Ditmar
Hmmm...I don't see anything at all wrong with the OP's schema.  It validated fine for me, but I'm not using JDOM or Xerces.  Essentially is says a choice of either A, or B or both.    

The problem with using an xs:sequence is that it allows this:

<OtherServer>
</OtherServer>

Which is not allowed with the DTD.  If an empty OtherServer is allowed, then this works.

I'm wondering, too, if there is a need to constrain the order of the URL and MSISDN elements.  If you don't want to constrain the order of the child elements of OtherServer, then use the xs:all declaration:

  <xs:element name="OtherServer" maxOccurs="unbounded">
    <xs:complexType>
      <xs:all>
        <xs:element name="URL" type="xs:anyURI" minOccurs="0"/>
        <xs:element name="MSISDN" type="xs:int" minOccurs="0"/>
      </xs:sequence>
    </xs:all>
  </xs:element>

But again, this allows an empty OtherServer element.

Regards,
Mike Sharp
Avatar of Andyfungkl

ASKER

Hi Mike,

Which XML parser are you using just curious? I am subjecting it is a problem with JDOM + Xerces too, since I don't see anything wrong as well.

Regards,
 Andy
I did the validation using XML Spy, Ver 5, Release 4.  While I have found obscure validation bugs in XML Spy, this seems like a pretty common content model to me.  In fact, it's weird that JDOM and Xerces would have problems with it.  I wonder if something else might be the problem...

Regards,
Mike Sharp
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well,
If it helps you to make a recommendation...
I am convinced that the question was answered correctly and completely :-)
I don't care about the points, but I would hate the solution being removed from the archive.

To quote my parser on the DTD:
Markup Error (0004) on line 2 in file Markup Stream:
A content model must not be ambiguous.
For the declared element "OtherServer", the element "URL" is ambiguous
in the content model.

Gertone
I agree.  Points to Gertone.

Regards,
Mike Sharp
Sorry for not checking experts-exchange for awhile, anyway Gertone really nails the problem. Thanks Gertone and rdcpro both of you!