aspose file tools*
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
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
Author

Schema: how to create element with default value

Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
Hi,

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:

<myelement1>1</myelement1>
<myelement2>2</myelement2>

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
<myelement1>1</myelement1>
<myelement2>2</myelement2>
<myelement3>3</myelement3>

Thank you,
Vladas

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
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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:

Name,
Type,
Short Description,
Long Description,
Validation rules...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Schema: how to create element with default value