English 中文(简体)
Schema Issue: Can define element type OR add element attribute, but not both. I want both!
原标题:
  • 时间:2009-11-11 21:30:11
  •  标签:
  • xsd

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

最佳回答

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>
问题回答

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>
<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>




相关问题
How to validate DataTable with XML Schema?

Does anyone know how to validate a DataTable against a particular DataTable XSD (Schema)? Which would be the best performance-optimized way of doing this? For example: <?xml version="1.0" ...

Deprecated XML validation code problem C#

I m trying to figure out how to correct his deprecated xml schema validation code. public static bool ValidateXml(string xmlFilename, string schemaFilename) { ⁞ //Forward stream reading ...

Getting an object representation of an .XSD file in Java

What is the easiest way to obtain a statically typed representation of an XML schema (.XSD) in Java? More specifically I want to be able to programatically traverse all defined simpleType:s and ...

xmlserializer validation

I m using XmlSerializer to deserialize Xml achives. But I found the class xsd.exe generated only offers capability to read the xml, but no validation. For example, if one node is missing in a document,...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签