ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> tclxml-users
tclxml-users
Re: [Tclxml-users] TCLXML question
by Steve Ball other posts by this author
May 24 2006 5:10PM messages near this date
[Tclxml-users] TCLXML question | [Tclxml-users] Bug in TclDOM 3.1
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
Steve.Ball@[...].au  +--------------------------- 
+---------------------
Ph. +61 2 6242 4099   |   Mobile (0413) 594 462   | Fax +61 2 6242 4099



-------------------------------------------------------
All the advantages of Linux Managed Hosting--Without the Cost and Risk!
Fully trained technicians. The highest number of Red Hat certifications in
the hosting industry. Fanatical Support. Click to learn more
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642
_______________________________________________
Tclxml-users mailing list
Tclxml-users@[...].net
https://lists.sourceforge.net/lists/listinfo/tclxml-users
Thread:
Jennifer M
Steve Ball

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved