|
From: Steve B. <Ste...@ex...> - 2006-05-24 23:39:53
|
Hi Jennifer,
You wrote:
> I am new to the TCLXML package and I am not sure if I can use
> this to also validate an xml document. I am going be receiving an XML
> documents that I will need to validate against a schema and once it is
> known to be valid I can parse it. I did see there is a validate
> command
> that I can use but the explanation is a bit vague and I did not see
> any
> where I could enter the name of the schema file it would use.
> Could you
> shed some light on this for me. If I can not use the TCLXML
> package to
> validate would I use the TCLDOM package for that?
Hmmm... just checked the TclDOM manual and it looks like I haven't
documented the validation interface properly(or at all ;-( ). Please
submit a documentation bug report on SourceForge.
Firstly, validation is only available when using the libxml2
implementation. The pure-Tcl implementation of TclXML does not have
any kind of validation available.
Secondly, validation must be performed either during parsing of the
XML document or after the document has been parsed (aka "posteriori
validation"). There is no concept of validating the document and
subsequently parsing it.
TclXML/libxml2 and TclDOM/libxml2 both offer validation services.
With TclXML, validation may be performed during parsing of the
document. With TclDOM, validation must be performed after the
document has been parsed (ie. "posteriori" or "after-the-fact").
When performing DTD validation, the instance XML document declares
the location of the DTD in its Document Type Declaration. TclXML is
able to read the declaration when parsing the document and can then
go and fetch the (external) DTD (DTDs can also be internal; ie.
embedded in the instance XML document). With TclDOM, you can specify
the DTD subset separately.
When performing XSD validation, the schema document is usually
specified separately from the instance XML document. RELAX NG
validation always specifies the schema document separately. In these
cases the schema document is itself an XML document. Your script
must first load and parse the schema XML document, and then use it to
validate the instance XML document.
For an example of how to perform validation of a document, look at
the tkxmllint application. The script for this tool is in the
examples directory of the TclDOM distribution. In the meantime,
here's a quick example:
set doc [dom::parse $xml] ;# this is the instance XML document
if {[catch {$doc dtd validate} msg]} {
puts stderr "document is not valid due to \"$msg\""
} else {
puts stderr "document is valid"
}
set schemadoc [dom::parse $schemaxml] ;# this is the schema XML document
if {[catch {$schemadoc schema compile} msg]} {
puts stderr "unable to compile schema doc due to \"$msg\""
exit 1
}
if {[catch {$schemadoc schema validate $doc} msg]} {
puts stderr "document is not schema-valid due to \"$msg\""
} else {
puts stderr "document is schema-valid"
}
Hope that helps,
Steve Ball
---
Steve Ball | XSLT Standard Library | Training & Seminars
Explain | Web Tcl Complete | XML XSL Schemas
http://www.explain.com.au/ | TclXML TclDOM | Tcl, Web
Development
Ste...@ex... +---------------------------
+---------------------
Ph. +61 2 6242 4099 | Mobile (0413) 594 462 | Fax +61 2 6242 4099
|