I was using javax.xml.validation.Validator.validate(StreamSource, StreamResult)
, and here are my xml and xsd:
xsd:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string" default="nobody"/>
</xs:sequence>
<xs:attribute name="color" type="xs:string" default="red" />
</xs:complexType>
</xs:element>
</xs:schema>
xml:
<note>
<to>you</to>
<from></from>
</note>
But what I got from StreamResult is something like this:
<?xml version="1.0" encoding="UTF-8"?>
<note color="red">
<to>you</to>
</from>
</note>
According to W3C XML Schema Part 0(see para4 of 2.2.1 Occurrence Constraints),
if the element appears without any content, the schema processor provides the element with a value equal to that of the default attribute.
Why is default attribute filled but not the empty element? How should I do to get the default value filled?