Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8419555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:38:47+00:00 2026-06-10T02:38:47+00:00

javax.xml.validation.Validator has a method to validate and augment xml against a schema. Simplified xml:

  • 0

javax.xml.validation.Validator has a method to validate and augment xml against a schema.

Simplified xml:

<something>
  <sub1>false</sub1> <!-- Suppose sub1 is optional and may not be present in xml -->
  <sub2>false</sub2>
</something>

Simplified xsd:

<complexType name="something">
  <sequence>
    <element name="sub1" type="boolean" maxOccurs="1" minOccurs="0" default="false"/>
    <element name="sub2" type="boolean" maxOccurs="1" minOccurs="1"/>
  </sequence>
</complexType>

Simplified validation and augmentation code:

Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);

DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.parse(new File(xmlFile));
DOMSource input = new DOMSource(document);
DOMResult output = new DOMResult();
validator.validate(input, output);

Document result = (Document)output.getNode();

So, besides validating xml against the schema, it should also augment it and add any missing defaults (like sub1) and send that into output.

However, sub1 is not present in result when it is missing in xml.

Where am I off track here?

EDIT:

Ok, found the reason why sub1 is missing. But how can I ensure that sub1 is present in result even when it’s not in xml?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:38:49+00:00Added an answer on June 10, 2026 at 2:38 am

    This answer quotes the spec: augmentation applies default values to empty elements. It does NOT add elements that are not present in xml.

    In other words, augmentation by schema cannot guarantee that elements are present.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java, I can validate an XML document against an XSD schema using javax.xml.validation.Validator,
I'm using Java 5 javax.xml.validation.Validator to validate XML file. I've done it for one
I am validating an in-memory DOM object using the javax.xml.validation.Validator class against an XSD
I perform XSD XML validation using the following classes: import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import
I am using javax.xml.validation.Validator from SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) to perform XSD validation. I would like to
I am validating XML against a XSD. But my java validation error message is
I am using the following code to validate an an XML file against a
I'm trying to validate one xml that I create with a local schema, but
I'm trying to validade a XML against a W3C XML Schema. The following code
Does Java XML Validation Support MTOM enabled Messages? The following error has been thrown

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.