title: Using XPath in MS Word to select an attribute from a specific node
body: <p>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:</p>
  
  <pre><code>&lt;REV_REPORT&gt;
    &lt;REV_HEADER&gt;
      &lt;NAME&gt;Student Schedule List&lt;/NAME&gt;
      &lt;NUMBER&gt;STU415&lt;/NUMBER&gt;
      &lt;ORGANIZATION&gt;My Fine High School&lt;/ORGANIZATION&gt;
      &lt;YEAR_TITLE&gt;2016-2017&lt;/YEAR_TITLE&gt;
    &lt;/REV_HEADER&gt;
    &lt;REV_DATA_ROOT&gt;
      &lt;Student Grade="12" StudentName="Smith, John J." &gt;
        &lt;StudentSchoolYear OrganizationName="My Fine High School"&gt;
          &lt;StudentClass PeriodBegin="1" RoomName="202-B" TeacherFormattedName="Able, J." /&gt;
          &lt;StudentClass PeriodBegin="2" RoomName="STD1-B" TeacherFormattedName="Baker, M." /&gt;
          &lt;StudentClass PeriodBegin="4" RoomName="614-H3" TeacherFormattedName="Channing, B." /&gt;
          &lt;StudentClass PeriodBegin="5" RoomName="BAND1-A" TeacherFormattedName="Donner, T." /&gt;
          &lt;StudentClass PeriodBegin="6" RoomName="805-H4" TeacherFormattedName="Enfield, K." /&gt;
          &lt;StudentClass PeriodBegin="7" RoomName="205-H1" TeacherFormattedName="Gonzalez, A." /&gt;
        &lt;/StudentSchoolYear&gt;
      &lt;/Student&gt;
    &lt;/REV_DATA_ROOT&gt;
  &lt;/REV_REPORT&gt;
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The final output should have blanks in a period column if the student has no classes that period.  </p>
  
  <p>In the Word document I created a table with a static header row.  The beginning and end of the table mergefield commands are:</p>
  
  <pre><code>{MERGEFIELD TableStart:REV_DATA_ROOT/Student \* MERGEFORMAT }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>{MERGEFIELD TableEnd:REV_DATA_ROOT/Student \* MERGEFORMAT }
  </code></pre>
  
  <p>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.:</p>
  
  <pre><code>{MERGEFIELD StudentSchoolYear/StudentClass[1]/@TeacherFormattedname \* MERGEFORMAT }
  {MERGEFIELD StudentSchoolYear/StudentClass[1]/@RoomNumber \* MERGEFORMAT }
  </code></pre>
  
  <p>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.</p>
  
  <p>Another question here on <a href="https://stackoverflow.com/questions/1006283/xpath-select-first-element-with-a-specific-attribute">Stack Overflow</a> led me to try this:</p>
  
  <pre><code>{MERGEFIELD (StudentSchoolYear/StudentClass[@PeriodBegin=’1’])[1]/@TeacherFormattedName \* MERGEFORMAT }
  {MERGEFIELD (StudentSchoolYear/StudentClass[@PeriodBegin=’1’])[1]/@RoomNumber \* MERGEFORMAT }
  </code></pre>
  
  <p>This yields an error:</p>
  
  <pre><code>Error: '(StudentSchoolYear/StudentClass[@PeriodBegin=’1’])' has an invalid token.
  </code></pre>
  
  <p>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?</p>