[XMLSCHEMA-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Repeating elements with fixed attribute values

From: Michael Kay <mike@saxonica.com>
Date: Fri, 4 Jan 2008 15:27:40 -0000
To: "'King, Jeffrey \(Mission Systems\)'" <Jeff.King@ngc.com>, <xmlschema-dev@w3.org>
Message-ID: <012001c84ee6$58474710$6401a8c0@turtle>
RE: Repeating elements with fixed attribute values
It's a good error message because it describes the problem precisely:
Elements with the same name and in the same scope must have the same type
 
You can't do this in XML Schema 1.0, which assumes that the structure will
be in the element names, not in attribute values.
 
Some of the options open to you are:
 
(a) Use Schematron
 
(b) Use the xs:assert facility of XML Schema 1.1, which is implemented in
the current Saxon release
 
(c) Do validation as a two-step operation: first transform to a structure
that can be described in XML Schema, then perform XML Schema validation on
the result.
 
Using xs:assert it would look something like this:
 
<xs:complexType>
  <xs:sequence>
    <xs:element name="field" type="fieldType" minOcccurs="3" maxOccurs="3"/>
  </xs:sequence>
  <xs:assert test="field[1][@name = 'field1' and @value='1']"/>
  <xs:assert test="field[2][@name = 'field2' and @value='2']"/>
  <xs:assert test="field[3][@name = 'field3' and @value='3']"/>
</xs:complexType>
 
Michael Kay
http://www.saxonica.com/
 
 


  _____  

From: xmlschema-dev-request@w3.org [mailto:xmlschema-dev-request@w3.org] On
Behalf Of King, Jeffrey (Mission Systems)
Sent: 04 January 2008 14:29
To: xmlschema-dev@w3.org
Subject: Repeating elements with fixed attribute values




I have an xml document similar to this: 

<root> 
  <word name="one"> 
    <field name="field1" number="1">any string here</field> 
    <field name="field2" number="2">any string here</field> 
    <field name="field3" number="3">any string here</field> 
    . 
  </word> 
  <word name="two"> 
    <field name="field4" number="4">any string here</field> 
    <field name="field5" number="5">any string here</field> 
    <field name="field6" number="6">any string here</field> 
    . 
  </word> 
</root> 

The attributes have fixed values.  I am struggling to create a schema for
this. 

If I take this approach: 

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="root"> 
    <xs:complexType> 
      <xs:sequence> 
        <xs:element maxOccurs="1" minOccurs="1" name="word"> 
           <xs:complexType> 
             <xs:sequence> 
                                                                
             <xs:element maxOccurs="1" minOccurs="1" name="field"> 
               <xs:complexType> 
                  <xs:simpleContent> 
                     <xs:extension base="xs:string"> 
                       <xs:attribute name="name" type="xs:string"
use="required" fixed="field1" /> 
                       <xs:attribute name="number" type="xs:unsignedShort"
use="required" fixed="1" /> 
                     </xs:extension> 
                 </xs:simpleContent> 
              </xs:complexType> 
             </xs:element> 
                                                        
        <xs:element maxOccurs="1" minOccurs="1" name="field"> 
             <xs:complexType> 
                    <xs:simpleContent> 
                       <xs:extension base="xs:string"> 
                          <xs:attribute name="name" type="xs:string"
use="required" fixed="field2" /> 
                          <xs:attribute name="number"
type="xs:unsignedShort" use="required" fixed="2" /> 
                       </xs:extension> 
               </xs:simpleContent> 
                </xs:complexType> 
              </xs:element> 
            ... 
          etc. 

I get an error stating: "Elements with the same name and in the same scope
must have the same type".  I'm not sure what approach to take here.  I think
this is pretty elementary, but any help with finding a solution would be
appreciated.

Thank you. 

Jeffrey 
Received on Friday, 4 January 2008 15:27:50 GMT

Subscribe to the Stylus Scoop newsletter for helpful XML tips and tutorials.
Email
First Name
Last Name
Company

Download Stylus Studio 6 XML Enterprise Edition

Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.