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

XML list types required for WCF
What is the meaning of xs:mixed without elements?
Copy xml element with whole tree
how to get unique record count for each location_Name by Record_No when RECORD_TYPE='ERROR' with XSLT
XML and accented charachters
XSLT grouping based on XML element values
REST and links: middle ground?
What's Haskell's attitude towards Unicode in XML?
Specific How-To Minimally Style XML with XSLT after adding over-arching categories
Why is my XSLT style sheet not pulling values from my XML file for a table?
Appending information to an XML file in VB.NEt
Microsoft JScript runtime error: Automation server can't create object‎
XML to HTML Generator with Style template
XPath expression to select first instance of element in repeating parent node
Getting xml parent node tag name with child node value using XSLT
Get all the albums of one artist

Categories

HOME
oracle12c
spark-streaming
awk
telegram
nvd3.js
pda
angular2-material
pascal
mstest
google-apps
settings
spring-session
android-json
data-visualization
teechart
spin
arangodb
categorical-data
watson-conversation
mod-security
xcodebuild
3nf
bitnami
do-while
altera
mathprog
google-content-api
soci
shortest-path
guzzle
angular-routing
aggregation
draft-js-plugins
dapper-extensions
underscore.js
8051
winexe
corenlp-server
openerp-7
lint
metawidget
numberpicker
watchface
portforwarding
branching-and-merging
postgresql-8.4
xmldocument
importerror
clang++
apache-directory
opencmis
pom.xml
opennms
mongotemplate
custom-font
qtranslate
boxing
spring-data-couchbase
monkey
shinobi
swingworker
visual-studio-2008-sp1
pure-css
resourcemanager
jquery-jscrollpane
embedding
omniauth-facebook
lua-telegram-bot
dbamp
turbo-prolog
uiactionsheet
iokit
fuelcms
page.js
.bash-profile
typemock
grouping-sets
at-job
android-cookiemanager
punycode
paw
linkedin-jsapi
wcm
with-statement
personalization
reflector
ecslidingviewcontroller
windows-embedded-standard
traceability
fb.ui
android-sdk-2.3
android-loadermanager
adobe-media-server
lambdaj
stress
node-redis
procedures
denied
exponentiation
objective-j
jqueryform
isapi-extension
flvplayback
httpcookie
online-storage
mathematical-typesetting

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