Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Validate Content Control Using Schema
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

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.

validate cc with schema 0
The content control tagged "Sex?" is mapped and validated
attached schema
validate cc with schema 1
Content control showing schema validation flag
validate cc with schema 2
User selected content control showing valid schema conditions
validate cc with schema 3
Any entry that does not meet the validation
conditions of the attached schema is flagged
validate cc with schema 4
The schema invalid flag is removed, and in this case the answer is true,
only when one of the schema conditions is met.

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.

So how is it done ...

You can create a document with a mapped content control validated with an attached schema by performing the following steps:

  1. Create and save a new Word 2007/2010/2013/2016/2019 macro enabled document (.docm extension).
  2. Insert a plain text content and assign the title and tag properties "Sex?".
  3. Create a basic XML script containing a data node that will serve as the CustomXMLPart and mapping path for the content control. You can use practically any XML text editor (e.g., Notepad) for this purpose.
Basic XLM Scirpt:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ccMap xmlns="http://ValidationDemo">
  <ccElement_SexualContact></ccElement_SexualContact>
</ccMap>

Site Note IconNote:  My XML script is saved as D:\CCValidationDemoXML.txt

  1. Create and save the schema file (an .xsd file).
Schema Script:
<?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: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>

Site Note IconNotes:
    1. The XSD file I used is saved on my PC as D:\CC ValidationDemoSchema.xsd
    2. The following link will introduce you to a complete W3Schools tutorial on creating XSD files: XSD Tutorial

  1. Create a CustomXMLPart in the document and load the XML script.
  2. Map the content control to the CustomXMLPart data node.

Site Note IconNotes:

     1.  The following VBA procedures can be used to perform steps 5 and 6.
    2.  Users with Word 2013 or higher can load and map the content control using the Developer>XML Mapping Pane tools.

VBA Script:
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
lbl_Exit:
  Exit SUb
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:\CCValidationDemoXML.txt"
lbl_Exit:
  Exit Sub
End Sub

Sub ClearXMLParts()
Dim lngIndex As Long
  For lngIndex = ActiveDocument.CustomXMLParts.Count To 4 Step -1
    ActiveDocument.CustomXMLParts(lngIndex).Delete
  Next lngIndex
lbl_Exit:
  Exit Sub
End Sub

Site Note iconSee: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.

  1. Attach the schema to the document:

- For Word 2007 and 2010 users, on the Developer tab, in the XML group, select "Schema."

validate cc with schema 5

-For Word 2013 and higher, on the Developer tab, in the Templates Group, Select "Document Template."

validate_cc_with_schema_005A

-In the "Templates and Add-Ins" dialog, XML Schema tag and select "Add Schema."

validate_cc_with_schema_006

-In the Add Schema dialog, select your saved schema file and click "Open."

validate_cc_with_schema_007

- In the Schema Setting dialog enter an alias (e.g., CC Validation Demo) and click "OK."

validate_cc_with_schema_008

-Note that schema is attached and click "OK."

validate_cc_with_schema_009

- Save, close and reopen the document.

Your content control is now mapped and validated to the attached schema.

Study iconI 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.

Share

PAYMENTS/DONATIONS

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

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.


Search my site or the web using Google Search Engine

Google Search Logo