50558

XSD make attribute nullable

Question:

When creating the XML the attribute wait may not always contain a value. How can I edit the schema so it allows the attribute wait to contain either a number or no value?

<xs:complexType name="CommandType"> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute type="xs:string" name="exe" use="required" /> <xs:attribute type="xs:string" name="args" use="required" /> <xs:attribute type="xs:int" name="wait" use="required" /> <xs:attribute type="xs:string" name="expectedOutput" use="required" /> <xs:attribute type="xs:string" name="toVariable" use="required" /> </xs:extension> </xs:simpleContent> </xs:complexType>

I have tried doing both these nillable="true" xsi:nil="true" but they don't work. When I tried to validate the XSD I got errors.

Answer1:

"nillable" only works for elements, not for attributes - and even then it's not very useful because if the element is empty you have to add xsi:nil="true", which is completely redundant.

Either (a) define a type that's a union of xs:integer and a zero-length string, as suggested by IMSoP, or (b) define a list type with item type integer, minLength 0, maxLength 1. I prefer the latter solution as it plays better with schema-aware XSLT and XQuery.

Answer2:

There may be an easier way, but perhaps you could create a custom type that was either an empty string or conformed to the definition of xs:int using a union type:

<xs:simpleType name="emptyString"> <xs:restriction base="xs:string"> <xs:length value="0" /> </xs:restriction> </xs:simpleType> <xs:simpleType name="intOrEmpty"> <xs:union memberTypes="xs:int emptyString" /> </xs:simpleType>

Incidentally, it's worth remembering that the xs:string type includes empty strings, so if, say, the exe attribute should always have a non-empty value, you need a nonEmptyString type (using a minLength restriction) as well as marking it as required. I know that's caught me out in the past.

Recommend

  • JAXB add additional static root element
  • traversing a BPEL array
  • SessionInformation expiration doesn't lead to user log out from system
  • Reading contents of a managed bean with reflection in a JSF application
  • Remove previous Directions Route Google Maps
  • Angular - How to access and replace innerHTML from a directive
  • How to align an image side by side with a heading element?
  • CSS Grid, position absolute an element in a css grid item: IMPOSSIBLE
  • getelement by class name for clicking
  • how to ignore xml namespaces?
  • Should a web service response include empty values?
  • Python/Javascript: WYSIWYG html editor - Handle large documents fast and/or design theory
  • Is there a package like bigmemory in R that can deal with large list objects?
  • Knockout custom binding handler
  • Why isn't obj.style.left = “200px”; working in this code?
  • CXF JAXB JAXBEncoderDecoder unmarshalling error : unexpected element when having qualified elements
  • Hide HTML elements without javascript, only CSS
  • Python pickle not one-to-one: different pickles give same object
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Row Count Is Returning the incorrect number using RaptureXML
  • Spring Data JPA custom method causing PropertyReferenceException
  • Obtain ObjectIdHex value from mgo query
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Weird JavaScript statement, what does it mean?
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?