1

我有这个xsd

 <xs:complexType name="ShapeLine">
    <xs:annotation>
        <xs:documentation>
            ShapeLine - the line between two shapes
        </xs:documentation>
    </xs:annotation>
    <xs:complexContent>
        <xs:extension base="archimate:Line" >
            <xs:attribute name="category" type="xs:string" use="required"> 
            </xs:attribute> 
            <xs:attribute name="source" type="xs:IDREF" use="required" />
            <xs:attribute name="target" type="xs:IDREF" use="required" />
        </xs:extension>             
    </xs:complexContent>
</xs:complexType>

    
 <xs:complexType name="Shape">
    <xs:annotation>
        <xs:documentation>
            Shape object
        </xs:documentation>
    </xs:annotation>
    <xs:complexContent>
        <xs:extension base="archimate:ViewNodeType" >                          
          <xs:attribute name="shapeId" type="xs:string" use="required">
                <xs:annotation>
                    <xs:documentation>
                        shapeId contains the id of the shape
                    </xs:documentation>
                </xs:annotation>
            </xs:attribute> 
              <xs:attribute name="name_internal" type="xs:string" use="required">
                <xs:annotation>
                    <xs:documentation>
                        name_internal contains the name_internal of the shape
                    </xs:documentation>
                </xs:annotation>
            </xs:attribute>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

这是我需要验证的 xml:

 <views>
 <diagrams>
  <view identifier="id-7b98c8ec-243a-49fb-b6b0-baa68c95badb">
    <name xml:lang="EN">helloe</name>
    <node xsi:type="q1:Shape" identifier="id-1d599237- 
 76af-4fc7-8d9a-c4356c3b2137" x="10" y="30" w="1" h="1" name_internal="BD_shapes_av_Box" 
shapeId="5da9cedd0c0ba649f8cae72e" angle="0" isgroup="False" alignment="" textalign="0" size="72 72">            
    </node>
    <node xsi:type="q2:Shape" identifier="id-76efea7a-6cf3-4cf0-bbd4-e36597c0653b" x="454" y="54" w="1" h="1" name_internal="BD_shapes_av_Two sided arrow" shapeId="5dad549f0c0ba639c4a5a3ac" angle="0" isgroup="False" alignment="" textalign="0" size="72 30,476">         
    </node>
    <connection xsi:type="q3:ShapeLine" identifier="id-4ab26cbf-509b-4657-b066-1a676a2773eb" source="id-1d599237-76af-4fc7-8d9a-c4356c3b2137" target="id-76efea7a-6cf3-4cf0-bbd4-e36597c0653b" category="line_dottednoarrows">         
      <sourceAttachment x="82" y="66" />
      <targetAttachment x="454" y="69" />
    </connection>
  </view>
</diagrams>

我需要创建一个 xsd 验证,以便仅在 Shape 类型的对象之间允许使用 ShapeLine。这可以在xsd中实现吗?我对 xsd-s 还很陌生,因此感谢您提供任何帮助。

4

1 回答 1

0

我不确定我是否正确理解了这个问题。我想我缺少必要的信息。您要求创建验证 xml 以确保类型 shape 的元素可以具有类型 shapeline 的子元素的 xml 架构定义。但是查看示例 xml,shapeline 类型的元素是 shape 类型元素的兄弟。而 q1:Shape en q2:Shape 让我想到有 2 个 Shape 类型具有不同的命名空间,或者 q1 和 q2 命名空间前缀指向同一个实际命名空间。

类型必须分配给元素或属性,并且这些元素和属性正在创建消息的布局。您不能仅使用类型来描述 xml 消息来创建 xsd。您需要将类型分配给元素和/或属性。您使用这些元素和属性创建的顺序和结构将决定 xml 消息的外观。

下面的 xml 示例确实有两个选项。首先是兄弟姐妹,然后是兄弟姐妹

<?xml version="1.0" encoding="UTF-8"?>
<ns:root xmlns:ns="http://tempuri.org/example" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ns:Sibling1 attr1="text" attr2="text">
        <ns:Child1 xsi:type="ns:ShapeLine">
            <ns:Child2>text</ns:Child2>
            <ns:Child3>text</ns:Child3>
        </ns:Child1>
    </ns:Sibling1>
    <ns:Sibling2 attr1="text" attr2="text" xsi:type="ns:Shape"/>
    <ns:Sibling3 xsi:type="ns:ShapeLine">
        <ns:Child2>text</ns:Child2>
        <ns:Child3>text</ns:Child3>
    </ns:Sibling3>
</ns:root>

此示例 xml 是从以下 xsd 生成的。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://tempuri.org/example" targetNamespace="http://tempuri.org/example" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Sibling1">
                    <xs:complexType>
                        <xs:complexContent>
                            <xs:extension base="ns:Shape">
                                <xs:sequence>
                                    <xs:element name="Child1" type="ns:ShapeLine"/>
                                </xs:sequence>
                            </xs:extension>
                        </xs:complexContent>
                    </xs:complexType>
                </xs:element>
                <xs:element name="Sibling2" type="ns:Shape"/>
                <xs:element name="Sibling3" type="ns:ShapeLine"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="Shape">
        <xs:attribute name="attr1"/>
        <xs:attribute name="attr2"/>
    </xs:complexType>
    <xs:complexType name="ShapeLine">
        <xs:sequence>
            <xs:element name="Child2"/>
            <xs:element name="Child3"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

这是您想要实现的目标的一个非常简化的版本,但希望它能帮助您入门。

于 2020-07-25T07:44:07.303 回答