title: How to generate RDF/XML using a template in javascript?
body: <p>I have some working Javascript code that generates an RDF/XML document using variables picked up from HTML fields:</p>
  
  <pre><code>...
    peopleDoap += "   &lt;foaf:name&gt;" + person_name + "&lt;/foaf:name&gt;\n";
    if (person_url != "") {
      peopleDoap += "   &lt;foaf:homepage rdf:resource=\"" + person_url + "/\"/&gt;\n";
    }
    if (person_pic != "") {
      peopleDoap += "   &lt;foaf:depiction rdf:resource=\"" + person_pic + "/\"/&gt;\n";
    }
  ...
  </code></pre>
  
  <p>It's hard, looking at this code, to get any sense of what the output will look like (especially as this code is scattered amongst sub functions etc).</p>
  
  <p>I'm wondering if there is an easy way that would enable me to have something like this:</p>
  
  <pre><code>...
  &lt;foaf:name&gt;%person_name%&lt;/foaf_name&gt;
    &lt;foaf:homepage rdf:resource="%person_url%"/&gt;
    &lt;foaf:depiction rdf:resource="%person_pic%"/&gt;
  ...
  </code></pre>
  
  <p>And then some substitution code. One slight complication is if fields are left blank, I will want to not generate the whole element. Ie, if person_url='', the above should generate as:</p>
  
  <pre><code>...
  &lt;foaf:name&gt;%person_name%&lt;/foaf_name&gt;
    &lt;foaf:depiction rdf:resource="%person_pic%"/&gt;
  ...
  </code></pre>
  
  <p>I guess I could do this fairly naively by defining the template as a huge string, then performing a bunch of replaces on it, but is there anything more elegant? Mild preference for native Javascript rather than libraries, but happy to be convinced...</p>
  
  <p>(Btw, yes, since this is RDF/XML, maybe there is a smarter way using some kind of RDF library. If you want to address that of the question instead, that's ok with me.)</p>
  
  <p><a href="http://code.google.com/p/simal/source/browse/trunk/uk.ac.osswatch.simal.web/src/main/widgets/doapcreator/scripts/doapform.js" rel="nofollow">Code is here</a>.</p>
  
  <p>Also, this is a widget running on a Jetty server. I don't think server-side code is an option.</p>