从具有多个命名空间的XML文件生成与xsd.exe工具XSD文件

问题描述:

我想做什么:

我试图生成一个现有的XML文件XSD文件。

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

我使用了 XSD.EXE 工具(随Visual Studio中)。

I'm using the xsd.exe tool (shipped with Visual Studio).

某些XML文件中的元素都是合格的命名空间。在某些情况下,本地名称是相同的,只是如下所示:

Some of the elements in the XML file are namespace-qualified. In some cases local names are the same, just as 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>

什么我得到:

调用 XSD.EXE将myfile.xml 给我一个错误:无法添加一个名为'图标列:具有相同名称的嵌套表已经属于此数据表。

确定,但是这就是命名空间是什么,不是吗?解决歧义之类的。如果没有命名空间,我只是调用的 foo_icons ,而不是用prefixes玩弄的元素。

OK but that's what the namespace is for, isn't it? Resolving ambiguity like that. If there was no namespaces, I would just call the element foo_icons instead of toying with prefixes.

我的尝试:

我试图寻找一种方法来配置 XSD.EXE ,以使它能命名空间的考虑,但也 XSD /?和我的谷歌查询发现任何回答。该 / N [amespace]:参数不允许指定多个命名空间。

I tried looking for a way to configure xsd.exe so that it takes namespaces into consideration, but neither xsd /? nor my google queries revealed any answer. The /n[amespace]: parameter does not allow for specifying multiple namespaces.

我读过与命名空间中的XML Schema工作但我不觉得更为明智。

I've read Working with Namespaces in XML Schema but I don't feel much wiser.

我一定要创建单独的XSD文件,并嵌入到海誓山盟?它不涉及使用 XSD.EXE 为了这个目的,无论是。

Do I have to create separate XSD files and embed them in eachother? It doesn't deal with using xsd.exe for this purpose, either.

我真的不是太熟悉XSD,所以我可能误解对整个过程的一些基本概念。我倒是AP preciate如果有人可以点我在正确的方向。

I'm really not too familiar with XSD, so I am probably misunderstanding some essential concepts about the whole process. I'd appreciate if someone could point me in the right direction.

修改1 - 以下马克Gravell的建议:

我试过了,但我也不得不重命名(prefixed),它出现在XML的不同部分元素(不同的父节点下)的 XSD 不会允许的。我不得不将其重命名为 elementOne elementTwo 等我要回去手动将其重命名。但我得到了XSD不反正工作。

I tried that, but I also had to rename a (prefixed) element which appeared in different sections of the XML (under different parent nodes) as xsd would not allow that. I had to rename it to elementOne, elementTwo etc. I was going to rename it back manually. But the XSD I got does not work anyway.

标题是:

<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'm trying to validate a file with it, I'm getting an error:

preFIX'APP2'不能映射到命名空间名称保留XML或的xmlns。

所以,用什么样的目的有 XSD.EXE 生成会这样?应该如何修复?

So, with what purpose has xsd.exe generated it like that? How should it be fixed?

您使用XSD.EXE给你一个XML架构的方式......它实际上试图拿出一个数据集;和.NET的非常实在有限。

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.

我会用另一种选择,可以在.NET:使用.NET API文档的这里

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

至少对你的XML片段,它会工作;我已经尝试过了,它会创建一个有效的XmlSchemaSet。下面是我跑了,我现在用的工具,它依赖于相同的API(有一些额外的花俏,否则你将不得不手工做一些小的修改)的测试。

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).

固定的XML(添加缺少的命名空间声明foo的preFIX):

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>

有关foo的命名空间的架构:

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>

生成的XML架构文件是很好的验证XML。

The generated XML Schema files are good to validate your XML.