File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Schema: how to create element with default value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Schema: how to create element with default value" Watch "Schema: how to create element with default value" New topic

Schema: how to create element with default value

Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385

In XML Schema the attribute "default" for the element doesn't create element when it's absent but only supplies value when it's empty. I need elements that do not exist in XML to be created by schema.
I have XML file:


And schema:

<xs:element name="myelement1" default="1" minOccurs="0"/>
<xs:element name="myelement2" default="2" minOccurs="0"/>
<xs:element name="myelement3" default="3" minOccurs="0"/>

I want result to be

Thank you,

P.S. I am not sure if schema will do that for me. As I found out XML schemas and XSL Transformations are somewhat limited and sometimes you have to code some transformation i.e. with Java. I hope this not the case
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Validating an XML document against a schema shouldn't modify the document in any way. The default attribute being present in a schema is a note to the programmer that the application processing instances of that schema will interpret a missing value as the given default value. If you want to replace such missing values/elements, I'm afraid you'll have to do it yourself (using XSLT or Java's DOM API, for example).

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
I heard that XSLT 2.0 works with types and schemas. Maybe I can use XSLT to "create" missing elements. I have configuration file which may have or not have some values. If it does not have required element entry it should create one based on default value. I am using XSD to specify ALL the elements. Can I walk XML and XSD with XSLT and create resulting XML based not on what entries are in config XML but rather on which entries are described in XSD? I.e. template match by type.

Another idea is to parse not XML but XSD itself.. I do hesitate to put configuration entries as attributes and not as elements because in my opinion it will make larger XSD and will reduce configuration file template readability.

Best regards!
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
I dont have much experience with XML yet but as I understand:

I make all configuration file as single element and all it's parameters as attributes. In this case missing attributes can be created using "default". But attributes can be only simpleType and hmm each configuration entry is:

Short Description,
Long Description,
Validation rules...
I agree. Here's the link:
subject: Schema: how to create element with default value
It's not a secret anymore!