can i use a qualifed attribute in a xs:field element

This should cover W3C XML Schema, Relax NG and DTD related problems.
darbouka_dribel
Posts: 2
Joined: Mon Mar 28, 2022 11:56 pm

can i use a qualifed attribute in a xs:field element

Post by darbouka_dribel »

I want to have an XSD schema that has an identity-constraint to ensure values in "code" attributes can be used as a key identity. the code attribute belong to a namesapce (code attribute must be qualified).

Code: Select all

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="test.org"
    elementFormDefault="qualified"  attributeFormDefault= 'qualified'
    xmlns:p = 'test.org'>
<xs:element name="list">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="person" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="first" type="xs:string"/>
                        <xs:element name="last" type="xs:string"/>
                    </xs:sequence>
                    <xs:attribute name="code" type="xs:string" use="required"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:key name="personKey">
        <xs:selector xpath="p:person"/>
        <xs:field xpath="@code"/>
    </xs:key>
</xs:element>
</xs:schema>
When i try to validate the following document XML against the above xsd file, I get this error:
cvc-identity-constraint.4.2.1.a: Element "list" has no value for the key "personKey".

Code: Select all

<list xmlns="test.org"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="test.org test.xsd">
    <person xmlns:p="test.org" p:code="code1">
        <first>first0</first>
        <last>last0</last>
    </person>
    <person xmlns:p="test.org" p:code="code2">
        <first>first1</first>
        <last>last1</last>
    </person>
</list>
tavy
Posts: 365
Joined: Thu Jul 01, 2004 12:29 pm

Re: can i use a qualifed attribute in a xs:field element

Post by tavy »

Hello,
You need to add also the prefix for the @code attribute. I think that the key definition should be something like this:

Code: Select all

 <xs:key name="personKey">
      <xs:selector xpath="p:person"/>
      <xs:field xpath="@p:code"/>
  </xs:key>
Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
darbouka_dribel
Posts: 2
Joined: Mon Mar 28, 2022 11:56 pm

Re: can i use a qualifed attribute in a xs:field element

Post by darbouka_dribel »

tavy wrote: Tue Mar 29, 2022 9:31 am Hello,
You need to add also the prefix for the @code attribute. I think that the key definition should be something like this:

Code: Select all

 <xs:key name="personKey">
      <xs:selector xpath="p:person"/>
      <xs:field xpath="@p:code"/>
  </xs:key>
thanks a lot, it works fine.
Best Regards,
Octavian
Post Reply