473,322 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Retain default attribute values after XSD Validation?

In my app I'm validating an XML file against an XSD which contains several
attribute default value specifications. I'm performing the validation via an
xml document load() using a ValidatingXMLReader. After loading the document
(and validating it) I assume that the resulting doc will (as appropriate)
have elements with attributes set to their default values in those cases
where there was nothing specified in the input XML file.

However, when I serialize the resulting doc using its WriteContentTo method,
the default attributes are NOT represented in the XML output stream. Is this
expected behaviour and, if so, how can I arrange to retain these default
attribute values?

Thanks in advance,
Bill
Nov 12 '05 #1
2 3930
"Bill Cohagan" <bi**@teraXNOSPAMXquest.com> wrote in message news:uT**************@TK2MSFTNGP11.phx.gbl...
In my app I'm validating an XML file against an XSD which contains several
attribute default value specifications. : : when I serialize the resulting doc using its WriteContentTo method,
the default attributes are NOT represented in the XML output stream. Is this
expected behaviour and, if so, how can I arrange to retain these default
attribute values?


WriteContentTo( ) could have any of three motivations:

1.) It could generate an XML serialization preserving as much of the
original XML serialization as possible, including insignificant white
space and the quote characters delimiting each attribute value.

(I'd dismiss this as it imposes too heavy of a burden to maintain
knowledge of the original serialization format. The XML Infoset
is unconcerned with these minutia which it sees as irrelevant
artifacts of the serialization. XmlDocument is a model of the
Infoset.)

2.) It could generate XML that is canonically equivalent to the
original serialization format. This disregards irrelevant minutia,
such as the quantity of whitespace between attributes in an
element tag. It preserves those details of importance when
two XML documents must be compared for equivalence; which
has important applications in XML Encryption and XML Digital
Signatures.

3.) It could produce the Post Schema Validation Infoset (or PSVI),
if available. The PSVI contains value-added information from the
schema validation process, such as default attribute values, that
were not present in the original document. While additive, this
creates a document which is not equivalent to the original source,
and under some circumstances might be undesirable.

As one example of where producing the PSVI could be a mistake,
suppose I have an XML document with a child element, Account.
Account may have an attribute, MaxFDICInsuredBalance, defined
in the XML Schema as having the default value of $100,000.00.
Being a bank, I'll want to digitally sign the Account element to both
preserve its integrity (detect any alteration) and authenticate myself
(or my back-end process) as being the last person to modify it.

What happens if I schema-validate this digitally-signed document
in a ValidatingXmlReader and then pass it along to another process
that verifies the authenticity of the digital signature? 'Lo and behold,
should WriteContentTo( ) produce a serialization based on the PSVI
rather than one thats canonically equivalent to the original signed
document, the Account element has been tampered with!

Looking at it more closely, the explicit content of the Account element
would've changed were a WriteContentTo( ) to serialize the PSVI.
It would now contain the implied attribute value of $100,000.00 for
MaxFDICInsuredBalance, whereas the Account element was signed
when that wasn't present.

If WriteContentTo( ) did intend to serialize the document in a canonically
equivalent representation of its original content (rather than the serialize
the document's PSVI including default attribute nodes), I imagine it would
employ the IsDefault property of XmlReader to do so.

This nugget may suggest an answer to your second question, how to retain
the default attribute values?

Try subclassing ValidatingXmlReader and override the virtual IsDefault
property to always return false.

- - - KeepPsviDefAttrs.cs
using System.Xml;
public class MyValidatingXmlReader : ValidatingXmlReader {
public override bool IsDefault { get { return false; } }
}
- - -

Then use this MyValidatingXmlReader when loading the XmlDocument.
Whenever WriteContentTo( ) asks whether a given attribute node should
be serialized because it was added as a default value, you can direct
WriteContentTo( ) to ignore this distinction (serialize all attributes whether
they are default or not).
HTH,

Derek Harmon
Nov 12 '05 #2
Derek
Good summary of the problem -- and good solution as well. Overriding the
isDefault method solves the problem.

Thanks again!

Bill

"Derek Harmon" <lo*******@msn.com> wrote in message
news:uA****************@tk2msftngp13.phx.gbl...
"Bill Cohagan" <bi**@teraXNOSPAMXquest.com> wrote in message news:uT**************@TK2MSFTNGP11.phx.gbl...
In my app I'm validating an XML file against an XSD which contains several attribute default value specifications.

: :
when I serialize the resulting doc using its WriteContentTo method,
the default attributes are NOT represented in the XML output stream. Is this expected behaviour and, if so, how can I arrange to retain these default
attribute values?


WriteContentTo( ) could have any of three motivations:

1.) It could generate an XML serialization preserving as much of the
original XML serialization as possible, including insignificant white
space and the quote characters delimiting each attribute value.

(I'd dismiss this as it imposes too heavy of a burden to maintain
knowledge of the original serialization format. The XML Infoset
is unconcerned with these minutia which it sees as irrelevant
artifacts of the serialization. XmlDocument is a model of the
Infoset.)

2.) It could generate XML that is canonically equivalent to the
original serialization format. This disregards irrelevant minutia,
such as the quantity of whitespace between attributes in an
element tag. It preserves those details of importance when
two XML documents must be compared for equivalence; which
has important applications in XML Encryption and XML Digital
Signatures.

3.) It could produce the Post Schema Validation Infoset (or PSVI),
if available. The PSVI contains value-added information from the
schema validation process, such as default attribute values, that
were not present in the original document. While additive, this
creates a document which is not equivalent to the original source,
and under some circumstances might be undesirable.

As one example of where producing the PSVI could be a mistake,
suppose I have an XML document with a child element, Account.
Account may have an attribute, MaxFDICInsuredBalance, defined
in the XML Schema as having the default value of $100,000.00.
Being a bank, I'll want to digitally sign the Account element to both
preserve its integrity (detect any alteration) and authenticate myself
(or my back-end process) as being the last person to modify it.

What happens if I schema-validate this digitally-signed document
in a ValidatingXmlReader and then pass it along to another process
that verifies the authenticity of the digital signature? 'Lo and behold,
should WriteContentTo( ) produce a serialization based on the PSVI
rather than one thats canonically equivalent to the original signed
document, the Account element has been tampered with!

Looking at it more closely, the explicit content of the Account element
would've changed were a WriteContentTo( ) to serialize the PSVI.
It would now contain the implied attribute value of $100,000.00 for
MaxFDICInsuredBalance, whereas the Account element was signed
when that wasn't present.

If WriteContentTo( ) did intend to serialize the document in a canonically
equivalent representation of its original content (rather than the

serialize the document's PSVI including default attribute nodes), I imagine it would
employ the IsDefault property of XmlReader to do so.

This nugget may suggest an answer to your second question, how to retain
the default attribute values?

Try subclassing ValidatingXmlReader and override the virtual IsDefault
property to always return false.

- - - KeepPsviDefAttrs.cs
using System.Xml;
public class MyValidatingXmlReader : ValidatingXmlReader {
public override bool IsDefault { get { return false; } }
}
- - -

Then use this MyValidatingXmlReader when loading the XmlDocument.
Whenever WriteContentTo( ) asks whether a given attribute node should
be serialized because it was added as a default value, you can direct
WriteContentTo( ) to ignore this distinction (serialize all attributes whether they are default or not).
HTH,

Derek Harmon

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark R | last post by:
I have one .asp page with a SELECT pulldown list on it and some INPUT fields. When SUBMIT is clicked the form data is submitted to that same page and validated. If INPUT fields are empty the asp...
4
by: blu4899 | last post by:
Hi, The Xerces XML parser is reading external DTD references in DOCTYPEs by default, but is not doing anything with them because validation is turned off by default. This is documented in...
1
by: UndoMiel | last post by:
Hi, I have a situation where i would like to validate the occurance of certain elements, based on the value of an attribute. What is the "best" way to handle such validations? I am fairly new...
4
by: Lénaïc Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, I've some namespace problems when defining default values for attributes. My problem seems to come from the fact that the attributes are...
12
by: Stefano | last post by:
Hi all, what is the correct use of the "default" attribute in XML Schema? For example: <xs:element name="myProperty" type="xs:string" default="myDefaultValue"/> What can I do with it? What...
44
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
3
by: Gary Wessle | last post by:
Hi the second argument in the functions below suppose to retain its value between function calls, the first does, the second does not and I would like to know why it doesn't? and how to make it...
1
by: Harinath | last post by:
Hi All, I am working on a portal using .net2.0. In one of my page, i have FileUpload control. I am doing some server side validation for form fields in general. if there is any validation...
2
by: Radu | last post by:
Hi. I have been working at home on a web project (VSNET 2005 SP1). Now I have brought the project at work, and I suddenly have plenty of warnings like: Validation (XHTML 1.0 Transitional) -...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.