help with xml schema -- making a unique key

Discussion in 'XML' started by X. Lee, Jul 26, 2004.

  1. X. Lee

    X. Lee Guest

    Hi, please take a look at this schema and tell me how I can make the
    value of my AttachmentKey element(s) unique within the entire instance
    document.

    I know that I have to use <key> or <unique>, but I can't get it to
    work. I don't have knowledge of xpath and the examples I find on the
    web aren't working.

    here is my schema:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema targetNamespace="test.ns.abc">
    <xsd:element name="root">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element ref="ns:A"/>
    <xsd:element ref="ns:Attachment" maxOccurs="unbounded"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="Attachment">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element ref="ns:AttachmentKey"/>
    <xsd:element ref="ns:AttachmentName"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="AttachmentText" type="xsd:string"/>
    <xsd:element name="AttachmentName" type="xsd:string"/>
    <xsd:element name="AttachmentPageTotal" type="xsd:int"/>
    <xsd:element name="A">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element ref="ns:B"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="B">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element ref="ns:Attachment"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="AttachmentKey"/>
    </xsd:schema>

    --------------

    As you can see, AttachmentKey is a subelement of Attachment, and is
    used in more than one place in the example above. I want it so that
    when the instance document is created, no matter how many instances of
    Attachment is in it, each AttachmentKey value is unique, so that I can
    use them as keys.

    So this following instance doc would be invalid:

    <root>
    <A>
    <B>
    <Attachment>
    <AttachmentKey>1</AttachmentKey>
    <AttachmentName>Name</AttachmentName>
    </Attachment>
    </B>
    </A>
    <Attachment>
    <AttachmentKey>1</AttachmentKey>
    <AttachmentName>Name</AttachmentName>
    </Attachment>
    <Attachment>
    <AttachmentKey>2</AttachmentKey>
    <AttachmentName>Name</AttachmentName>
    </Attachment>
    </root>



    But this one would be valid:

    <root>
    <A>
    <B>
    <Attachment>
    <AttachmentKey>1</AttachmentKey>
    <AttachmentName>Name</AttachmentName>
    </Attachment>
    </B>
    </A>
    <Attachment>
    <AttachmentKey>2</AttachmentKey>
    <AttachmentName>Name</AttachmentName>
    </Attachment>
    <Attachment>
    <AttachmentKey>3</AttachmentKey>
    <AttachmentName>Name</AttachmentName>
    </Attachment>
    </root>

    thanks!
     
    X. Lee, Jul 26, 2004
    #1
    1. Advertising

  2. Hi,

    Within your declaration of root (before the </xs:element>), you should
    add

    <xsd:key name="uniqueAttachment">
    <xsd:selector xpath=".//ns:Attachment"/>
    <xsd:field xpath="ns:AttachmentKey"/>
    </xsd:key>

    You also need to give AttachmentKey a simple type (or a complex type
    with simple content).

    Hope that helps,
    Priscilla

    -----------------------------------------------------
    Priscilla Walmsley
    Author, Definitive XML Schema (Prentice Hall PTR)
    http://www.datypic.com
    -----------------------------------------------------

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Priscilla Walmsley, Jul 27, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,663
    Markus
    Nov 23, 2005
  2. Fred Smith
    Replies:
    1
    Views:
    997
    Martin Honnen
    Feb 5, 2004
  3. Hari Om
    Replies:
    1
    Views:
    482
    Martin SChukrazy
    Mar 2, 2004
  4. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,328
    Stanimir Stamenkov
    Apr 25, 2005
  5. Replies:
    3
    Views:
    3,172
Loading...

Share This Page