开发者

Strange XML schema

Ok, so I have been tasked with writing an XSD from an XML document given to us by a vendor. This vendor does not have an XSD they can furnish, so I am reverse engineering one. Here is an example of the way this xml is formed:

<field name="id">1</field>
<field name="Sport">Football</field>
<field name="Position">Quarterback</field>
<field name="Touchdowns">7</field>
<field name="Interceptions">2</field>
<field name="Yardage">2000</field>

So, since all elements are named "field", I am having trouble creating the schema for this. This is what I have so far:

<xs:element name="field" type="xs:int">
     <xs:simpletype>
         <xs:attribute name="name" type="xs:string" default="id开发者_StackOverflow"/>
     </xs:string>
</xs:element>

The problem I am running into is that all the elements will have the same name. The software I am using to write the xsd is having problems with that. Do I have the correct approach here?

Thanks for any help.


The default value for maxOccurs of your field element is 1 (so try to set it to unbounded) also, simpleType may not contain attributes or sub-elements.

I believe the following example is working:

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="fields">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="field"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

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

XML:

<?xml version="1.0" ?>
<fields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="fields.xsd">
    <field name="id">1</field>
    <field name="Sport">Football</field>
    <field name="Position">Quarterback</field>
    <field name="Touchdowns">7</field>
    <field name="Interceptions">2</field>
    <field name="Yardage">2000</field>
</fields>

I used xs:string in the name type, but I guess you could define a new datatype to enforce only certain keywords...


You essentially have a type that is a basic element that you extend in different ways. Please excuse me if the code is off a bit, I've not tried validating the schema and I'm writing it from memory, not a good way of writing a Schema, but you should get the idea.

<xsd:complexType name="field">
    <xsd:simpleContent>
        <xsd:extension>
            <xsd:attribute name="name" />
        </xsd:extension>
    </xsd:simpleContent>
</xsd:complexType>

to define the basic element with the attribute name (no constraint on the attribute type). This will validate all of the examples in the original post.

then you can try extending that:

<xsd:complexType name="idField">
    <xsd:complexContent>
        <xsd:extension base="field">
            ...restrict it in here
        </xsd:extension>
    <xsd:complexContent>
</xsd:complexType>

And doing something like that for each variation, essentially making them sub-types of the main definition.

I realise this "answer" doesn't really give a definitive answer, but hopefully it'll push you in the right direction.

Failing that, I don't see what you can do apart from get the vendor to rewrite their XML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜