XML Instance document cannot detect attribute

Good Day guys, here's my problem:

I declared this global attribute in my schema document. By the way, I'm using VS .NET 2003.

<xs:attribute name="OutputId">

<xs:simpleType>

<xs:restriction base="xs:byte">

<xs:pattern value="[1-4]" />

</xs:restriction>

</xs:simpleType>

</xs:attribute>

I also declared this global element:

<xs:element name="Output">

<xs:complexType>

<xs:sequence>

<xs:element name="MinOutputVoltageLimit" type="xs:float" />

<xs:element name="NomOutputVoltageLimit" type="xs:float" />

<xs:element name="MaxOutputVoltageLimit" type="xs:float" />

<xs:element name="MaxLoadCurrent" type="xs:float" />

</xs:sequence>

<xs:attribute ref="mstns:OutputId" use="required" />

</xs:complexType>

</xs:element>

My schema namespace is "mstns" and somewhere in my schema document, I declared this:

<xs:element name="OutputSpecifications" minOccurs="1" maxOccurs="1">

<xs:complexType>

<xs:sequence>

<xs:element ref="mstns:Output" minOccurs="1" maxOccurs="4" />

</xs:sequence>

</xs:complexType>

<xs:key name="OutputKey">

<xs:selector xpath="OutputSpecifications" />

<xs:field xpath="@OutputId" />

</xs:key>

</xs:element>

Here is a part of my instance document:

<OutputSpecifications>

<Output OutputId="1">

<MinOutputVoltageLimit>3</MinOutputVoltageLimit>

<NomOutputVoltageLimit>4</NomOutputVoltageLimit>

<MaxOutputVoltageLimit>2</MaxOutputVoltageLimit>

<MaxLoadCurrent>1</MaxLoadCurrent>

</Output>

<Output OutputId="2">

<MinOutputVoltageLimit>3</MinOutputVoltageLimit>

<NomOutputVoltageLimit>4</NomOutputVoltageLimit>

<MaxOutputVoltageLimit>2</MaxOutputVoltageLimit>

<MaxLoadCurrent>1</MaxLoadCurrent>

</Output>

<Output OutputId="3">

<MinOutputVoltageLimit>3</MinOutputVoltageLimit>

<NomOutputVoltageLimit>4</NomOutputVoltageLimit>

<MaxOutputVoltageLimit>2</MaxOutputVoltageLimit>

<MaxLoadCurrent>1</MaxLoadCurrent>

</Output>

<Output OutputId="4">

<MinOutputVoltageLimit>3</MinOutputVoltageLimit>

<NomOutputVoltageLimit>4</NomOutputVoltageLimit>

<MaxOutputVoltageLimit>2</MaxOutputVoltageLimit>

<MaxLoadCurrent>1</MaxLoadCurrent>

</Output>

</OutputSpecifications>

Below are the following errors I get. The pink-colored parts are where the editor detects the following errors:

D:\VS2003Apps\WinApps\XML\XML\default.xml(16): The 'OutputId' attribute is not declared. An error occurred at , (16, 15).
D:\VS2003Apps\WinApps\XML\XML\default.xml(22): The 'OutputId' attribute is not declared. An error occurred at , (22, 15).
D:\VS2003Apps\WinApps\XML\XML\default.xml(28): The 'OutputId' attribute is not declared. An error occurred at , (28, 15).
D:\VS2003Apps\WinApps\XML\XML\default.xml(34): The 'OutputId' attribute is not declared. An error occurred at , (34, 15).
D:\VS2003Apps\WinApps\XML\XML\default.xml(16): The required attribute 'http://tempuri.org/default.xsd:OutputId' is missing. An error occurred at , (16, 8).
D:\VS2003Apps\WinApps\XML\XML\default.xml(22): The required attribute 'http://tempuri.org/default.xsd:OutputId' is missing. An error occurred at , (22, 8).
D:\VS2003Apps\WinApps\XML\XML\default.xml(28): The required attribute 'http://tempuri.org/default.xsd:OutputId' is missing. An error occurred at , (28, 8).
D:\VS2003Apps\WinApps\XML\XML\default.xml(34): The required attribute 'http://tempuri.org/default.xsd:OutputId' is missing. An error occurred at , (34, 8).



Answer this question

XML Instance document cannot detect attribute

  • Sam2

    You need to change your <xs:key> constraint. First of all, you specify <outputSpecifications> element in the selector and it looks like what you want is <Output>. Second, you have to prefix the element name to indicate the appropriate namespace.

    <xs:key name="OutputKey">

    <xs:selector xpath="mstns:Output" />

    <xs:field xpath="@OutputId" />

    </xs:key>



  • MU786

    Good day,

    Sir, when I actually change this :

    <xs:attribute name="OutputId">

    <xs:simpleType>

    <xs:restriction base="xs:byte">

    <xs:pattern value="[1-4]" />

    </xs:restriction>

    </xs:simpleType>

    </xs:attribute>

    to this :

    <xs:attribute name="OutputId" form="unqualified">

    <xs:simpleType>

    <xs:restriction base="xs:byte">

    <xs:pattern value="[1-4]" />

    </xs:restriction>

    </xs:simpleType>

    </xs:attribute>

    I get the error : C:\VS2003Apps\WinApps\XML\XML\default.xsd(79): The 'form' attribute cannot be present. An error occurred at C:\VS2003Apps\WinApps\XML\XML\default.xsd, (79, 3).


  • r3n

    The .NET 2.0 schema processor expects the keyref to be defined on a element that is a descendant or self of the element on which the key is defined. ie, the keyref should be within the scope of the key. Hence the second sample you posted, where the key is defined within SomeElementA and the keyref is defined within SomeElementB will not work.

    For it to work, you can move both the key and keyref to the common ancestor of SomeElementA and SomeElementB.

    Thanks,

    Priya


  • Nick Mc

    Try whether using

    <xs:attribute name="OutputId" form="unqualified">

    helps, but then you need to make sure that anywhere you reference the attribute you use e.g.

    <xs:attribute ref="OutputId" use="required" />

    That way your XML instance document can use the name OutputId in no namespace.

    If you want to use the schema you have unchanged then the instance document has to do what the current schema requires, namely use an attribute with name OutputId in the target namespace of the schema e.g.

    <Output xmlns:pf="http://tempuri.org/default.xsd" pf:OutputId="4">

    Generally if you use XSD and want to define the normal attributes in no namespace then doing

    <xs:schema attributeFormDefault="unqualified"

    on the schema root element saves you from the trouble you encounter currently.



  • forrestcupp

    jcarlos.net wrote:

    I also need all of the OutputId's to be synchronized all throughout the document, such that for example, an OutputId of 1 and 2 on the <OutputSpecifications> element and an OutputId of 1, 2, and 3 on the <Outputs> element would result in an invalid document.

    Simply using

    <xs:attribute name="attribute-name" type="xs:ID"/>

    should do, although ID attribute values need to start with a letter and not a digit. But the restriction of xs:ID applies for the whole document.



  • Dustin_H

    So it means that any element/attribute declared as a direct child to the <xs:schema> element is by default qualified and unchangeable So what if I want to declare an attribute that is a direct child to the <xs:schema> element Can I still reference it in my elements Does that mean that my XML instance document have to use the attribute this way:

    <Output xmlns:pf="http://tempuri.org/default.xsd" pf:OutputId="4">

    The reason why I'm declaring a global attribute is that, I want to reference it in multiple elements. I thought it was for this purpose. So for example I have this in my document:

    <OutputSpecifications>

    <Output OutputId="1">

    <MinOutputVoltageLimit>3</MinOutputVoltageLimit>

    <NomOutputVoltageLimit>4</NomOutputVoltageLimit>

    <MaxOutputVoltageLimit>2</MaxOutputVoltageLimit>

    <MaxLoadCurrent>1</MaxLoadCurrent>

    </Output>

    <Output OutputId="2">

    <MinOutputVoltageLimit>3</MinOutputVoltageLimit>

    <NomOutputVoltageLimit>4</NomOutputVoltageLimit>

    <MaxOutputVoltageLimit>2</MaxOutputVoltageLimit>

    <MaxLoadCurrent>1</MaxLoadCurrent>

    </Output>

    </OutputSpecifications>

    I can also use that OutputId attribute in another element, somewhere in my document.

    <Outputs>

    <Output OutputId="1">

    <Shunt>

    <VoltageRating>1</VoltageRating>

    <CurrentRating>2</CurrentRating>

    <DasuChannel>102</DasuChannel>

    </Shunt>

    <VoltageChannel>103</VoltageChannel>

    </Output>

    <Output OutputId="2">

    <Shunt>

    <VoltageRating>1</VoltageRating>

    <CurrentRating>2</CurrentRating>

    <DasuChannel>102</DasuChannel>

    </Shunt>

    <VoltageChannel>103</VoltageChannel>

    </Output>

    </Outputs>

    I also need all of the OutputId's to be synchronized all throughout the document, such that for example, an OutputId of 1 and 2 on the <OutputSpecifications> element and an OutputId of 1, 2, and 3 on the <Outputs> element would result in an invalid document.


  • Lawrence 007

    so this means that I cannot declare 2 OutputId attributes with the same value of "1" in two or more different locations. I'm thinking of using the <keyref> attribute and refer to the first OutputId attribute in the <OutputSpecifications> element and just refer to it. The problem is, I think, <keyref> and <key> must be in the same location or level:

    <SomeElement>

    <key>

    :

    </key>

    <keyref (refer to the key)>

    :

    </keyref>

    </SomeElement>

    Is this possible

    <SomeElementA>

    <key>

    :

    </key>

    </SomeElementA>

    <SomeElementB>

    <keyref (refer to the key in SomeElementA)>

    :

    </keyref>

    </SomeElementB>


  • Alessandro Camargo

    Right, the problem is if you have an xs:attribute declaration on the top level (as a child of xs:schema) then you are not allowed to use the 'form' attribute, instead any attribute declared that way globally is in the namespace the schema targets.

    So if you want that attribute to be in no namespace but your schema has a target namespace for the elements then you can't use a global xs:attribute and reference that later, instead you need to have the xs:attribute declaration local to the element declaration e.g.

    <xs:element name="Output">

    <xs:complexType>

    <xs:sequence>

    <xs:element name="MinOutputVoltageLimit" type="xs:float" />

    <xs:element name="NomOutputVoltageLimit" type="xs:float" />

    <xs:element name="MaxOutputVoltageLimit" type="xs:float" />

    <xs:element name="MaxLoadCurrent" type="xs:float" />

    </xs:sequence>

    <xs:attribute name="OutputId" form="unqualified" use="required">

    <xs:simpleType>

    <xs:restriction base="xs:byte">

    <xs:pattern value="[1-4]" />

    </xs:restriction>

    </xs:simpleType>

    </xs:attribute>

    </xs:complexType>

    </xs:element>

    You might want to define the type of the attribute elsewhere of course and simply to <xs:attribute name="OutputId" form="unqualified" use="required" type="typeNameDefinedElsewhere"/>



  • XML Instance document cannot detect attribute