xml


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?
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.
Have you tried:
{MERGEFIELD StudentSchoolYear/StudentClass[#PeriodBegin='1']/#TeacherFormattedname \* MERGEFORMAT }
This assumes you only ever have one StudentClass element with a #PeriodBegin=1, but with that assumption you shouldn't need [1].
I must admit I've never seen XPath that is part-bracketed and that might be why you are getting the syntax error.

Related Links

Xsl anchor attribute
How to get start and end of a week of a specific date using xslt?
how to select elements when the root element has a lot of attributes
xsd select multiple values from enumeration or equivalent type
Issue with XSL loops and linefeeds
what is the xpath of this element
Openerp module output not generated properly
trasfer one excel sheet into multiple xml content in mule
R : Get attribute value from an XML
How to use the ref attribute without allowing multiple roots
XSLT - Multiple parent groups of same name
multiple attributes in different combinations
XML Read to Array in Autoit
Count number of children of the child with more children
how to read data from an XML site and put it in a var in vb.net
XQuery request opens whole file

Categories

HOME
hyperlink
fpga
battery
components
sublimetext
soa
echo
missing-data
banner
barcode
qpython
jquery-ui-sortable
openstreetmap
endeca
ibeacon-android
jxl
intercom
qsqlquery
azure-powershell
coin3d
mathprog
apache2.4
guzzle
pimcore
mongoid6
epsilon
progress-bar
android-things
wcag
rpgle
tpm
complex-numbers
clickjacking
joomla2.5
arduino-ide
video-capture
latitude-longitude
google-knowledge-graph
lint
playframework-2.1
chronicle-map
apollostack
importerror
memory-mapping
vaadin-elements
ntvs
clarifai
hammer.js
user-defined-fields
launchctl
amq
context-sensitive-grammar
opengl-4
mach-o
py2neo
renaming
polyline
parfor
tinkerpop3
neon
windows-azure-queues
mta
vorpal.js
jtwig
lua-telegram-bot
spreadjs
thread-sleep
nio2
np-spring
design-principles
jbase
character-replacement
terracotta
rewrite
pypiserver
iced-coffeescript
telepat
fits
procedural-programming
cross-join
mass-assignment
at-job
jbake
psr-4
poker
traceability
windows-98
cardreader
sqlclr
booksleeve
jquery-blockui
paginator
kqueue
apache-commons-dbutils
webdev.webserver
jqote
rmdir
timthumb
tmx
mtu
instantiationexception
spread
django-piston
online-storage
blind
image-capture
wspbuilder

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App