【发布时间】:2019-05-09 00:53:10
【问题描述】:
我正在从提供给我的 XSD 生成一个 c# 类文件。如果只使用原始的本机 XSD,我可以毫无问题地做到这一点。但是,我想通过添加我自己的命名空间来修改 XSD,并且我想通过生成器提供新修改的 XSD 以生成一个类文件,其中包含我提供的类定义以及我添加的定义。
所以我的理解是我需要添加一个命名空间以及该命名空间下的新属性。我确实有权修改原始 XSD 或添加我自己的 xsd。
我看了这篇类似Extend XSD with custom attributes?的帖子
我的尝试示例如下。在我的尝试中,我试图将属性“湿度”添加到“mns”命名空间下的 DeviceInfo 元素。这当前显示“在此上下文中不支持'mns:attribute'元素”的错误。
test.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://MyNamespace.com"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="DeviceInfo">
<xs:complexType>
<xs:attribute name="Humidity" type="xs:float" />
</xs:complexType>
</xs:element>
</xs:schema>
main.xsd
<xs:schema targetNamespace="http://www.CIP4.org/JDFSchema_2_0"
xmlns="http://www.CIP4.org/JDFSchema_2_0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mns="http://MyNamespace.com"
xsi:schemaLocation="http://MyNamespace.com ./test.xsd"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="DeviceInfo">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Activity"/>
<xs:element maxOccurs="2" minOccurs="0" ref="FileSpec"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="JobPhase"/>
</xs:sequence>
<xs:attribute name="CounterUnit" type="NMTOKEN" use="optional"/>
<mns:Humidity value="0"/>
</xs:schema>
目前,此上下文不支持返回 'mns:humidity' 元素。
谁能解释我如何将它插入到复杂类型中?
我也尝试将 mns:Humidity 放在与 deviceInfo 相同的标签中
<xs:element name="DeviceInfo mns:Humidity="0">
通过类生成器发送它会产生两个单独的“DeviceInfo”实例,其中第二个附加了一个 1。
如何将其插入到带有命名空间的 complexType 中?
【问题讨论】: