xsd按值限制

时间:2013-03-14 17:25:42

标签: xsd xsd-validation

伙计们,我需要你的帮助以下。 我的xml看起来像这样:

    <Images>
    <Image ImageId="1" ImageFile="a.png"/>
    <Image ImageId="2" ImageFile="b.png"/>
    <Image ImageId="3" ImageFile="c.png"/>
    <Image ImageId="4" ImageFile="d.png"/>
    <Image ImageId="5" ImageFile="e.png"/>
</Images>
<Banners>
    <Banner BannerId="1" ImageId="2"/>
    <Banner BannerId="43" ImageId="3"/>
    <Banner BannerId="45" ImageId="4"/>
    <Banner BannerId="596" ImageId="2"/>
    <Banner BannerId="6" ImageId="2"/>
</Banners>

我需要创建一个xsd,它将Banner元素中的ImageId值限制为出现在Images中的值。有可能吗?

1 个答案:

答案 0 :(得分:1)

是;您需要的XML架构组件是key / keyref:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="sample">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Images">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element maxOccurs="unbounded" name="Image">
                                <xsd:complexType>
                                    <xsd:attribute name="ImageId" type="xsd:unsignedByte" use="required"/>
                                    <xsd:attribute name="ImageFile" type="xsd:string" use="required"/>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="Banners">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element maxOccurs="unbounded" name="Banner">
                                <xsd:complexType>
                                    <xsd:attribute name="BannerId" type="xsd:unsignedShort" use="required"/>
                                    <xsd:attribute name="ImageId" type="xsd:unsignedByte" use="required"/>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:key name="PK">
            <xsd:selector xpath="Images/Image"/>
            <xsd:field xpath="@ImageId"/>
        </xsd:key>
        <xsd:keyref name="FK" refer="PK">
            <xsd:selector xpath="Banners/Banner"/>
            <xsd:field xpath="@ImageId"/>       
        </xsd:keyref>
    </xsd:element>
</xsd:schema>

示例错误消息:

The key sequence '8' in Keyref fails to refer to some key.
xsd-restriction-by-value.xml is XSD 1.0 invalid.