Personal website of Gregory K. Maxey, Commander USN (Retired)
Do you have ad-blocking software enabled? While I respect your right to do so, your donations and the minimal advertisements on this site help to defray internet and other costs of providing this content. Please consider excluding this website from blocking or turning off the blocker while browsing this site.
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
The purpose of this Microsoft Word Tips & Microsoft Word Help page is is to offer a basic demonstration of how you can validate mapped Content Control (CC) data entries with an attached XML Schema. For additional information on mapped Content Controls see my: Repeating Data (or populating fields).
The discussion offered is basic because the field of XML and its validation methods are fairly broad and complex and I only have a basic understanding of the subject myself. Hopefully you can build on what I have presented here to meet your specific needs.
The illustration below shows a document with two content controls. The second control has been mapped to a CustomXMLPart and validated with an attached XML schema. Note the red dashed outline around the placeholder text. This indicates that the content does not meet the validation requirements of the attached schema and the condition is flagged for the user.
Unfortunately the validation that you can perform using an attached XML schema is a lot like Caspar Milquetoast (i.e., very timid). It will flag invalid data for the user, but there doesn't seem to be any way to require validation before a user can save, print, or otherwise complete the document.
You can create a document with an attached schema by peforming the following steps:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <ccMap xmlns="http://ValidationDemo"> <ccElement_SexualContact></ccElement_SexualContact> </ccMap>
Note: My XML script is saved as D:\CC Validation Demo (XML).xml
<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ValidationDemo" xmlns="http://ValidationDemo" elementFormDefault="qualified"> <xs:element name="ccMap"> <xs:complexType> <xs:sequence> <xs:element name="ccElement_SexualContact"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Yes"/> <xs:enumeration value="No"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"></xs:any> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
1. The XSD file I used is saved on my PC as D:\CC Validation Demo (Schema).xsd
2. The following link will introduce you to a complete W3Schools tutorial on creating XSD files: XSD Tutorial
Note: The following VBA procedures can be used to perform steps 4 and 5.
Sub MapCC() Dim oCC As ContentControl Dim XPath As String LoadCXP Set oCC = ActiveDocument.SelectContentControlsByTitle("Sex?").Item(1) XPath = "/ns0:ccMap/ns0:ccElement_SexualContact" oCC.XMLMapping.SetMapping XPath End Sub Sub LoadCXP() Dim oCustXMLPart As CustomXMLPart ClearXMLParts 'Optional Set oCustXMLPart = ActiveDocument.CustomXMLParts.Add 'Be sure to use your own file name. oCustXMLPart.Load "D:\CC Validation Demo (XML).xml" End Sub Sub ClearXMLParts() Dim i As Long For i = ActiveDocument.CustomXMLParts.Count To 4 Step -1 ActiveDocument.CustomXMLParts(i).Delete Next i End Sub
See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.
- On the Developer tab, in the XML group, select "Schema."
-In the "Templates and Add-Ins" dialog, XML Schema tag and select "Add Schema."
-In the Add Schema dialog, select your saved schema file and click "Open."
- In the Schema Setting dialog enter an alias (e.g., CC Validation Demo) and click "OK."
-Note that schema is attached and click "OK."
- Save, close and reopen the document.
Your content control is now mapped and validated to the attached schema.
I still have a lot to learn about working with XML and associated schema. I would appreciate any feedback on this Microsoft Word Help & Tips page. Feedback to make it better or to address areas I may have missed or just plain got wrong.
That finishes it! I hope you have found this Microsoft Word Help & Tips page useful and informative. You can download a demo package containing the files used to create this page here: CC Validate Schema Demo.
Do you want to make a payment for consulting work or donate to help support this site?
PayPal is a safe, easy way to pay online.
Use the appropriate currency "Donate" button to make a payment or donation.