Search code examples
xmlxpathms-wordmailmerge

Using XPath in MS Word to select an attribute from a specific node


Our Student Information System (Synergy) allows us to create a mail merge document in MS Word to replace existing reports. To that end the report I'm trying to replace generates the following (simplified) XML:

<REV_REPORT>
  <REV_HEADER>
    <NAME>Student Schedule List</NAME>
    <NUMBER>STU415</NUMBER>
    <ORGANIZATION>My Fine High School</ORGANIZATION>
    <YEAR_TITLE>2016-2017</YEAR_TITLE>
  </REV_HEADER>
  <REV_DATA_ROOT>
    <Student Grade="12" StudentName="Smith, John J." >
      <StudentSchoolYear OrganizationName="My Fine High School">
        <StudentClass PeriodBegin="1" RoomName="202-B" TeacherFormattedName="Able, J." />
        <StudentClass PeriodBegin="2" RoomName="STD1-B" TeacherFormattedName="Baker, M." />
        <StudentClass PeriodBegin="4" RoomName="614-H3" TeacherFormattedName="Channing, B." />
        <StudentClass PeriodBegin="5" RoomName="BAND1-A" TeacherFormattedName="Donner, T." />
        <StudentClass PeriodBegin="6" RoomName="805-H4" TeacherFormattedName="Enfield, K." />
        <StudentClass PeriodBegin="7" RoomName="205-H1" TeacherFormattedName="Gonzalez, A." />
      </StudentSchoolYear>
    </Student>
  </REV_DATA_ROOT>
</REV_REPORT>

As you can see, the student has periods 3 and 8 free, and the XML doesn't give a node for either of those periods. I am trying to generate something from this XML that will look like this:

Student Name    Gr    P1         P2         P3       P4            P5          P6           P7            P8
Smith, John J.  12    Able, J.   Baker, M.           Channing, B.  Donner, T.  Enfield, K.  Gonzalez, A.   
                      202-B      STD1-B              614-H3        BAND1-A     805-H4       205-H1

The final output should have blanks in a period column if the student has no classes that period.

In the Word document I created a table with a static header row. The beginning and end of the table mergefield commands are:

{MERGEFIELD TableStart:REV_DATA_ROOT/Student \* MERGEFORMAT }

and

{MERGEFIELD TableEnd:REV_DATA_ROOT/Student \* MERGEFORMAT }

and are placed in the first and last cells of the second row. I'm able to get the name and grade to print without any problems. I can get the teacher and room number to print if I tell it to choose a specific position, i.e.:

{MERGEFIELD StudentSchoolYear/StudentClass[1]/@TeacherFormattedname \* MERGEFORMAT }
{MERGEFIELD StudentSchoolYear/StudentClass[1]/@RoomNumber \* MERGEFORMAT }

Unfortunately, if I do that for each period (updating the number to match the period number I want), for this student it will print P4 in the P3 column, P5 in P4, etc.

Another question here on Stack Overflow led me to try this:

{MERGEFIELD (StudentSchoolYear/StudentClass[@PeriodBegin=’1’])[1]/@TeacherFormattedName \* MERGEFORMAT }
{MERGEFIELD (StudentSchoolYear/StudentClass[@PeriodBegin=’1’])[1]/@RoomNumber \* MERGEFORMAT }

This yields an error:

Error: '(StudentSchoolYear/StudentClass[@PeriodBegin=’1’])' has an invalid token.

My question: is what I'm trying to do too complex for Word's mail merge engine, or is there a syntax error I'm not seeing?


Solution

  • You are receiving this error,

    Error: '(StudentSchoolYear/StudentClass[@PeriodBegin=’1’])' has an invalid token.

    because your XPath expression is using single curly/smart quotes instead of straight/dumb quotes surrounding 1. Change ’1’ to '1' to fix this problem.