XSD - 如何定义两个元素之间的关系

时间:2022-04-20 19:38:45

I have a XSD file like below:

我有一个如下的XSD文件:

<element name="finder-def" minOccurs="0" maxOccurs="unbounded">
    <complexType>
         <attribute name="name" type="string" use="required"></attribute>
         <attribute name="description" type="string"></attribute>
         <attribute name="class" type="string" use="required"></attribute>
    </complexType>
</element>

<complexType name="Dimension">
    <sequence>
        <element name="finder" type="Finder" minOccurs="0" maxOccurs="1"/>
    </sequence>
</complexType>

<complexType name="Finder">
    <attribute name="name" type="String" use="required"/>
</complexType> 

XML file corresponds to above XSD file is below:

XML文件对应上面的XSD文件如下:

<finder-def name="circleFinder" description="Finds circle based on msisdn" class="com.onmobile.reporting.etl.processor.common.propertyplugins.CircleIdPropertyPlugin" />

<dimension name="circleId">
    <finder name="circleFinder" />
</dimension>

So, here I have defined one finder-def i.e. circleFinder and then want to refer to this finder-def through finder element.

所以,在这里我定义了一个finder-def,即circleFinder,然后想通过finder元素引用这个finder-def。

So the question is How can I validate that finder circleFinder has its defination defined above in finder-def

所以问题是如何验证finder circleFinder在finder-def中定义的定义

1 个解决方案

#1


2  

Just another way to use ID and IDREF types inside schema. Example: Sample XML:

在架构中使用ID和IDREF类型的另一种方法。示例:示例XML:

<?xml version="1.0" encoding="UTF-8"?>
<f:root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:f="http://test.com/finder" xsi:schemaLocation="http://test.com/finder finder.xsd">

<f:finder-def name="circleFinder" description="Finds circle based on msisdn"
              class="com.onmobile.reporting.etl.processor.common.propertyplugins.CircleIdPropertyPlugin"/>

<f:dimension name="circleId">
    <f:finder name="circleFinder"/>
</f:dimension>
</f:root>

XSD schema (I've formatted it a bit to validate)

XSD架构(我已将其格式化以进行验证)

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://test.com/finder"
        xmlns:tns="http://test.com/finder"
        elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:element name="root">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="finder-def" type="tns:finder-def" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="dimension" type="tns:Dimension" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
<xsd:complexType name="finder-def">
    <xsd:attribute name="name" type="xsd:ID" use="required"/>
    <xsd:attribute name="description" type="xsd:string"/>
    <xsd:attribute name="class" type="xsd:string" use="required"/>
</xsd:complexType>
<xsd:complexType name="Dimension">
    <xsd:sequence>
        <xsd:element name="finder" type="tns:Finder" minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="name" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="Finder">
    <xsd:attribute name="name" type="xsd:IDREF" use="required"/>
</xsd:complexType>
</xsd:schema>

#1


2  

Just another way to use ID and IDREF types inside schema. Example: Sample XML:

在架构中使用ID和IDREF类型的另一种方法。示例:示例XML:

<?xml version="1.0" encoding="UTF-8"?>
<f:root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:f="http://test.com/finder" xsi:schemaLocation="http://test.com/finder finder.xsd">

<f:finder-def name="circleFinder" description="Finds circle based on msisdn"
              class="com.onmobile.reporting.etl.processor.common.propertyplugins.CircleIdPropertyPlugin"/>

<f:dimension name="circleId">
    <f:finder name="circleFinder"/>
</f:dimension>
</f:root>

XSD schema (I've formatted it a bit to validate)

XSD架构(我已将其格式化以进行验证)

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://test.com/finder"
        xmlns:tns="http://test.com/finder"
        elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:element name="root">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="finder-def" type="tns:finder-def" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="dimension" type="tns:Dimension" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
<xsd:complexType name="finder-def">
    <xsd:attribute name="name" type="xsd:ID" use="required"/>
    <xsd:attribute name="description" type="xsd:string"/>
    <xsd:attribute name="class" type="xsd:string" use="required"/>
</xsd:complexType>
<xsd:complexType name="Dimension">
    <xsd:sequence>
        <xsd:element name="finder" type="tns:Finder" minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="name" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="Finder">
    <xsd:attribute name="name" type="xsd:IDREF" use="required"/>
</xsd:complexType>
</xsd:schema>