2
votes

I've inherited the task of creating a schema for some XML which already exists - and IMHO is not the best that could have been done. The section giving me problems is the element at the end of the 'scan-result' element.

The best I'm hoping for with regard to the data in the 'spectrum' element is to treat it as type="xs:string". I'll programatically divide up the numeric pairs that constitute the data in the string later. (Even though this step would not be needed had the data been properly structured in the first place.)

Here's a similar piece of XML data to what I have to work with...

    <scan-result>
      <spectrum-index>0</spectrum-index>
      <scan-index>2</scan-index>
      <time-stamp>5609</time-stamp>
      <tic>55510</tic>
      <start-mass>22.0</start-mass>
      <stop-mass>71.0</stop-mass>
      <spectrum count="5">30,11352;31,360;32,16634;45,1161;46,26003</spectrum>
    </scan-result>

The problem is, I can't seem to get a working definition for the 'spectrum' element that has the 'count' attribute and allows me to define the 'spectrum' element type as "xs:string".

What I would like is something like the following:

<xs:complexType name="ctypScanResult">
    <xs:sequence>
        <xs:element name="spectrum-index" type="xs:integer"/>
        <xs:element name="scan-index" type="xs:integer"/>
        <xs:element name="time-stamp" type="xs:integer"/>
        <xs:element name="tic" type="xs:integer"/>
        <xs:element name="start-mass" type="xs:float"/>
        <xs:element name="stop-mass" type="xs:float"/>
        <xs:element name="spectrum" type="xs:string">
            <xs:complexType>
                <xs:attribute name="count" type="xs:integer"/>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
    <xs:attribute name="count" type="xs:integer"/>
</xs:complexType>

The problem is that I can define the type of the 'spectrum' element as "xs:string" XOR I can define the anonymous 'xs:complexType' in the 'spectrum' element, which allows me to insert the 'count' attribute. But I need to be able to express both.

Given that I'm kind of stuck with the XML as it was handed to me, is there a schema definition that will allow me to describe this data?

Sorry this is long, but thanks to any and all who respond,

AlarmTripper

Followup: I know why the error occurs...

Quoted from W3C:

3.3.3 Constraints on XML Representations of Element Declarations Schema Representation Constraint: Element Declaration Representation OK

In addition to the conditions imposed on element information items by the schema for schemas: all of the following must be true: 1 default and fixed must not both be present. 2 If the item's parent is not , then all of the following must be true: 2.1 One of ref or name must be present, but not both. 2.2 If ref is present, then all of , , , , , nillable, default, fixed, form, block and type must be absent, i.e. only minOccurs, maxOccurs, id are allowed in addition to ref, along with . 3 type and either or are mutually exclusive. 4 The corresponding particle and/or element declarations must satisfy the conditions set out in Constraints on Element Declaration Schema Components (§3.3.6) and Constraints on Particle Schema Components (§3.9.6).

But I'm still in the same fix I was before... How can I actually accomplish something that resembles my goal?

Thanks,

AlarmTripper

3

3 Answers

2
votes

Let a tool do it for you! Try xsd.exe.

Or, if you must define by hand, at least check your hand-written-definition with an automatically generated one.

Here's what XSD.exe gave me for your input. I trimmed out some MS-NS cruft.

<xs:element name="spectrum">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="count" type="xs:string" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
</xs:element>
1
votes

You need to set the attribute mixed="true" on complexType:

<xs:element name="spectrum">
  <xs:complexType mixed="true">
    <xs:attribute name="count" type="xs:integer" />
  </xs:complexType>
</xs:element>

EDIT: Okay, just read your comment, sorry. I believe the following should work instead:

<xs:element name="spectrum">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="count" type="xs:integer" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>
1
votes
<xs:element name="spectrum" type="xs:string">
  <xs:complexType>
    <!-- ADD THIS NEXT LINE -->
    <xs:complexContent mixed="true"/>
    <xs:attribute name="count" type="xs:integer"/>
  </xs:complexType>
</xs:element>