Generating an XSD file with xsd.exe tool from an XML file with multiple namespaces

前提是你 提交于 2019-12-03 16:21:15

The way you're using xsd.exe to give you an XML Schema... it actually tries to come up with a DataSet; and that on .NET is very limited indeed.

I would use another option, available on .NET: to build a script using the .NET API documented here.

At least for your XML fragment, it'll work; I've tried it and it creates a valid XmlSchemaSet. Below is the test that I ran with the tool that I am using, which relies on the same API (with some additional bells and whistles that otherwise you'll have to do some minor fixes by hand).

Fixed XML (added the 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>

The top level schema (no target namespace, matches your images 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>

The schema for the 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 to validate your XML.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!