/// <summary> /// Validate compatibility rule attributes - Ignorable, ProcessContent, PreserveElements, PreserveAttributes, MustUnderstand. /// </summary> /// <param name="validationContext">The validation context.</param> internal static void ValidateMcAttributes(ValidationContext validationContext) { var element = validationContext.Element; if (element.MCAttributes == null) { return; } HashSet <string> ignorableNamespaces = null; ValidationErrorInfo errorInfo; if (element.MCAttributes != null) { // validate Ignorable attribute if (!string.IsNullOrEmpty(element.MCAttributes.Ignorable)) { ignorableNamespaces = new HashSet <string>(); // rule: the prefix must already be defined. var prefixes = new ListValue <StringValue>(); prefixes.InnerText = element.MCAttributes.Ignorable; foreach (var prefix in prefixes.Items) { var ignorableNamespace = element.LookupNamespace(prefix); if (string.IsNullOrEmpty(ignorableNamespace)) { // error, the prefix is not defined. errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidIgnorableAttribute", element.MCAttributes.Ignorable); validationContext.EmitError(errorInfo); } else { ignorableNamespaces.Add(ignorableNamespace); } } } // validate PreserveAttributes attribute if (!string.IsNullOrEmpty(element.MCAttributes.PreserveAttributes)) { // The ProcessAttributes attribute value shall not reference any attribute name that does not belong to a namespace // that is identified by the Ignorable attribute of the same element. if (ignorableNamespaces == null) { // must have Ignorable on same element. errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidPreserveAttributesAttribute", element.MCAttributes.PreserveAttributes); validationContext.EmitError(errorInfo); } else { string errorQName = ValidateQNameList(element.MCAttributes.PreserveAttributes, ignorableNamespaces, validationContext); if (!string.IsNullOrEmpty(errorQName)) { errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidPreserveAttributesAttribute", element.MCAttributes.PreserveAttributes); validationContext.EmitError(errorInfo); } } } // validate PreserveElements attribute if (!string.IsNullOrEmpty(element.MCAttributes.PreserveElements)) { // The ProcessAttributes attribute value shall not reference any attribute name that does not belong to a namespace // that is identified by the Ignorable attribute of the same element. if (ignorableNamespaces == null) { // must have Ignorable on same element. errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidPreserveElementsAttribute", element.MCAttributes.PreserveElements); validationContext.EmitError(errorInfo); } else { string errorQName = ValidateQNameList(element.MCAttributes.PreserveElements, ignorableNamespaces, validationContext); if (!string.IsNullOrEmpty(errorQName)) { errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidPreserveElementsAttribute", element.MCAttributes.PreserveElements); validationContext.EmitError(errorInfo); } } } // validate ProcessContent attribute if (!string.IsNullOrEmpty(element.MCAttributes.ProcessContent)) { // The ProcessAttributes attribute value shall not reference any attribute name that does not belong to a namespace // that is identified by the Ignorable attribute of the same element. if (ignorableNamespaces == null) { // must have Ignorable on same element. errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidProcessContentAttribute", element.MCAttributes.ProcessContent); validationContext.EmitError(errorInfo); } else { string errorQName = ValidateQNameList(element.MCAttributes.ProcessContent, ignorableNamespaces, validationContext); if (!string.IsNullOrEmpty(errorQName)) { errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidProcessContentAttribute", element.MCAttributes.ProcessContent); validationContext.EmitError(errorInfo); } } foreach (var exAttribute in element.ExtendedAttributes) { // Markup consumers that encounter a non-ignored element that has an xml:lang or xml:space attribute and is also identified by a ProcessContent attribute value might generate an error. if (AlternateContentValidator.IsXmlSpaceOrXmlLangAttribue(exAttribute)) { // report error. errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidXmlAttributeWithProcessContent"); validationContext.EmitError(errorInfo); } } } if (!string.IsNullOrEmpty(element.MCAttributes.MustUnderstand)) { // TODO: MustUnderstand // A markup consumer that does not understand these identified namespaces shall not continue to process the markup document // rule: the prefix must already be defined. var prefixes = new ListValue <StringValue>(); prefixes.InnerText = element.MCAttributes.MustUnderstand; foreach (var prefix in prefixes.Items) { var mustunderstandNamespace = element.LookupNamespace(prefix); if (string.IsNullOrEmpty(mustunderstandNamespace)) { // report error, the prefix is not defined. errorInfo = validationContext.ComposeMcValidationError(element, "MC_InvalidMustUnderstandAttribute", element.MCAttributes.MustUnderstand); validationContext.EmitError(errorInfo); } } } } }
/// <summary> /// Only validation whether the children elements are valid according to this type's constraint defined in schema. /// </summary> /// <param name="validationContext">The validation context.</param> internal void Validate(ValidationContext validationContext) { Debug.Assert(validationContext != null); Debug.Assert(validationContext.Element != null); OpenXmlElement theElement = validationContext.Element; Debug.Assert(!(theElement is OpenXmlUnknownElement)); Debug.Assert(!(theElement is OpenXmlMiscNode)); if (theElement.ElementTypeId < ReservedElementTypeIds.MaxReservedId) { // MiscElement, UnknownElement, // AlternateContent, AlternateContentChoice, AlternateContentFallback if (theElement.ElementTypeId == ReservedElementTypeIds.AlternateContentId) { AlternateContentValidator.Validate(validationContext); } Debug.Assert(!(theElement is AlternateContentChoice)); Debug.Assert(!(theElement is AlternateContentFallback)); return; } // validte Inorable, ProcessContent, etc. compatibility-rule attributes CompatibilityRuleAttributesValidator.ValidateMcAttributes(validationContext); SchemaTypeData schemaTypeData = this._sdbSchemaDatas.GetSchemaTypeData(theElement); ValidateAttributes(validationContext, schemaTypeData); // validate particles if (theElement is OpenXmlLeafTextElement) { SimpleContentComplexTypeValidator.Validate(validationContext, schemaTypeData.SimpleTypeConstraint); } else if (theElement is OpenXmlLeafElement) { EmptyComplexTypeValidator.Validate(validationContext); } else { Debug.Assert(theElement is OpenXmlCompositeElement); Debug.Assert(!(theElement is AlternateContentChoice)); Debug.Assert(!(theElement is AlternateContentFallback)); if (schemaTypeData.ParticleConstraint != null) { // composite element CompositeComplexTypeValidator.Validate(validationContext, schemaTypeData.ParticleConstraint); } else { // special case, see O14 bug #662644 // A root element which does not allow any children. Debug.Assert(theElement is OpenXmlPartRootElement); EmptyRootComplexTypeValidator.Validate(validationContext); } } }