2013-03-14 89 views
1

夥計們,我需要你的幫助以下。 我的XML看起來是這樣的:xsd限制值

<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將在橫幅元素限制圖像標識值的出現在圖片。可能嗎?

回答

1

是;你需要的XML架構組件是鍵/ 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. 
+0

感謝,這工作。我原來的錯誤是試圖使用./Banner和./Image代替完整路徑。 – atarno 2013-03-15 14:40:37