-1

What would be the correct XML Schema declaration for:

...<answersList>
    <question quest="Name?" cod="n_name">Variable Content</question>
    <question quest="Weight?" cod="n_weight">Variable Content</question>
</answersList>...

Every value for both attributes should be in the XSD enumeration.

So far I tried: (It doesen't work)

        <xs:element name="question">
            <xs:complexType>
              <xs:attribute name="quest">
                <xs:restriction base="xs:string">
                    <xs:enumeration value="Name?"/>
                    <xs:enumeration value="Weight?"/>
                </xs:restriction>
            </xs:attribute>
            <xs:attribute name="cod">
                <xs:restriction base="xs:string">
                    <xs:enumeration value="n_name"/>
                    <xs:enumeration value="n_weight"/>
                </xs:restriction>
            </xs:attribute>
          </xs:complexType>
        </xs:element>

Using this web to test: https://www.freeformatter.com/xml-validator-xsd.html

  • Either define a fixed value, or define an enumeration facet with only one value in the list. – Michael Kay Mar 16 '21 at 10:11
  • @MichaelKay I dont see how to do fixed values for a list. Im trying to do it with enumeration but it keeps failing for me :/ – David González Arias Mar 16 '21 at 13:58
  • And I'm afraid I just don't understand your question. I don't see any lists in your data, and and I can't see why you want quest to be a fixed value when your instance has two quest attributes with different values. I suspect you are using both the terms "list" and "fixed value" in a sense quite different from their actual meaning in the XSD specification, which doesn't aid communication. – Michael Kay Mar 16 '21 at 17:47
  • @MichaelKay Yes you are completly right. I wasn't even understanding what I wanted. Im trying to get an enumeration for 2 attributes for the tag question. – David González Arias Mar 17 '21 at 13:32
  • At the example the idea is that the tag "quest" only should validate if it contains "Name?" or Weight?. And the tag "cod" should only validate if it contains: "n_name" or "n_weight" – David González Arias Mar 17 '21 at 13:40
  • Right, I was coming to that conclusion. So, nothing to do with lists or fixed values. – Michael Kay Mar 17 '21 at 17:03

2 Answers2

1

In XML Schema, the name of a tag identifies its type and the type describes the allowed content. When a tag repeats, each occurrence has the same type.

So you cannot apply one set of rules to the attributes in //answersList/question[1] and a different set of rules to the attributes in //answersList/question[2].

kimbert
  • 2,376
  • 1
  • 10
  • 20
  • The idea I have is to apply the same rules for all questions, but different rules in attribute[1.1] and attribute[1.2]. Do you have any idea on how to make the concept xsd i did to work and validate? Im like 12h in and still havent been able to make it "compile" Thanks for answering! – David González Arias Mar 16 '21 at 15:37
  • It's not clear (to me, anyway) what you are trying to do. Your XSD snippet is clearly not valid because you do not have any simpleType definition for the attributes. Does that help at all? – kimbert Mar 16 '21 at 17:09
  • Yes my XSD is not valid. Im trying to get both attributes to have an enumeration for what they can store and what they cant. At the example the idea is that the tag quest only should validate if it contains "Name?" or Weight?. And the tag cod should only validate if it contains: "n_name" or "n_weight" – David González Arias Mar 17 '21 at 13:36
1

It seems to me that what you are actually looking for is cross-node constraints: "if X is A then Y must be B". That can be achieved using assertions in XSD 1.1; it can't be achieved in XSD 1.0.

You can constrain each of the attributes to a fixed set of values using an enumeration type (a simple type defined by restricting xs:string with an enumeration facet), and you can define cross-attribute constraints using assertions, provided that you use an XSD 1.1 processor.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164