Kon*_*ski 5 .net xml xsd xsd.exe .net-3.5
我想做的事:
我正在尝试为现有的 XML 文件生成 XSD 文件。
我正在使用该xsd.exe工具(随 Visual Studio 一起提供)。
XML 文件中的某些元素是命名空间限定的。在某些情况下,本地名称是相同的,如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
我得到了什么:
打电话xsd.exe myfile.xml给我一个错误:Cannot add a column named 'icons': a nested table with the same name already belongs to this DataTable.
好的,但这就是命名空间的用途,不是吗?像这样解决歧义。如果没有命名空间,我只会调用元素foo_icons而不是玩弄前缀。
我试过的:
我尝试寻找一种配置方法,xsd.exe以便将名称空间考虑在内,但xsd /?我的 google 查询都没有透露任何答案。该/n[amespace]:参数不允许指定多个命名空间。
我已经阅读了在 XML 模式中使用命名空间,但我觉得自己不太聪明。
我是否必须创建单独的 XSD 文件并将它们嵌入到彼此中?它也不涉及xsd.exe为此目的使用。
我真的不太熟悉XSD,所以我可能误解了整个过程的一些基本概念。如果有人能指出我正确的方向,我将不胜感激。
编辑 1 - 遵循 Marc Gravell 的建议:
我试过了,但我也不得不重命名出现在 XML 的不同部分(在不同的父节点下)的(带前缀的)元素,这是xsd不允许的。我不得不重新命名为elementOne,elementTwo等我要回去手动重新命名。但是我得到的 XSD 无论如何都不起作用。
标题是:
<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">
Run Code Online (Sandbox Code Playgroud)
当我尝试使用它验证文件时,出现错误:
Prefix 'app2' cannot be mapped to namespace name reserved for "xml" or "xmlns".
那么,它是出于什么目的而xsd.exe产生的呢?应该怎么修?
您使用 xsd.exe 为您提供 XML 架构的方式...它实际上试图提供一个数据集;而.NET 上的功能确实非常有限。
我将使用 .NET 上提供的另一个选项:使用此处记录的 .NET API 构建脚本。
至少对于您的 XML 片段来说,它会起作用;我已经尝试过了,它创建了一个有效的 XmlSchemaSet。下面是我使用我正在使用的工具运行的测试,它依赖于相同的 API(还有一些额外的功能,否则您将不得不手动进行一些小修复)。
修复了 XML(添加了 foo 前缀缺少的命名空间声明):
<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>
Run Code Online (Sandbox Code Playgroud)
顶级架构(没有目标命名空间,与您的图像元素匹配):
<?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>
Run Code Online (Sandbox Code Playgroud)
foo 命名空间的架构:
<?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>
Run Code Online (Sandbox Code Playgroud)
生成的 XML 架构文件非常适合验证您的 XML。