Creating an XSD File Using the xsd.exe Tool from an XML File with Multiple Namespaces

What I want to do:

I am trying to create an XSD file for an existing XML file.

I am using the xsd.exe tool (comes with Visual Studio).

Some elements in the XML file have access to the namespace. In some cases, the local names are the same, as shown below:

 <images> <icons> <icon url="http://www.icons.com/logo.png"/> </icons> <foo:icons> <foo:foo_icon url="http://www.foo.org/pic.ico"/> </foo:icons> </images> 

What I get:

Calling xsd.exe myfile.xml gives me an error: Cannot add a column named 'icons': a nested table with the same name already belongs to this DataTable.

OK, but what is the namespace used for, right? The solution to this ambiguity. If there were no namespaces, I would just call the foo_icons element instead of playing with prefixes.

What I tried:

I tried to find a way to configure xsd.exe to take namespaces into account, but not xsd /? , neither my Google queries found any answer. The /n[amespace]: Does not allow multiple namespaces to be specified.

I read Working with namespaces in an XML Schema , but I don't feel much wiser.

Do I need to create separate XSD files and embed them in eachother? xsd.exe also not used for this purpose.

I'm really not very familiar with XSD, so I probably misunderstood some of the essential concepts of the whole process. I would appreciate it if someone could point me in the right direction.

Change 1 - the following sentence of Mark Gravel:

I tried this, but I also had to rename the element (prefix) that appeared in different sections of XML (under different parent nodes), since xsd did not allow this. I had to rename it to elementOne , elementTwo , etc. I was going to rename it manually. But the XSD I received does not work anyway.

Title:

 <xs:schema id="NewDataSet" targetNamespace="http://www.foo.com/bar" xmlns:mstns="http://www.foo.com/bar" xmlns="http://www.foo.com/bar" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:app1="http://www.foo.com/bar/extensions" xmlns:app2="http://www.w3.org/XML/1998/namespace"> 

And when I try to check the file with it, I get an error message:

Prefix 'app2' cannot be mapped to namespace name reserved for "xml" or "xmlns".

So, for what purpose xsd.exe generate it like that? How should this be fixed?

+4
source share
1 answer

How do you use xsd.exe to give you an XML schema ... it is actually trying to create a DataSet; and that on .NET is very limited.

I would like to use another option available in .NET: to create a script using an API document here .

At least for your XML fragment, it will work; I tried this and it creates a valid XmlSchemaSet. Below is the test that I used with the tool that I use, which relies on the same API (with some additional bells and whistles that otherwise you will have to make small corrections manually).

Fixed XML (added missing namespace declaration for foo prefix):

 <images> <icons> <icon url="http://www.icons.com/logo.png"/> </icons> <foo:icons xmlns:foo="urn:tempuri-org:test"> <foo:foo_icon url="http://www.foo.org/pic.ico"/> </foo:icons> </images> 

Top-level scheme (without target namespace, matches your image element):

 <?xml version="1.0" encoding="utf-8"?> <!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)--> <xsd:schema xmlns:foo="urn:tempuri-org:test" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:import schemaLocation="XSDMultipleNamespaces1.xsd" namespace="urn:tempuri-org:test" /> <xsd:element name="images"> <xsd:complexType> <xsd:sequence> <xsd:element name="icons"> <xsd:complexType> <xsd:sequence> <xsd:element name="icon"> <xsd:complexType> <xsd:attribute name="url" type="xsd:string" use="required" /> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element ref="foo:icons" /> </xsd:sequence> </xsd:complexType> 

Schema for foo namespace:

 <?xml version="1.0" encoding="utf-8"?> <!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)--> <xsd:schema xmlns="urn:tempuri-org:test" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:tempuri-org:test" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="icons"> <xsd:complexType> <xsd:sequence> <xsd:element name="foo_icon"> <xsd:complexType> <xsd:attribute name="url" type="xsd:string" use="required" /> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> 

The generated XML schema files are good for validating your XML.

+3
source

Source: https://habr.com/ru/post/1382251/


All Articles