By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,564 Members | 810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,564 IT Pros & Developers. It's quick & easy.

Problems with XmlSchema name mangling XmlSchemaSet compilation

P: n/a
Hello Xml Gurus,

I'm trying to build an XML schema in memory using the System.Xml.XmlSchema
namespace objects, validate it, and then write it to a file. The problem
I'm facing is that XmlSchema.Write() is changing

<xsd:element minOccurs="1" name="AccountId" type="s-xsd:primarykey" />

to be

<xsd:element minOccurs="1" name="AccountId" xmlns:q1="s-xsd"
type="q1:primarykey" />

and I am also getting the following error messages by XmlSchemaSet.Compile()

Namespace 's-xsd' is not available to be referenced in this schema.
Type 's-xsd:primarykey' is not declared.
--------------------------------------------------------------------------------
My code basically looks like this
--------------------------------------------------------------------------------

XmlSchema schema = new XmlSchema();
schema.AttributeFormDefault = XmlSchemaForm.Unqualified;
schema.ElementFormDefault = XmlSchemaForm.Qualified;
schema.TargetNamespace = "http://www.baseUri.com/sxml/1.0";
schema.Namespaces.Add("xsd", "http://www.w3.org/2001/XMLSchema");
schema.Namespaces.Add("s-xml", "http://www.baseUri.com/sxml/1.0");
schema.Namespaces.Add("s-xsd", "http://www.baseUri.com/sxsd/1.0");

// <xsd:import...
XmlSchemaImport import = new XmlSchemaImport();
schema.Includes.Add(import);
import.Namespace = "http://www.baseUri.com/sxsd/1.0";
import.SchemaLocation = "s-xsd.xsd";

// <xsd:complexType...
XmlSchemaComplex complexType = new XmlSchemaComplexType();
schema.Items.Add(complexType);
complexType.Name = "Account";

// <xsd:sequence>
XmlSchemaSequence sequence = new XmlSchemaSequence();
complexType.Particle = sequence;

// <xsd:element...
XmlSchemaElement element = new XmlSchemaElement();
element.Name = "AccountId";
element.MinOccurs = 1;
element.MaxOccurs = 1;
element.SchemaType = new XmlQualifiedName("primarykey", "s-xsd");

// Compile schema and validate
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new
ValidationEventHandler(schemaSet_ValidationEventHa ndler);
schemaSet.Add(schema);
schemaSet.Compile();

// Write compiled schemas to output
foreach (XmlSchema compiledSchema in schemaSet.Schemas())
{
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("s-xml", "http://www.baseUri.com/sxml/1.0");
nsmgr.AddNamespace("s-xsd", "http://www.baseUri.com/sxsd/1.0");
compiledSchema.Write(Console.Out);
}

--------------------------------------------------------------------------------
And the output looks like this:

(Note that there are 2 schemas. The first is the one I
created through code, and the second is the one
referenced by the first)
--------------------------------------------------------------------------------

<?xml version="1.0" encoding="IBM437"?>
<xsd:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://www.baseUri.com/sxml/1.0"
xmlns:s-xml="http://www.baseUri.com/sxml/1.0"
xmlns:s-xsd="http://www.baseUri.com/sxsd/1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import schemaLocation="s-xsd.xsd"
namespace="http://www.baseUri.com/sxsd/1.0" />
<xsd:complexType name="queue">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="1" name="queueid"
xmlns:q1="s-xsd" type="q1:primarykey" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

<?xml version="1.0" encoding="IBM437"?>
<xsd:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://www.baseUri.com/sxsd/1.0"
xmlns:s-xsd="http://www.baseUri.com/sxsd/1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="primarykey">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:schema>

Can someone please tell me what I'm doing wrong? Is the
System.Xml.XmlSchema more pain than it's worth?

Thanks,

Ryan Parlee


Nov 30 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Took a break then came back and quickly realized my stupid mistake...
element.SchemaType = new XmlQualifiedName("primarykey", "s-xsd");
should have been
element.SchemaType = new XmlQualifiedName("primarykey",
"http://www.baseUri.com/sxsd/1.0");
That took care of both the aliasing (which now makes sense because it had to
use local scope to override a global namespace naming collision) and the
errors output from XmlSchemaSet.Compile()
-Ryan

"Ryan" <pa*****@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hello Xml Gurus,

I'm trying to build an XML schema in memory using the System.Xml.XmlSchema
namespace objects, validate it, and then write it to a file. The problem
I'm facing is that XmlSchema.Write() is changing

<xsd:element minOccurs="1" name="AccountId" type="s-xsd:primarykey" />

to be

<xsd:element minOccurs="1" name="AccountId" xmlns:q1="s-xsd"
type="q1:primarykey" />

and I am also getting the following error messages by
XmlSchemaSet.Compile()

Namespace 's-xsd' is not available to be referenced in this schema.
Type 's-xsd:primarykey' is not declared.
--------------------------------------------------------------------------------
My code basically looks like this
--------------------------------------------------------------------------------

XmlSchema schema = new XmlSchema();
schema.AttributeFormDefault = XmlSchemaForm.Unqualified;
schema.ElementFormDefault = XmlSchemaForm.Qualified;
schema.TargetNamespace = "http://www.baseUri.com/sxml/1.0";
schema.Namespaces.Add("xsd", "http://www.w3.org/2001/XMLSchema");
schema.Namespaces.Add("s-xml", "http://www.baseUri.com/sxml/1.0");
schema.Namespaces.Add("s-xsd", "http://www.baseUri.com/sxsd/1.0");

// <xsd:import...
XmlSchemaImport import = new XmlSchemaImport();
schema.Includes.Add(import);
import.Namespace = "http://www.baseUri.com/sxsd/1.0";
import.SchemaLocation = "s-xsd.xsd";

// <xsd:complexType...
XmlSchemaComplex complexType = new XmlSchemaComplexType();
schema.Items.Add(complexType);
complexType.Name = "Account";

// <xsd:sequence>
XmlSchemaSequence sequence = new XmlSchemaSequence();
complexType.Particle = sequence;

// <xsd:element...
XmlSchemaElement element = new XmlSchemaElement();
element.Name = "AccountId";
element.MinOccurs = 1;
element.MaxOccurs = 1;
element.SchemaType = new XmlQualifiedName("primarykey", "s-xsd");

// Compile schema and validate
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new
ValidationEventHandler(schemaSet_ValidationEventHa ndler);
schemaSet.Add(schema);
schemaSet.Compile();

// Write compiled schemas to output
foreach (XmlSchema compiledSchema in schemaSet.Schemas())
{
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("s-xml", "http://www.baseUri.com/sxml/1.0");
nsmgr.AddNamespace("s-xsd", "http://www.baseUri.com/sxsd/1.0");
compiledSchema.Write(Console.Out);
}

--------------------------------------------------------------------------------
And the output looks like this:

(Note that there are 2 schemas. The first is the one I
created through code, and the second is the one
referenced by the first)
--------------------------------------------------------------------------------

<?xml version="1.0" encoding="IBM437"?>
<xsd:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://www.baseUri.com/sxml/1.0"
xmlns:s-xml="http://www.baseUri.com/sxml/1.0"
xmlns:s-xsd="http://www.baseUri.com/sxsd/1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import schemaLocation="s-xsd.xsd"
namespace="http://www.baseUri.com/sxsd/1.0" />
<xsd:complexType name="queue">
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="1" name="queueid"
xmlns:q1="s-xsd" type="q1:primarykey" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

<?xml version="1.0" encoding="IBM437"?>
<xsd:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://www.baseUri.com/sxsd/1.0"
xmlns:s-xsd="http://www.baseUri.com/sxsd/1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="primarykey">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:schema>

Can someone please tell me what I'm doing wrong? Is the
System.Xml.XmlSchema more pain than it's worth?

Thanks,

Ryan Parlee


Nov 30 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.