1

伙计们,我需要您在以下方面的帮助。我的 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 值限制为图像中出现的值。是否可以?

4

1 回答 1

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.
于 2013-03-14T18:38:09.323 回答