Applying schema and default attributes

V

Venu

This is my simple schema
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" attributeFormDefault="unqualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Application">
<xs:complexType>
<xs:sequence>
<xs:element name="NotesList">
<xs:complexType>
<xs:sequence>
<xs:element name="Note">
<xs:complexType>
<xs:attribute name="SectionID" type="xs:integer" default="1"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


------------------------------------------------------------------------------------

And here sample XML file…
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<Application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Nodes.xsd">
<NotesList>
<Note />
</NotesList>
</Application>

------------------------------------------------------------------------------------

My question is, in the XML file the attribute of “Note†element is not
supplied, since in the xsd the attribute has the default value as “1†- When
the schema is applied to given XML, will this copy the “default†from Schema
to XML ??

If so
1) How to apply the XSD to XML so that attribute is copied??
2) How can view the content of XML after schema is applied (just to make
sure that attributes are copied)


I’m a new to this area, please can you help…

Thanks in advance..

Cheers
Venu
 
N

Nicholas Paldino [.NET/C# MVP]

Venu,

I think that you are a little bit confused. An XSD (or schema) is used
to validate the contents of an XML document. It defines how the XML
document must be structured.

If the XML document does not have an attribute that the schema defines,
then when validating against that schema, it will fail.

If you want to transform one XML document to another, look into XSLT
stylesheets, which you should be able to find classes to work with in the
System.Xml.Xsl namespace in .NET.

What you will need to do is create an XSLT stylesheet which will apply
the attribute. Either that, or you could load the XML into an XmlDocument
instance and then add the attribute yourself.
 
V

Venu

Nicholas,

i'm bit confused now by your statement....

I understand XSD is used to validate the XML, but when a 'default' value is
specified for an 'attribute' in XSD, how does this work (and for what this is
used)??

i have extracted below statements from W3C website about the 'default' of an
'attribue'.. i just want to understand how to implement this...

-----------------------------------------------------------------------------
http://www.w3.org/TR/xmlschema-1/#cAttribute_Declarations

{value constraint} reproduces the functions of XML 1.0 default and #FIXED
attribute values. default specifies that the attribute is to appear
unconditionally in the ·post-schema-validation infoset·, with the supplied
value used whenever the attribute is not actually present; fixed indicates
that the attribute value if present must equal the supplied constraint value,
and if absent receives the supplied value as for default. Note that it is
values that are supplied and/or checked, not strings
--------------------------------------------------------------------------------

Thanks in adv.

Cheers,
Venu


Nicholas Paldino said:
Venu,

I think that you are a little bit confused. An XSD (or schema) is used
to validate the contents of an XML document. It defines how the XML
document must be structured.

If the XML document does not have an attribute that the schema defines,
then when validating against that schema, it will fail.

If you want to transform one XML document to another, look into XSLT
stylesheets, which you should be able to find classes to work with in the
System.Xml.Xsl namespace in .NET.

What you will need to do is create an XSLT stylesheet which will apply
the attribute. Either that, or you could load the XML into an XmlDocument
instance and then add the attribute yourself.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Venu said:
This is my simple schema
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Application">
<xs:complexType>
<xs:sequence>
<xs:element name="NotesList">
<xs:complexType>
<xs:sequence>
<xs:element name="Note">
<xs:complexType>
<xs:attribute name="SectionID" type="xs:integer" default="1"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


------------------------------------------------------------------------------------

And here sample XML file…
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<Application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Nodes.xsd">
<NotesList>
<Note />
</NotesList>
</Application>

------------------------------------------------------------------------------------

My question is, in the XML file the attribute of “Note†element is not
supplied, since in the xsd the attribute has the default value as
1†- When
the schema is applied to given XML, will this copy the “default†from
Schema
to XML ??

If so
1) How to apply the XSD to XML so that attribute is copied??
2) How can view the content of XML after schema is applied (just to make
sure that attributes are copied)


I’m a new to this area, please can you help…

Thanks in advance..

Cheers
Venu
 
N

Nicholas Paldino [.NET/C# MVP]

Venu,

When you see "default" when it relates to the schema definition of an
attribute, it means that in the absence of that attribute value, that is the
default value.

However, I don't know of any XML parsers that when validating against a
schema, would inject the value in the event that it is missing. You would
have to go through the schema and get the default value, and then assign
that when you are processing your document, and see the attribute is
missing.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Venu said:
Nicholas,

i'm bit confused now by your statement....

I understand XSD is used to validate the XML, but when a 'default' value
is
specified for an 'attribute' in XSD, how does this work (and for what this
is
used)??

i have extracted below statements from W3C website about the 'default' of
an
'attribue'.. i just want to understand how to implement this...

-----------------------------------------------------------------------------
http://www.w3.org/TR/xmlschema-1/#cAttribute_Declarations

{value constraint} reproduces the functions of XML 1.0 default and #FIXED
attribute values. default specifies that the attribute is to appear
unconditionally in the ·post-schema-validation infoset·, with the supplied
value used whenever the attribute is not actually present; fixed indicates
that the attribute value if present must equal the supplied constraint
value,
and if absent receives the supplied value as for default. Note that it is
values that are supplied and/or checked, not strings.
--------------------------------------------------------------------------------

Thanks in adv.

Cheers,
Venu


Nicholas Paldino said:
Venu,

I think that you are a little bit confused. An XSD (or schema) is
used
to validate the contents of an XML document. It defines how the XML
document must be structured.

If the XML document does not have an attribute that the schema
defines,
then when validating against that schema, it will fail.

If you want to transform one XML document to another, look into XSLT
stylesheets, which you should be able to find classes to work with in the
System.Xml.Xsl namespace in .NET.

What you will need to do is create an XSLT stylesheet which will
apply
the attribute. Either that, or you could load the XML into an
XmlDocument
instance and then add the attribute yourself.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Venu said:
This is my simple schema
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Application">
<xs:complexType>
<xs:sequence>
<xs:element name="NotesList">
<xs:complexType>
<xs:sequence>
<xs:element name="Note">
<xs:complexType>
<xs:attribute name="SectionID" type="xs:integer" default="1"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


------------------------------------------------------------------------------------

And here sample XML file.
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<Application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Nodes.xsd">
<NotesList>
<Note />
</NotesList>
</Application>

------------------------------------------------------------------------------------

My question is, in the XML file the attribute of "Note" element is not
supplied, since in the xsd the attribute has the default value as
1" - When
the schema is applied to given XML, will this copy the "default" from
Schema
to XML ??

If so
1) How to apply the XSD to XML so that attribute is copied??
2) How can view the content of XML after schema is applied (just to
make
sure that attributes are copied)


I'm a new to this area, please can you help.

Thanks in advance..

Cheers
Venu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top