• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

optional attributes with defaults vs. length-restriction->cvc-length-valid: Value '' with length =

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

in my xsd i have declared that an string attribute has a default value "a". Additionally I have set its length-restriction to "1". :



My understanding of this would be: "The attribute test is optional. If it is not existent in the xml, the JAVA-Pojo object will be initialised with "a" (or at least will return "a" when getTest() is called). If the attribute test is existent, then it must have the lenght=1" CORRECT??

However, when unmarshalling an xml where this attribute is not existent, I get the following error:

Caused by: org.xml.sax.SAXParseException: cvc-length-valid: Value '' with length = '0' is not facet-valid with respect to length '1' for type '#AnonType_testFoo'.

Can you help??...Apparently he complains because the (non-existing) attribute does not meet the length restriction of 1, but that i find astonishing?...I mean, it should only apply the length restriction if the xml actually contains the attribute, or not??...Well, it could also be that the default-value is not applied at all??-->why?? (the surrounding xml element is existent of course..)

Or in other words: How do i have to specify the xsd so that an attribute may be optional, but if it is existent certain length restrictions apply??...

THANKS a lot for your help.


 
oli mueller
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just for completeness: I am using JAXB...does anybody know how to validate attributes with defaults???.........

THANKS for your answers....
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My understanding of this would be: "The attribute test is optional. If it is not existent in the xml, the JAVA-Pojo object will be initialised with "a" (or at least will return "a" when getTest() is called). If the attribute test is existent, then it must have the lenght=1" CORRECT??


That is correct, though.

Or in other words: How do i have to specify the xsd so that an attribute may be optional, but if it is existent certain length restrictions apply??...


The xsd:attribute posted should do the job correctly. Though it is not of material help, I think I can assure you that if problem there is, it is not from that part.

In the xsd:attribute so posted, you've default=" " (I hope it is at least a space inside). As it is not what you said default to "a", I guess maybe you have to re-examine the block showing how the element carrying it or referring it would help.

Also make sure the jaxb be the one from glassfish or as a separate download (v2.2 or so) - as there is known to have problem of jwsdp v2.0 working together with jdk1.6 as some wrong version of xjc is mistakenly packed with the jaxb.

If there are no problem on the above, make a minimal application to assure for yourself the approach is correct. I've made one rad and the result delivers as advertised.
 
oli mueller
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for your answer.. Well, I have downloaded my jar from:http://jaxb.java.net/ and i am using jdk1.6.

With regards to the posted XSD:attribute....well, in my actual application, the default value is one empty space char " ", BUT I also tested it with default="a" with the same result...for the forum i thought it would be less confusing with default = "a", but apparently i forgot to type it...;)...

I will try a little sample application tomorrow and post this, hoping for help...

cioa.
 
They weren't very bright, but they were very, very big. Ad contrast:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic