SV: [xsd-users] Setting xsi:nil=true

Steinar Rune Eriksen steinar at viz.no
Thu Jun 28 03:49:00 EDT 2007


Thank you for the feedback. 
 
I will look into the example for how to create custom validation.
 
 
My initial problem was however how to actually create documents with this attribute.
 
Example
 
<MyMessage>
   <ChildElement xsi:nil="true"/>
</MyMessage>
 
Looking at the <examples\cxx\tree\streaming\driver.cxx> I am constructing the XML by creating elements
 
MyMessage::ChildElement::type childElement("Content String");
(.. etc constructing the MyMessage with childElement in constructor)
 
If I wanted to specify a NULL value instead for ChildElement, this is where my problems are.  Is there anyway to get access to the xerces Dom model during construction of the tree? Then I could manually add the xsi:nil part.
 
I have tried the following:
 
MyMessage::ChildElement::type  childElement;

xercesc::DOMNode* n = childElement._node();

// FAILS here since n is NULL when returned from _node()

if(n->getNodeType() == xercesc::DOMNode::ELEMENT_NODE){

      xercesc::DOMElement* xelTest = (xercesc::DOMElement*)n;

      xelTest ->setAttribute(std::c2w("xsi:nil").c_str(), std::s2w("true").c_str());

}

 

________________________________

Fra: Boris Kolpackov [mailto:boris at codesynthesis.com]
Sendt: on 27.06.2007 13:48
Til: Steinar Rune Eriksen
Kopi: xsd-users at codesynthesis.com
Emne: Re: [xsd-users] Setting xsi:nil=true



Hi Steinar,

Steinar Rune Eriksen <steinar at viz.no> writes:

> I am trying to use cxx_tree to generate an element of the type, example
> <n1:MemAddress2 xsi:nil="true"/>  How do I do that?

Nillable elements are very rarely used (you are the first person who asked
for this) and as a result are not directly supported by XSD. There is,
however, a number of ways to work around this limitation.

The easy way is only suitable if the MemAddress2 can have an empty value.
In other words, the following is legal:

<n1:MemAddress2></n1:MemAddress2>

If that's the case then you can use the generated code as is. The
generated code will map both the empty value and the nil value cases
to the same instance with empty value (XSD-generated code simply
ignores the xsi:nil attribute). You can distinguish between the two
by either using the DOM association feature or using type customization,
as described below.

The more elaborate way, that will work for any type, is to customize
the generated type for MemAddress2 and to check for the xsi:nil
attribute in the parsing constructor. For more information on type
customization see:

http://wiki.codesynthesis.com/Tree/Customization_guide

For an idea about customizing the parsing constructor see the wildcard
example in examples/cxx/tree/custom/wildcard/.

hth,
-boris






More information about the xsd-users mailing list