3

I did some xml Xsd validation according to following method: Xml validation using XSD schema

 .......................................................
 XmlReaderSettings settings = new XmlReaderSettings();
 settings.Schemas.Add(null, xsdFilePath);
 settings.ValidationType = ValidationType.Schema;
 settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(settings_ValidationEventHandler);
 XmlDocument document = new XmlDocument();
 document.Load(xmlFilePath);
 XmlReader rdr = XmlReader.Create(new StringReader(document.InnerXml), settings);

 while (rdr.Read())
 {

 }
 ...........................................................

and it gives me error saying: "The 'ref' attribute cannot be present"

my XSD looks like :

...........
<xs:element name="totals" minOccurs="0" ref="DocTotal"/>
..................................

<xs:element name="DocTotal">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="totalQty" minOccurs="0" type="xs:decimal"/>
            <xs:element name="totalTax" minOccurs="0" type="xs:decimal"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:string" use="required"/>
    </xs:complexType>
</xs:element>

and my xml looks like:

<totals>
    <totalQty>800</totalQty>
    <totalTax>0.00<totalTax>
</totals>

I believe this error occurs because of the both "name" and "ref": attributes exists in same elements: however I think this is not wrong in XSD(appreciate your comments on this): in this case is there any way to validate this XSD with xml:

Community
  • 1
  • 1
  • 2
    And you tagged this question with "java" because...? – Ed S. Mar 13 '13 at 06:55
  • [this](http://stackoverflow.com/q/8896602/238902) question might be of assistance – default Mar 13 '13 at 07:17
  • I know that w3cschools aren't recommended, but at [their site](http://www.w3schools.com/schema/el_attribute.asp) it says _ref Optional. Specifies a reference to a named attribute. **Name and ref attributes cannot both be present.** If ref is present, simpleType element, form, and type cannot be present_. So you are correct. – default Mar 13 '13 at 07:19
  • Thanks for that: but I think your link XML Schema **attribute** Element:(http://www.w3schools.com/schema/el_attribute.asp) is discuss about attribute elements: for that Name and ref attributes cannot both be present. how ever ["XML Schema element Element"](http://www.w3schools.com/schema/el_element.asp) does not say that name and ref cannot be present in same element: – Bathiya Ladduwahetty Mar 13 '13 at 07:30
  • And yet, Default is correct. The name and ref attributes are mutually exclusive on every element in the XSD namespace that can carry name or ref attributes. Using 'name' signals that this is a declaration (of an attribute, of an element, of a type, ...), and using 'ref' signals that this is a reference to something (attribute, element, type, ...) declared elsewhere. – C. M. Sperberg-McQueen Mar 13 '13 at 14:50

2 Answers2

4

It looks to me like DocTotal ought to be a type, not an element:

<xs:element name="totals" minOccurs="0" type="DocTotal"/>
..................................

<xs:complexType name="DocTotal">
    <xs:sequence>
      <xs:element name="totalQty" minOccurs="0" type="xs:decimal"/>
      <xs:element name="totalTax" minOccurs="0" type="xs:decimal"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>

If you want to define the structure of an element somewhere (but not its name), and reference it elsewhere, it should be a type.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
1

Your schema is not valid because a ref attribute is not allowed on top-level element declarations.

This can be seen at least in the schema for schemas, which is a normative part of the W3C XML Schema recommendation. http://www.w3.org/TR/xmlschema-1/#normative-schemaSchema

The ref attribute is used to refer to global (=defined on top level) elements, types, attributes, groups etc. If you want to define globally two elements that have the same type, but different name, you could declare a global (named) type and then refer to that type in the element declaration. This is done by using the type attribute, like @Damien_The_Unbeliever did in their answer.

jasso
  • 13,736
  • 2
  • 36
  • 50