2

I'm creating an XML file where I would like to store my changes of a project just for fun purposes. And therefore I have written a small XML schema. The problem is that I want that each Revision's id attribute to be unique.

So I have searched through the Internet and Stack Overflow but I could not fix this problem. I'm using Visual Studio 2015 - not sure if this would be a problem.

The XML Schema that I'm using is:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
    <xs:element name="Revisions">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Revision" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Author" type="xs:string"/>
                            <xs:element name="Date" type="xs:date"/>
                            <xs:element name="Comments" type="xs:string"/>
                        </xs:sequence>
                        <xs:attribute name="id" type="xs:string" use="required"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
        <xs:unique name="Revision">
            <xs:annotation>
                <xs:documentation>
                    The id of each Revision element must be unique.
                </xs:documentation>
            </xs:annotation>
            <xs:selector xpath="Revision"/>
            <xs:field xpath="@id"/>
        </xs:unique>
    </xs:element>
</xs:schema>

And the XML file that I'm using:

<?xml version="1.0" encoding="utf-8"?>
<Revisions xmlns="http://tempuri.org/XMLSchema.xsd">
    <Revision id="1">
        <Author>JP</Author>
        <Date>2014-07-09</Date>
        <Comments>Initial version</Comments>
    </Revision>
    <Revision id="2">
        <Author>JP</Author>
        <Date>2016-01-26</Date>
        <Comments>
            Created a RegistrationValidator class which uses regular expressions to check
            if usernames, passwords, email addresses, etc.. are in correct format
        </Comments>
    </Revision>
    <Revision id="3">
        <Author>JP</Author>
        <Date>2016-08-14</Date>
        <Comments>Created an XML schema which validates this XML file</Comments>
    </Revision>
    <Revision id="3">
        <Author>Test</Author>
        <Date>2016-08-14</Date>
        <Comments>dummy comments</Comments>
    </Revision>
</Revisions>

As you can see I've assigned the same id value to the last Revision-tags and I get no errors, warnings or messages from the error list. Does somebody know what I'm doing wrong?

1

1 Answer 1

0

You were very close. Just add the namespace prefix, mstns, to the xs:selector/@xpath to fix the problem:

<xs:unique name="Revision">
  <xs:annotation>
    <xs:documentation>
      The id of each Revision element must be unique.
    </xs:documentation>
  </xs:annotation>
  <xs:selector xpath="mstns:Revision"/>
  <xs:field xpath="@id"/>
</xs:unique>
1
  • Anonymous downvoter: Care to explain your discontent on this year old, accepted answer? Or are you afraid your objection won't withstand scrutiny if openly presented?
    – kjhughes
    Commented Aug 31, 2018 at 2:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.