Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML for website: Schema or DTD? 1

Status
Not open for further replies.

fuli42

Technical User
Jul 9, 2002
74
HU
Hello!

I am building a site using XML and XSLT to generate XHTML code. The question is should I use DTD instead of XML Schema for validation?
XML Schema is more advanced, but as far as i know XSLT parser can't use them. Is this a problem for the transoformation?

Thank you all,

Balint

 
You generally need to use a schema when you're validating XML that someone else supplies you (like in SOAP or in an data import program). You shouldn't need it to *output* xhtml files.

Chip H.
 
Thank you!

I'll use DTD, since the data in the XML file will be entered by humans (not IT professionals), and I have to make sure they enter it correctly.

Thanks again

Bálint
 
OK, different situation then. I thought that you were outputting XML. If you're *accepting* XML, then you must indeed validate it. And the best way to do that is through a schema (XSD file), and NOT a DTD. The XSD file allows you to apply many more controls over the contents of an XML file.

For example: A DTD looks like this:
Code:
<!ELEMENT Root (FillCount, Priority, StartDate)>
<!ELEMENT FillCount (#PCDATA)>
<!ATTLIST FillCount
	FillCount NMTOKEN #FIXED &quot;number&quot;
>
<!ELEMENT Priority (#PCDATA)>
<!ELEMENT StartDate (#PCDATA)>
which says you expect a Number for the FillCount element, a String for Priority, and a String for StartDate. According to this, this is a valid document:
Code:
<!DOCTYPE Root SYSTEM &quot;C:\WINNT\Temp\TestDTD.dtd&quot;>
<Root>
	<FillCount FillCount=&quot;number&quot;>-3</FillCount>
	<Priority>blah blah blah</Priority>
	<StartDate>kitty cat</StartDate>
</Root>
Obviously, a negative value for FillCount is wrong, &quot;blah blah blah&quot; is not a valid Priority, and &quot;kitty cat&quot; is not a valid StartDate.

However, using an XSD, you can restrict what people put in the elements to valid values. For an XSD of:
Code:
<xs:schema xmlns:xs=&quot;[URL unfurl="true"]http://www.w3.org/2001/XMLSchema&quot;[/URL] elementFormDefault=&quot;qualified&quot; attributeFormDefault=&quot;unqualified&quot;>
  <xs:element name=&quot;Root&quot;>
    <xs:complexType>
      <xs:sequence>
        <xs:element name=&quot;FillCount&quot;>
          <xs:simpleType>
            <xs:restriction base=&quot;xs:int&quot;>
              <xs:minInclusive value=&quot;0&quot;/>
              <xs:maxInclusive value=&quot;9999&quot;/>
              <xs:totalDigits value=&quot;4&quot;/>
              <xs:fractionDigits value=&quot;0&quot;/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name=&quot;Priority&quot;>
          <xs:simpleType>
            <xs:restriction base=&quot;xs:string&quot;>
              <xs:enumeration value=&quot;Low&quot;/>
              <xs:enumeration value=&quot;Medium&quot;/>
              <xs:enumeration value=&quot;High&quot;/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name=&quot;StartDate&quot; type=&quot;xs:date&quot;/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
will prevent them from entering &quot;unusual&quot; values. The FillCount must have values between 0 and 9999 (inclusive), the Priority can only be Low Medium or High, and the StartDate must look like YYYY-MM-DD. All these fields are required (you could make them optional if you wanted), and must be present in this order.

Not to make fun of my customers, but I've seen them send me some amazing (as in dumb) things, and the XSD is my first line of defense against bad data.

Chip H.
 
Thanks Chiph!

I wasn't sure if XSD worked with XSL. I've read somewhere that some functions wouldn't work in XSLT if you're using XSD (IDs for example).
If I'm doing XSL transorfmation to output XHTML can I still use XSD? I have a book that explains both DTD, and XSD so neither of them should be too difficult ;)

Thanks,

Balint
 
The two technologies (XSD and XSLT) do not interact at all, other than the fact that both are in the XML &quot;family&quot;.

Like I've been saying, XSD is used to validate an XML document, to ensure you've got good data. XSLT is used to transform that document into another format.

Forget all about DTD's. They're old technology from the dawn of XML, and doesn't meet the needs of a modern app. Think: &quot;deprecated&quot;.

Chip H.
 
Thanks!

I wrote the validation file using xsd, though it needed a couple of turns with the SQC before I got it all right...

Balint
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top