You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by praetorian20 <as...@gmail.com> on 2010/07/15 06:42:58 UTC

Get default value for element from schema using xerces (C++)

Hi,
Say I have a schema which defines an element as follows:

    <xsd:element name="Widget" type="tns:WidgetType" />
    
    <xsd:complexType name="WidgetType">
      <xsd:sequence>

        <xsd:element name="Name" type="xsd:normalizedString" maxOccurs="1"
minOccurs="1" />
        <xsd:element name="Description" type="xsd:normalizedString"
default="Unknown" maxOccurs="1" minOccurs="0" />

      </xsd:sequence>
    </xsd:complexType>

I'm parsing (DOM parser) an XML file that has been validated against this
schema using Xerces-C++. If the `Description` element is present, I know how
to read it by iterating through all the child elements of the `DOMElement`
for a given `Widget` and using `DOMElement::getTextContent()` upon finding
the `Description` element.

But, if a particular `Widget` element does not have a `Description` child
element (which is allowed by the schema), how can I fetch the default value
(`Unknown`) from the schema?

Thanks for your responses,
Ashish
-- 
View this message in context: http://old.nabble.com/Get-default-value-for-element-from-schema-using-xerces-%28C%2B%2B%29-tp29169204p29169204.html
Sent from the Xerces - C - Users mailing list archive at Nabble.com.


Re: Get default value for element from schema using xerces (C++)

Posted by praetorian20 <as...@gmail.com>.


Boris Kolpackov-2 wrote:
> 
> Hi,
> 
> praetorian20 <as...@gmail.com> writes:
> 
>>         <xsd:element name="Description" type="xsd:normalizedString"
>> default="Unknown" maxOccurs="1" minOccurs="0" />
>>
>> I'm parsing (DOM parser) an XML file that has been validated against this
>> schema using Xerces-C++. If the `Description` element is present, I know
>> how
>> to read it by iterating through all the child elements of the
>> `DOMElement`
>> for a given `Widget` and using `DOMElement::getTextContent()` upon
>> finding
>> the `Description` element.
>> 
>> But, if a particular `Widget` element does not have a `Description` child
>> element (which is allowed by the schema), how can I fetch the default
>> value
>> (`Unknown`) from the schema?
> 
> Xerces-C++ XML Schema processor has support for default attribute/element
> values in that it will supply the default values as if they were specified
> in the XML being parsed.
> 
> The problem with your situation is that you are using default elements
> incorrectly. In XML Schema an element takes the default value if it is
> present but has empty value, not if is is not present (which is how
> attributes work). In other words:
> 
> <outer>
>   <Description>foo</Description> <!-- value 'foo' -->
> <outer>
> 
> <outer>
>   <Description></Description>    <!-- value 'Unknown' (default) -->
> <outer>
> 
> <outer>
>                                  <!-- Description element not present -->
> <outer>
> 
> Boris
> 
> -- 
> Boris Kolpackov, Code Synthesis       
> http://codesynthesis.com/~boris/blog
> Open-source XML data binding for C++  
> http://codesynthesis.com/products/xsd
> XML data binding for embedded systems 
> http://codesynthesis.com/products/xsde
> Command line interface to C++ compiler
> http://codesynthesis.com/projects/cli
> 
> 

Thanks Boris! I just tried that out and it works just like you said.
However, is there any way to do it the way I described? 

Is there some way to get the "WidgetType" definition from the schema and
iterate through it to find the default value for an element that is allowed
to be absent from a sequence?

Regards,
Ashish.
-- 
View this message in context: http://old.nabble.com/Get-default-value-for-element-from-schema-using-xerces-%28C%2B%2B%29-tp29169204p29187914.html
Sent from the Xerces - C - Users mailing list archive at Nabble.com.


Re: Get default value for element from schema using xerces (C++)

Posted by Boris Kolpackov <bo...@codesynthesis.com>.
Hi,

praetorian20 <as...@gmail.com> writes:

>         <xsd:element name="Description" type="xsd:normalizedString"
> default="Unknown" maxOccurs="1" minOccurs="0" />
>
> I'm parsing (DOM parser) an XML file that has been validated against this
> schema using Xerces-C++. If the `Description` element is present, I know how
> to read it by iterating through all the child elements of the `DOMElement`
> for a given `Widget` and using `DOMElement::getTextContent()` upon finding
> the `Description` element.
> 
> But, if a particular `Widget` element does not have a `Description` child
> element (which is allowed by the schema), how can I fetch the default value
> (`Unknown`) from the schema?

Xerces-C++ XML Schema processor has support for default attribute/element
values in that it will supply the default values as if they were specified
in the XML being parsed.

The problem with your situation is that you are using default elements
incorrectly. In XML Schema an element takes the default value if it is
present but has empty value, not if is is not present (which is how
attributes work). In other words:

<outer>
  <Description>foo</Description> <!-- value 'foo' -->
<outer>

<outer>
  <Description></Description>    <!-- value 'Unknown' (default) -->
<outer>

<outer>
                                 <!-- Description element not present -->
<outer>

Boris

-- 
Boris Kolpackov, Code Synthesis        http://codesynthesis.com/~boris/blog
Open-source XML data binding for C++   http://codesynthesis.com/products/xsd
XML data binding for embedded systems  http://codesynthesis.com/products/xsde
Command line interface to C++ compiler http://codesynthesis.com/projects/cli

Re: Get default value for element from schema using xerces (C++)

Posted by deloptes <de...@yahoo.com>.
praetorian20 wrote:

> 
> Hi,
> Say I have a schema which defines an element as follows:
> 
>     <xsd:element name="Widget" type="tns:WidgetType" />
>     
>     <xsd:complexType name="WidgetType">
>       <xsd:sequence>
> 
>         <xsd:element name="Name" type="xsd:normalizedString" maxOccurs="1"
> minOccurs="1" />
>         <xsd:element name="Description" type="xsd:normalizedString"
> default="Unknown" maxOccurs="1" minOccurs="0" />
> 
>       </xsd:sequence>
>     </xsd:complexType>
> 
> I'm parsing (DOM parser) an XML file that has been validated against this
> schema using Xerces-C++. If the `Description` element is present, I know
> how to read it by iterating through all the child elements of the
> `DOMElement` for a given `Widget` and using `DOMElement::getTextContent()`
> upon finding the `Description` element.
> 
> But, if a particular `Widget` element does not have a `Description` child
> element (which is allowed by the schema), how can I fetch the default
> value (`Unknown`) from the schema?
> 
> Thanks for your responses,
> Ashish

Hi, I have almost same question, except that my XSD is using a lot of
namespaces. So thee is one element <layer> that is defined in different
namespaces to have different attributes.

I think we need an example how to handle xsd files.

<xsd:element name="a:layer" type="xsd:normalizedString" maxOccurs="1"
> minOccurs="1" />

<xsd:element name="b:layer" type="xsd:normalizedString" maxOccurs="1"
> minOccurs="1" />

<xsd:element name="c:layer" type="xsd:normalizedString" maxOccurs="1"
> minOccurs="1" />

????

thanks