0

I am pretty new to xsd. I have also found a lot of questions on the Q&A but non was able to solve my problem. I have an html file which I want to validate using xsd validation. The standard am using is http://www.ihe.net/uploadedFiles/Documents/Radiology/IHE_RAD_Suppl_MRRT.pdf. And it says that the way the html files are formatted, it should be possible to validate it using any xml validator. Note this question XSD for same tag different attribute names, is kind of close but not what i want. Here is the section of the html i want to validate

<!DOCTYPE html>
<html>
<head>
    <tittle>Hello World</title>
    <meta charset="UTF-8"/>
    <meta name="d1" content="c1"/>
    <meta name="d2" content="c2"/>
</head>
</html>

The standard says there must be exactly 1 meta tag with attribute charset, and zero or more with attributes name, and content. So i tried to make the following xsd files.

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="html">
        <xs:complextType>
            <xs:sequence>
                <xs:element name="title" type="xs:string"/>
                <xs:element name="meta">
                    <xs:complexType>
                        <xs:attribute name="charset" type="xs:string"/>
                    </xs:complexType>
                </xs:element>
                <!-- obviously this wouldn't work -->
                <xs:element name="meta" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:attribute name="name" type="xs:string"/>
                        <xs:attribute name="content" type="xs:string"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Obviously the schema above will not work due to the repetition of the meta element. I get a mutliple elements error. So i tried to rap the other meta tags in a group element like below, but even that did not work.

    <xs:group name="otherMetatags">
        <xs:element name="meta" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
                <xs:attribute name="name" type="xs:string"/>
                <xs:attribute name="content" type="xs:string"/>
            </xs:complexType>
        </xs:element>
    </xs:group>
</xs:schema>

I know my two approaches seem to be stupid, but like I earlier said, am new to xsd.

Community
  • 1
  • 1
Stylishcoder
  • 1,142
  • 1
  • 11
  • 21

1 Answers1

0

In XSD 1.0, you cannot express the constraint that among a sequence of meta attributes, only one must have a charset attribute.

In XSD 1.1, you can do so using an assertion. See XSD: Restrict only one child element to have specific value for an example.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • there is a lot difference between the example you gave an the problem am facing. First, I am try to restrict one element to have a specific value but am trying to restrict 1 element to have a specific attribute and should not contain the attributes of the others – Stylishcoder Aug 04 '16 at 22:37
  • The point wasn't to provide you with an exact match but to show you the way to achieve your goal. Before either of us invest any more time in an assertion-based solution, please check that your validation tool supports XSD 1.1 -- many do not. Also, please [**accept**](http://meta.stackoverflow.com/q/5234/234215) some of the answers to your previous questions. Thanks. – kjhughes Aug 04 '16 at 22:55
  • thanks for your help. I am using standard java validation api to do my validation(specifically java8). About the questions, the answers there did not actually solve my problem or where not good solutions. I accepted 1 though. It was close to what I finally did. – Stylishcoder Aug 05 '16 at 06:58
  • You're not setup for XSD 1.1: See [How to validate XML against XSD 1.1 in Java?](http://stackoverflow.com/q/20807066/290085) – kjhughes Aug 05 '16 at 12:17