【问题标题】:Using ref in XML schema在 XML 模式中使用 ref
【发布时间】:2014-02-17 15:22:40
【问题描述】:

我正在尝试通过 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>

【问题讨论】:

  • 你能展示你目前写的架构吗?
  • 我把它添加到原来的问题中

标签: xml xsd


【解决方案1】:

您的架构有一些小问题 - Row 类型中的 element ref="column" 需要是 name 而不是 ref,并且它需要一个大于 1 的 maxOccurs(默认值)。

<xsd:complexType name="Row">
    <xsd:sequence>
        <xsd:element name="column" maxOccurs="unbounded">

一旦我修复了这些错误,一切都很好,并且交叉引用要求(row 中提到的每个列 ID 必须对应于 columns 部分中的一个)由 key 提供和keyref

【讨论】:

  • 但我想使用 ref 来强制执行它是对列的引用的想法。这不是为了什么?
  • @user3319681 no,ref 用于指向架构中其他地方的顶级element 声明。 keykeyref 对强制执行交叉引用约束,这已经是正确的。如果我将“124”引用之一更改为“125”,我会收到验证失败。
  • 你是说,鉴于我的初衷,我拥有的模式(“ref”除外)是正确的方法吗?
  • @user3319681 是的。 Rows 类型中的 unique 确保您不能在同一行中引用同一列两次,key 断言 columns 部分中的所有列 ID 必须是不同的,并且 @ 987654339@ 处理交叉引用要求(row 中的每个 column 必须与 columns 中的一个匹配)。
  • 感谢您的确认
猜你喜欢
  • 2019-08-11
  • 1970-01-01
  • 2015-08-10
  • 2012-02-16
  • 2019-12-31
  • 2018-04-13
  • 2012-09-22
  • 1970-01-01
  • 2014-07-08
相关资源
最近更新 更多