Forum How do I...?

Mail Merge

davidpratten
Our organisation does a regular mail out and I have just realised that our XHTML newsletter could be personalised if I could figure out how to do mail merge with XHTML.

The name and address and account details are in a database (of course) and the newsletter is in XHTML.

Is there a standardised way of creating a meta-document in xml to control a mail merge?

Is there any work being done on standardising Mail Merge Field Names and database bindings?

I could always make an extension to my database to to implement this kind of functionality but I would rather not "re-invent the wheel".

Thanks for any advice.

David
davidpratten
In looking at the W3 I checked out XQuery and it looked interesting. I downloaded Saxon XQuery commandline processor

XQuery is a useful way to do very flexible XHTML Mail merge

For example - for this list of recipients of a newsletter

<newsletter>
		<recipient><group><name>Sydney Group</name>	</group><last>Stevens</last><first>William</first><address1>12 Line St, Gladesville NSW 2111</address1>
		</recipient>
		<recipient><group><name>Sydney Group</name>	</group><last>Jones</last><first>Jane</first><address1>22 June Ave, Sutherland NSW 2483</address1>
		</recipient>
		<recipient><group><name>Sydney Group</name>	</group><last>Payne</last><first>Homer</first><address1>22 June Ave, Sutherland NSW 2483</address1>
		</recipient>
		<recipient><group><name>Canberra Group</name></group><last>Phillips</last><first>Robert</first><address1>12 Line St, Gladesville NSW 2111</address1>
		</recipient>
		<recipient><group><name>Canberra Group</name></group><last>Lane</last><first>Jill</first><address1>22 June Ave, Sutherland NSW 2483</address1>
		</recipient>
</newsletter>



This Xquery

<newsletters>
  {
    let $a := doc("newsletter.xml")//group
    for $name in distinct-values($a/name)
    order by $name
    return
	<div>
        <div>
		{ $name }
	</div>
    	{
                for $r in doc("newsletter.xml")/newsletter/recipient,
			$first in $r/first
                where some $ra in $r/group
                satisfies ($ra/name = $name)
                return 
			<div>
			Dear {fn:data($first)},
We are so glad to be writing to you today ....
			</div>		
			
        }
	</div>
  }
</newsletters> 


Produces this output of merged output with a once per group div for a cover sheet!

<newsletters>
   <div>
      <div>Canberra Group</div>
      <div>
			Dear Robert,
We are so glad to be writing to you today ....
			</div>
      <div>
			Dear Jill,
We are so glad to be writing to you today ....
			</div>
   </div>
   <div>
      <div>Sydney Group</div>
      <div>
			Dear William,
We are so glad to be writing to you today ....
			</div>
      <div>
			Dear Jane,
We are so glad to be writing to you today ....
			</div>
      <div>
			Dear Homer,
We are so glad to be writing to you today ....
			</div>
   </div>
</newsletters>


With a little bit of imagination, we have a way of inserting merge fields
Dear {fn:data($first)},
into an xhtml document and generating a printable document!