简体   繁体   中英

DTD Validation #Required Field and empty string

I'm doing XML validation in our system, and I've already achieved this thru XmlReader, But I want to validate a #Required Attribute with EMPTY STRING value. Is there a way to achieve this in DTD Attribute?

DTD:

<!ATTLIST TEST
   pUsername CDATA #REQUIRED (...SHOULD NOT BE AN EMPTY STRING)
   pTestAttrib CDATA #REQUIRED (...SHOULD NOT BE AN EMPTY STRING)
   >

XML:

<TEST pUsername="" pTestAttrib=""> (I want to validate if this attribute value is empty string)

C#: Pseudo code

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += delegate (object sender, ValidationEventArgs args) { messageBuilder.AppendLine(args.Message); };
settings.ValidationType = ValidationType.DTD;
settings.DtdProcessing = DtdProcessing.Parse;
using (XmlReader reader = XmlReader.Create(stream, settings))
{
    do
    {
    }
    while (reader.Read());
}

You can declare the attributes as NMTOKEN or NMTOKENS , but the values for those attributes will need to only consist of NameChar 's.

Not sure if that will work for all possible values coming from the 3rd party system or not.

If not, you're stuck as far as DTD changes go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM