Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在XML模式中使用ref_Xml_Xsd - Fatal编程技术网

在XML模式中使用ref

在XML模式中使用ref,xml,xsd,Xml,Xsd,我试图创建一个模式,该模式通过columns id属性具有行引用列。以下xml和xsd将不会验证,因为找不到该列 如何为以下XML创建架构,以便从行元素引用列id: <?xml version="1.0" encoding="ISO-8859-1" ?> <mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <rows> <row>

我试图创建一个模式,该模式通过columns id属性具有行引用列。以下xml和xsd将不会验证,因为找不到该列

如何为以下XML创建架构,以便从行元素引用列id:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <rows>
        <row>
            <column id="123" />
            <column id="124" />
        </row>
        <row>
            <column id="123" />
            <column id="124" />
        </row>
    </rows>

    <columns>
        <column id="123">
            <name>Apple</name>
        </column>

        <column id="124">
            <name>Banana</name>
        </column>
    </columns>

</mapping>

苹果
香蕉
我的xsd看起来像这样,但它不工作。。。它找不到列引用:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="mapping">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="rows" type="Rows" minOccurs="0" maxOccurs="1"/>
            <xsd:element name="columns"  type="Columns" minOccurs="0" maxOccurs="1"/>
        </xsd:sequence>
    </xsd:complexType>      
    <xsd:key name="PKeyColumn">
        <xsd:selector xpath="columns/column"/>
        <xsd:field xpath="@id"/>
    </xsd:key>      
    <xsd:keyref name="FKeyColumn" refer="PKeyColumn">
        <xsd:selector xpath="rows/row/column"/>
        <xsd:field xpath="@id"/>
    </xsd:keyref>
</xsd:element>

<xsd:complexType name="Row">
    <xsd:sequence>
        <xsd:element ref="column">
            <xsd:complexType>
                <xsd:attribute name="id" use="required" type="xsd:integer" />
            </xsd:complexType>
        </xsd:element>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="Rows">
    <xsd:sequence>
        <xsd:element name="row" type="Row" minOccurs="1" maxOccurs="unbounded">
            <xsd:unique name="UKeyColumn">
                <xsd:selector xpath="column"/>
                <xsd:field xpath="@id"/>
            </xsd:unique>   
        </xsd:element>
    </xsd:sequence>
</xsd:complexType>  

<xsd:complexType name="Columns">
    <xsd:sequence>
        <xsd:element name="column" type="Column" minOccurs="1" maxOccurs="unbounded"/>
     </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="Column">
    <xsd:sequence>
        <xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:integer" />
</xsd:complexType>

</xsd:schema>

您的模式有一些小问题-行中的
元素ref=“column”
类型需要是
名称
而不是
ref
,并且需要大于1的
maxOccurs
(默认值)



一旦我修复了这些错误,所有这些都会很好地验证交叉引用要求(在
中提到的每个列ID必须对应于
部分中的一个)是由
keyref

来满足的。你能展示一下你到目前为止写的模式吗?我把它添加到了原始问题中,但我想用ref来强化这个想法,它是对专栏的引用。这不是为什么吗?@user3319681否,
ref
用于指向架构中其他位置的顶级
元素
声明。
key
keyref
对是强制执行交叉引用约束的,这已经是正确的了。如果我将其中一个“124”引用更改为“125”,则验证失败。那么,您是否认为,根据我的原始意图,我使用的模式(除了“ref”)是正确的方法?@user3319681是的。
类型中的
唯一
确保您不能在同一行中两次引用同一列,
声明
部分中的所有列ID必须是不同的,
keyref
处理交叉引用要求(一行
中的每一列
必须与
列中的一列相匹配。感谢您的确认
<xsd:complexType name="Row">
    <xsd:sequence>
        <xsd:element name="column" maxOccurs="unbounded">