views:

1238

answers:

1

Hello everyone,

I want to do XML validation to validate whether an input XML file conforming to a schema file (.xsd). My question is when do we need to specify the target namespace parameter and what is the function of the target namespace parameter?

I got this question from the following MSDN pages,

http://msdn.microsoft.com/en-us/library/1hh8b082.aspx

http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschemaset.aspx

More specifically, I am referring to the 1st parameter of Add method of XmlSchemaSet class.

thanks in advance, George

EDIT1: My current confusion is,

  1. In the XML schema file, there is a target namespace attribute at top, and all elements defined in this XML schema file are defined in the target namespace;
  2. In the XML document file to check against scheme, the XML document may use elements defined in the "target namespace" of the XML schema file, and may also use elements from other namespaces other than the "target namespace" defined in the XML schema file;
  3. Question is, should we just add the "target namespace" to the Add method of XmlSchemaSet class, or add all namespaces which the XML document will use to the Add method of XmlSchemaSet class?
  4. Another question is, whether we could define multiple "target namespace" in the XML schema file? If yes, how?
+2  A: 

You'd need to use that parameter if you were using namespaces in your XML document - e.g. if some of you tags were of the form <xx:TagName>. Otherwise you can just pass in an empty string ("").

dommer
To add to your answer, you need to pass in Null/Nothing as the targetNamespace if you want to XmlSchemaSet to use the namespace defined in the XmlSchema. This allows you to dispense with explicitly passing the namespace in the Add method. +1
Cerebrus
@dommer, "using namespaces in your document ", you mean using namespace in the XML docuemnt or the schema document?
George2
@Cerebrus, "use the namespace defined in the XmlSchema", what means namespace defined in the XML Schame file or something else? Could you illustrate you points by the sample here (about the books.xml and books.xsd sampl)?http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschemaset.aspxe
George2
In the XML document (edited post to make clear).
dommer
@George: You guessed right... It means the namespace defined in the Xml schema file.
Cerebrus
@dommer, the XML document to check may have various namespaces used. But seems I could only assign one namespace from method XmlSchemaSet.Add? Could you explain the relationship between the namespace assigned by XmlSchemaSet.Add method and the namespaces used by the XML document to be checked?
George2
@Cerebrus, in the sample of books.xsd, there is statement like -- targetNamespace="http://www.contoso.com/books", does it mean the schema definition content in books.xsd are all belong to namespace http://www.contoso.com/books?
George2
@George. You can assign multiple namespaces by calling XmlSchemaSet.Add multiple times.
dommer
@dommer, in the sample of books.xsd, there is statement like -- targetNamespace="http://www.contoso.com/books";, does it mean the schema definition content in books.xsd are all belong to namespace http://www.contoso.com/books?, like price element, ISBN element?
George2
@dommer and @Cerebrus, I have editted my original question, and please refer to the EDIT1 section for my question item 3 and item 4. Any ideas for 3/4?
George2