如何向 xsd 中的 ComplexType 元素添加自定义属性?

How to add custom Attributes to ComplexType Elements in xsd?

我正在从提供给我的 XSD 生成一个 c# class 文件。如果只使用原始原生 XSD,我可以毫无问题地做到这一点。但是,我想通过添加自己的命名空间来修改 XSD,并且我想通过生成器提供新修改的 XSD 以生成一个 class 文件,其中包含 class 提供了我的定义,以及我添加的定义。

所以我的理解是我需要添加一个命名空间以及该命名空间下的新属性。我确实有权访问并能够修改原始 XSD 或添加我自己的 xsd.

我看了这个 post 很相似

我的尝试示例如下。在我的尝试中,我试图将属性 "Humidity" 添加到 "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>

目前此 returns 'mns:humidity' 元素在此上下文中不受支持。

任何人都可以解释我如何将其插入复杂类型吗?

我还尝试将 mns:Humidity 放在与 deviceInfo

相同的标签中
<xs:element name="DeviceInfo mns:Humidity="0">

通过 class 生成器发送它会产生两个单独的 "DeviceInfo" 实例,其中第二个实例附加了 1。

如何将它插入到具有命名空间的 complexType 中?

在main.xsd中,在DeviceInfo的声明中,添加

<xs:attribute ref="mns:Humidity"/>

加上 "mns" 的命名空间声明和带有 schemaLocation="test.xsd".

的该命名空间的 xs:import 声明

test.xsd 中包含单个声明

<xs:attribute name="Humidity" type="xs:float"/>

您尝试解决此问题表明您正在使用试错法,而不是实际阅读 XSD 中的结构的实际含义。这不是一个好的前进方向。您似乎对这些概念相当困惑,例如,架构永远不应包含 xsi:schemaLocation 属性。