1

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?

John Smith
  • 442
  • 1
  • 6
  • 15

1 Answers1

1

XSD is meant for Validation. It validates the structure and data of XML and let's you know if the XML is as expected or it's different than what's defined in Schema.

It's not to modify the XML data.

Refer this [click_here]

What you are looking for is XSLT. XSLT is meant for transforming an XML to XML (or HTML/Text) with customized hierarchy and data.

Or you can also modify the source XML by using DOM concept from host code like .Net C# or Java.

Or the other way is to treat XML as string and use string manipulation technique. But this method is not recommended

Community
  • 1
  • 1
Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114
  • Thanks for your answer. But if it's not to modify the XML data, why default attribute is filled, and even the empty element `` is modified to ``. Anything else I should do to the XSD settings? – John Smith Mar 14 '14 at 01:53
  • I think it's `` not ``. Most of the XML parsers convert `` to `` which is one other the same ie, Null tag.. There's no difference between these two.. – Rookie Programmer Aravind Mar 14 '14 at 07:32
  • And what my point is .. XSD doesn't change anything, XSD is just another XML which is used for validation. It's the host program which I think is Java in your case is making the difference.. – Rookie Programmer Aravind Mar 14 '14 at 07:33