471,092 Members | 1,030 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

Problems with XmlSchema name mangling XmlSchemaSet compilation

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
1 3858
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.

Similar topics

reply views Thread by Michiel | last post: by
1 post views Thread by Tim Slattery | last post: by
1 post views Thread by Linus | last post: by
2 posts views Thread by Alex Shirshov | last post: by
4 posts views Thread by Igor Koretsky | last post: by
7 posts views Thread by seema | last post: by
reply views Thread by Default User | last post: by
14 posts views Thread by Megalo | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.