继承和实现的 .NET 类型的 XML 架构

发布于 2024-08-26 15:31:06 字数 1786 浏览 10 评论 0原文

请考虑以下三种 .NET 类型:我有一个接口、一个抽象类和一个具体类。我的问题是如何编写 XML 架构以包含来自接口和抽象类的属性。

public interface IStartable
{
    bool RequiresKey { get; set; }

    void Start(object key);
}

public abstract class Vehicle
{
    uint WheelCount { get; set; }
}

public class Car : Vehicle, IStartable
{
    public bool RequiresKey { get; set; }

    public string Make { get; set; }

    publilc string Model { get; set; }


    public Car() {}

    public void Start(object key)
    {
        // start car with key
    }
}

我不知道如何完成这个架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="cars"
    xmlns="cars"
    xmlns:c="cars">

    <!-- How do I get car to have vehicle's wheelcount
         AND IStartable's RequiresKey? -->
    <xs:element name="Car" type="c:Car" />
    <xs:complexType name="Car">
        <xs:complexContent>
            <xs:extension base="c:Vehicle">
                <xs:group ref=c:CarGroup" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:group name="CarGroup">
        <xs:sequence>
            <xs:element name="Make" type="xs:token" />
            <xs:element name="Model" type="xs:token" />
        </xs:sequence>
    </xs:group>

    <xs:complexType name="Vehicle">
        <xs:sequence>
            <xs:element name="WheelCount" type="xs:unsignedInt" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="IStartable">
        <xs:sequence>
            <xs:element name="RequiresKey" type="xs:boolean" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

Please consider the following three .NET types: I have an interface, an abstract class, and a concrete class. My question is how to write the XML Schema to include the properties from the interface and from the abstract class.

public interface IStartable
{
    bool RequiresKey { get; set; }

    void Start(object key);
}

public abstract class Vehicle
{
    uint WheelCount { get; set; }
}

public class Car : Vehicle, IStartable
{
    public bool RequiresKey { get; set; }

    public string Make { get; set; }

    publilc string Model { get; set; }


    public Car() {}

    public void Start(object key)
    {
        // start car with key
    }
}

I don't know how to complete this schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="cars"
    xmlns="cars"
    xmlns:c="cars">

    <!-- How do I get car to have vehicle's wheelcount
         AND IStartable's RequiresKey? -->
    <xs:element name="Car" type="c:Car" />
    <xs:complexType name="Car">
        <xs:complexContent>
            <xs:extension base="c:Vehicle">
                <xs:group ref=c:CarGroup" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:group name="CarGroup">
        <xs:sequence>
            <xs:element name="Make" type="xs:token" />
            <xs:element name="Model" type="xs:token" />
        </xs:sequence>
    </xs:group>

    <xs:complexType name="Vehicle">
        <xs:sequence>
            <xs:element name="WheelCount" type="xs:unsignedInt" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="IStartable">
        <xs:sequence>
            <xs:element name="RequiresKey" type="xs:boolean" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆一缕阳光ご 2024-09-02 15:31:07

我真的不明白如何在 XML 模式中实际做到这一点。 XML 模式确实具有继承 - 但只有单一继承,并且它不知道接口等。

在当前的 XML 模式中,没有办法让一个类(复杂类型)既基于基类,又同时基于“从第二个“接口”类继承” - XML 模式只是没有这些概念,抱歉。

您也许可以稍微重新设计您的继承 - IStartable >车辆> Car - 然后您可以在 XML 模式中对其进行建模。当前的设置可能无法使用当前的 XML 模式标准来实现。

I don't really see how you could actually do this in XML schema. XML schema does have inheritance - but only single inheritance, and it doesn't know about interfaces etc.

There's no way in current XML schema to have a class (complex type) be based both on a base class, and at the same time "inherit" from a second "interface" class - XML schema just doesn't have those concepts, sorry.

You might be able to redesign your inheritance a bit - IStartable > Vehicle > Car - then you could model it in XML schema. The current setup is probably just not doable using current XML schema standards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文