josephino
josephino

Reputation: 360

xml - Empty String attribute value xsd

The attribute match may not always contain a value , it should allow Empty String :

<template mode="on" match="">
</template>

To validate that the previous code , I'm using the following xsd Here is my xsd :

<xs:element name="template">
    <xs:complexType>
        <xs:sequence>
            <xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
        </xs:sequence>
        <xs:attribute type="xs:string" name="mode" use="optional"/>
        <xs:attribute name="match" use="optional">
            <xs:simpleType>
                <xs:union memberTypes="xs:string emptyString"/>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
</xs:element>
<xs:simpleType name="emptyString">
        <xs:restriction base="xs:string">
            <xs:length value="0"/>
        </xs:restriction>
</xs:simpleType>

In the validation process against the xsd, I got the following error message : no viable alternative at input ' <EOF> ' .

The following code doesn't show me any error.

<template mode="on" match="aaa">
</template>

Any Idea how to solve this ?

Upvotes: 0

Views: 2219

Answers (1)

kjhughes
kjhughes

Reputation: 111521

Assuming that your complete XSD is as follows,

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="template">
    <xs:complexType>
      <xs:sequence>
        <xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
      </xs:sequence>
      <xs:attribute type="xs:string" name="mode" use="optional"/>
      <xs:attribute name="match" use="optional">
        <xs:simpleType>
          <xs:union memberTypes="xs:string emptyString"/>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="emptyString">
    <xs:restriction base="xs:string">
      <xs:length value="0"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

Then your XML is valid against your XSD, both with template/@match="" and with with template/@match="aaa". There is nothing wrong with either your XML or your XSD.

Note, though, that a xs:string can already be empty without the extra xs:union with emptyString part.

Note, too, that your error message does not look like one generated by common validating XML/XSD parsers. I suspect that your message is actually originating from another program, or that you've failed to produce a true MCVE of your issue.

Upvotes: 1

Related Questions