• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Schema: how to create element with default value

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
reply
    Bookmark Topic Watch Topic
  • New Topic