![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Right now, in order to validate my xml file against xsd, I include the following line in the xml file: xmlns="urn:mySchemaName". |
|
Is it possible to link xml to xsd without specifying the schema inside the xml file? |
#3
| |||
| |||
|
|
Eve wrote: Right now, in order to validate my xml file against xsd, I include the following line in the xml file: xmlns="urn:mySchemaName". xmlns="urn:mySchemaName" is a namespace declaration, not an "instruction" to validate against a schema. You should use xsi:schemaLocation and/or xsi:noNamespaceSchemaLocation attributes if you want to provide a hint as to which schema(s) an XML instance document should be validated against. Is it possible to link xml to xsd without specifying the schema inside the xml file? Yes, sure, but how you do that depends on the API of the validating parser you use. With the .NET framework 2.0 or 3.0 you simply use XmlReaderSettings and add schemas to its Schemas property e.g. C# pseudo code XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.ValidationType = ValidationType.Schema; readerSettings.ValidationEventHandler += delegate (object sender, ValdiationEventArgs vargs) { Console.WriteLine("{0}: {1}", vargs.Severity, vargs.Message); }; readerSettings.Schemas.Add(null, "schema1.xsd"); using (XmlReader xmlReader = XmlReader.Create("file.xml", readerSettings)) { while (xmlReader.Read()) {} } -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ |
#4
| |||
| |||
|
|
Yes, I am using XmlReaderSettings (I realize xmlns="urn:mySchemaName" is just a namespace, not an instruction). I was wondering if it's possible to not specify a namespace in the xml doc. The reason I want to do that is I don't know what xsd I will be using up in front and I want to set it on the fly programmatically(withouth touching the original xml doc). |
#5
| |||
| |||
|
|
Eve wrote: Yes, I am using XmlReaderSettings (I realize xmlns="urn:mySchemaName" is just a namespace, not an instruction). I was wondering if it's possible to not specify a namespace in the xml doc. The reason I want to do that is I don't know what xsd I will be using up in front and I want to set it on the fly programmatically(withouth touching the original xml doc). The Add method on XmlSchemaSet http://msdn2.microsoft.com/en-us/library/1hh8b082.aspx> takes as the first argument the targetNamespace and allows the argument to be null (C#) respectively Nothing (VB.NET) to have the parser use the targetNamespace as specified in the schema itself. That way you can validate against any schema without your code to validate needing to know the schema in advance. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ |
#6
| |||
| |||
|
|
I am using null as the targetNamespace (please look at the code below), but I still need to include xmlns="mySchemaHere" in the xml that I'm validating. If I don't do that, I get messages saying something like "schema not defined for X element". Is it possible to omit the xmlns in my xml doc and still somehow programatically link the doc to the xsd? |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |