views:

126

answers:

2

I have an XSD definition that has a list of tagged elements. In what way is it possible to allow developer's to only select from a list of elements already defined in the XML file when entering values for a choice-restricted element?

In other words, given this XML declaration:

<collection>
      <myItem name="Item_1">
        <childElement />
      </myItem>

      <myItem name="Item_2">
        <childElement>
          <item name="Item_1"/>
        </childElement>
      </myItem>

      <myItem name="Item_3">
        <childElement>
          <item name="Child_2"/>
        </childElement>
      </myItem>
</collection>

... and this XSD definition

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="collection">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="collection">
          <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="unbounded">
              <xs:element name="childElement" >
                <xs:complexType>
                  <xs:sequence  minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="item">
                      <xs:complexType>
                        <xs:attribute name="name" type="xs:string" use="optional" />
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>

            <xs:attribute name="name" type="xs:string" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I would like to enable users to select from a list of myItem elements already defined in the XML file whenever they want to enter the name value of the childEelement?

This concept is seen when selecting attributes values in XAML or even some of Visual Studio 2008's schemas.

A: 

Did you try the <choice/> element?

Ionuț G. Stan
I noticed the <choice/> tag but it looks as if i need to reference another element in the document, which would not be practical if i have to manually reference a *physical* declaration and there could be numerous item. Do you have any pointers in this regard?
Mike J
+3  A: 

You may want to look at the identity constraints that are available through XML schema. These constraints allow you to apply an XPath expression to denote both uniqueness and reference constraints for a given element.

You should be able to write something like the following, albeit my syntax is probably missing a <sequence> or <complexType> definition. I haven't verified this, but the idea is to create a "key" for the values you wish to constrain to, then refer to the key with a "key-ref" in the elements you wish to constrain.

<xs:element name="myItem">
  <xs:complexType>
    <xs:attribute name="name" type="xs:string" use="optional"/>
    <xs:key name="itemNameKey">
      <xs:selector xpath="myItem"/>
      <xs:field xpath="@name"/>
    </xs:key>
  </xs:complexType>
</xs:element>

<xs:element name="childElement">
  <xs:complexType>
    <xs:attribute name="name" type="xs:string" use="optional"/>
    <xs:keyref name="itemConstraint" refer="itemNameKey">
      <xs:selector xpath="childElement"/>
      <xs:field xpath="@name"/>
    </xs:key>
  </xs:complexType>
</xs:element>
Steve Guidi
Thank you for this sample. I will try it out now and send feedback.:)
Mike J
I tried this option and while the validation portion works, it does not quite give me the Intellisense I'm looking for. I came across the <xs:enumeration> tag and that seems to be a closer match, but is not really dynamic based on the contents of the XML file.
Mike J
I'm not sure how to get Intellisense to work for this type of XML. Perhaps you could study the XAML file in question to see if there are any similarities?
Steve Guidi