[xsd-users] Howto to use an attribute as an index to a sequence

Thomas Maenner tmaenner at aehr.com
Wed Dec 12 21:04:18 EST 2007


Hi Boris,

I'd like to use unsigned int attributes as an index or key into a sequence.

Basically, I'd like to use the attribute "unIndex" in something like this (See xsd and xml files below):

---snip---
	test_root::node_sequence& ns( t->node() );
	for( unsigned int i=0; i < ns.size(); i++ )
	{
		cout << i << ": " << ns[i].strTest() << endl;
	}
---snip---

The output should be:
0: String0
1: String2
2: String1
and not:
0: String0
1: String1
2: String2

Would  this be possible? If so, I'm just too blind to see it... ;)

Thanks much in advance for your help
Tom

------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
        <xs:element name="test_root">
                <xs:annotation>
                        <xs:documentation>Comment describing your root element</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                        <xs:sequence>
                                <xs:element name="node" maxOccurs="unbounded">
                                        <xs:complexType>
                                                <xs:complexContent>
                                                        <xs:extension base="nodeType"/>
                                                </xs:complexContent>
                                        </xs:complexType>
                                </xs:element>
                        </xs:sequence>
                </xs:complexType>
        </xs:element>
        <xs:complexType name="nodeType">
                <xs:sequence>
                        <xs:element name="strTest" type="xs:string"/>
                        <xs:element name="nTest" type="xs:integer"/>
                        <xs:element name="unTest" type="xs:unsignedInt"/>
                </xs:sequence>
                <xs:attribute name="unIndex" type="xs:unsignedInt"/>
        </xs:complexType>
</xs:schema>


<?xml version="1.0" encoding="UTF-8"?>
<test_root xsi:noNamespaceSchemaLocation="test_schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <node>
                <strTest>String0</strTest>
                <nTest>-100</nTest>
                <unTest>0</unTest>
        </node>
        <node unIndex="2">
                <strTest>String1</strTest>
                <nTest>-10</nTest>
                <unTest>10</unTest>
        </node>
        <node unIndex="1">
                <strTest>String2</strTest>
                <nTest>-20</nTest>
                <unTest>20</unTest>
        </node>
</test_root>

-- 
Thomas Maenner
E-Mail: mailto:tmaenner at aehr.com




More information about the xsd-users mailing list