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 an attached schema by peforming the following steps:

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

Site Note IconNote:  My XML script is saved as D:\CC Validation Demo (XML).xml

  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://gmaxey.mvps.org/ValidationDemo"
xmlns="http://gmaxey.mvps.org/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>

Site Note IconNotes:
    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

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

Site Note IconNote: The following VBA procedures can be used to perform steps 4 and 5.

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
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

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:

- On the Developer tab, in the XML group, select "Schema."

validate cc with schema 5

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

validate cc with schema 6

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

validate cc with schema 7

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

validate cc with schema 8

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

validate cc with schema 9

- 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 Stumbleupon

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

Or

JustAnswerAsk a Word Expert OnlineSubmit