Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Schema: how to create element with default value

 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic