This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes optional attributes with defaults vs. length-restriction->cvc-length-valid: Value '' with length = Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "optional attributes with defaults vs. length-restriction->cvc-length-valid: Value Watch "optional attributes with defaults vs. length-restriction->cvc-length-valid: Value New topic
Author

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

oli mueller
Ranch Hand

Joined: Feb 13, 2011
Posts: 42
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

Joined: Feb 13, 2011
Posts: 42
just for completeness: I am using JAXB...does anybody know how to validate attributes with defaults???.........

THANKS for your answers....
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
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

Joined: Feb 13, 2011
Posts: 42
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: optional attributes with defaults vs. length-restriction->cvc-length-valid: Value '' with length =
 
Similar Threads
validate my XSD code
xml:lang and XML Schema
POJO not generating for the xsd which contains enumeration list with jaxb tool. Its urgent please
WDSL2Java using Axis2 via Ant in Eclipse
Problem related to XML validation using XSD