HighTechTalks DotNet Forums  

Problems with XmlSchema name mangling XmlSchemaSet compilation

Dotnet XML microsoft.public.dotnet.xml


Discuss Problems with XmlSchema name mangling XmlSchemaSet compilation in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Ryan
 
Posts: n/a

Default Problems with XmlSchema name mangling XmlSchemaSet compilation - 11-30-2007 , 10:38 AM






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-xsdrimarykey" />

to be

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

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-xsdrimarykey' 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="q1rimarykey" />
</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







Reply With Quote
  #2  
Old   
Ryan
 
Posts: n/a

Default Re: Problems with XmlSchema name mangling XmlSchemaSet compilation - 11-30-2007 , 01:22 PM






Took a break then came back and quickly realized my stupid mistake...

Quote:
element.SchemaType = new XmlQualifiedName("primarykey", "s-xsd");
should have been

Quote:
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" <parleer (AT) nospam (DOT) nospam> wrote

Quote:
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-xsdrimarykey" /

to be

xsd:element minOccurs="1" name="AccountId" xmlns:q1="s-xsd"
type="q1rimarykey" /

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-xsdrimarykey' 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="q1rimarykey" /
/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









Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.