如何在 xsd 上定义不同的属性范围?

发布于 2025-01-08 12:34:22 字数 792 浏览 1 评论 0原文

<Metamodel>
  <Package Name="serhat">
    <OwnedType>
      <Class Name="serhat"></Class>
    </OwnedType>
  </Package>
  <Package Name="serhat2">
    <OwnedType>
      <Class Name="serhat"></Class>
    </OwnedType>
  </Package>
  <Class Name="serhat"></Class>
</Metamodel>

我在元素上有名称属性。此名称属性在包元素中必须是唯一的 但在类元素中名称属性可能会在另一个类元素上重复,例如;

  <Package Name="serhat">
    <OwnedType>
      <Class Name="serhat"></Class>
    </OwnedType>
  </Package>
  <Package Name="serhat2">
    <OwnedType>
      <Class Name="serhat"></Class>
    </OwnedType>
  </Package>

我如何定义这个属性?

<Metamodel>
  <Package Name="serhat">
    <OwnedType>
      <Class Name="serhat"></Class>
    </OwnedType>
  </Package>
  <Package Name="serhat2">
    <OwnedType>
      <Class Name="serhat"></Class>
    </OwnedType>
  </Package>
  <Class Name="serhat"></Class>
</Metamodel>

i have name attribute on elements.This name attribute must be unique in Package element
but in Class element name attribute may repeat on another class element like;

  <Package Name="serhat">
    <OwnedType>
      <Class Name="serhat"></Class>
    </OwnedType>
  </Package>
  <Package Name="serhat2">
    <OwnedType>
      <Class Name="serhat"></Class>
    </OwnedType>
  </Package>

how can i define this attribute ?

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

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

发布评论

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

评论(1

孤蝉 2025-01-15 12:34:22

我相信你想要的不是属性,而是身份约束。因此,生成的 XSD 将如下所示(我假设 Package 下的类必须以某种方式重复):

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Metamodel">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element maxOccurs="unbounded" name="Package">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="OwnedType">
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element ref="Class" maxOccurs="unbounded"/>
                                        </xsd:sequence>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                        <xsd:attribute name="Name" type="xsd:string" use="required"/>
                    </xsd:complexType>
                    <xsd:unique name="ClassNameUniqueInPackage">
                        <xsd:selector xpath="OwnedType/Class"/>
                        <xsd:field xpath="@Name"/>
                        </xsd:unique>
                </xsd:element>
                <xsd:element ref="Class"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="Class">
        <xsd:complexType>
            <xsd:attribute name="Name" type="xsd:string" use="required"/>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

使用此假设模型,以下 XML 将验证:

<Metamodel>
    <Package Name="serhat">
        <OwnedType>
            <Class Name="serhat"></Class>
            <Class Name="serhat2"></Class>
        </OwnedType>
    </Package>
    <Package Name="serhat2">
        <OwnedType>
            <Class Name="serhat"></Class>
        </OwnedType>
    </Package>
    <Class Name="serhat"></Class>
</Metamodel>

但不是这样:

<Metamodel>
    <Package Name="serhat">
        <OwnedType>
            <Class Name="serhat"></Class>
            <Class Name="serhat"></Class>
        </OwnedType>
    </Package>
    <Package Name="serhat2">
        <OwnedType>
            <Class Name="serhat"></Class>
        </OwnedType>
    </Package>
    <Class Name="serhat"></Class>
</Metamodel>

错误将是:

Error occurred while loading [], line 5 position 5
There is a duplicate key sequence 'serhat' for the 'ClassNameUniqueInPackage' key or unique identity constraint.

I believe that what you want is not in the attribute, but in a identity constraint. So, a generated XSD would look like this (I assume the Class under Package must somehow repeat):

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Metamodel">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element maxOccurs="unbounded" name="Package">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="OwnedType">
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element ref="Class" maxOccurs="unbounded"/>
                                        </xsd:sequence>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                        <xsd:attribute name="Name" type="xsd:string" use="required"/>
                    </xsd:complexType>
                    <xsd:unique name="ClassNameUniqueInPackage">
                        <xsd:selector xpath="OwnedType/Class"/>
                        <xsd:field xpath="@Name"/>
                        </xsd:unique>
                </xsd:element>
                <xsd:element ref="Class"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="Class">
        <xsd:complexType>
            <xsd:attribute name="Name" type="xsd:string" use="required"/>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

With this assumed model, the following XML will validate:

<Metamodel>
    <Package Name="serhat">
        <OwnedType>
            <Class Name="serhat"></Class>
            <Class Name="serhat2"></Class>
        </OwnedType>
    </Package>
    <Package Name="serhat2">
        <OwnedType>
            <Class Name="serhat"></Class>
        </OwnedType>
    </Package>
    <Class Name="serhat"></Class>
</Metamodel>

But not this:

<Metamodel>
    <Package Name="serhat">
        <OwnedType>
            <Class Name="serhat"></Class>
            <Class Name="serhat"></Class>
        </OwnedType>
    </Package>
    <Package Name="serhat2">
        <OwnedType>
            <Class Name="serhat"></Class>
        </OwnedType>
    </Package>
    <Class Name="serhat"></Class>
</Metamodel>

The error would be:

Error occurred while loading [], line 5 position 5
There is a duplicate key sequence 'serhat' for the 'ClassNameUniqueInPackage' key or unique identity constraint.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文