<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stefan Cameron on Forms &#187; Data Binding</title>
	<atom:link href="http://forms.stefcameron.com/category/data-binding/feed/" rel="self" type="application/rss+xml" />
	<link>http://forms.stefcameron.com</link>
	<description>Building intelligent forms using Adobe LiveCycle Designer</description>
	<lastBuildDate>Wed, 10 Mar 2010 18:33:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tech Talk on Database Connected Forms</title>
		<link>http://forms.stefcameron.com/2010/03/10/tech-talk-on-database-connected-forms/</link>
		<comments>http://forms.stefcameron.com/2010/03/10/tech-talk-on-database-connected-forms/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 09:46:15 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Acrobat]]></category>
		<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Designer]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XFA]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/?p=522</guid>
		<description><![CDATA[Paul Guerette, a colleague of mine at Adobe, will be giving a &#8220;tech talk&#8221; eSeminar on connecting forms to databases over at AcrobatUsers.com on Tuesday, March 23, 2010, at 10am PST (1pm EST).
If you have been struggling with connecting a form to a database, are wanting to know what the best practices and/or security concerns [...]]]></description>
			<content:encoded><![CDATA[<p>Paul Guerette, a colleague of mine at <a href="http://adobe.com/">Adobe</a>, will be giving a &#8220;tech talk&#8221; eSeminar on <a href="http://www.acrobatusers.com/events/2220/tech-talk-database-connected-forms">connecting forms to databases</a> over at <a href="http://acrobatusers.com/">AcrobatUsers.com</a> on Tuesday, March 23, 2010, at 10am PST (1pm EST).</p>
<p>If you have been struggling with connecting a form to a database, are wanting to know what the best practices and/or security concerns are, or have some specific questions you would like to ask, I highly recommend you attend the session. It&#8217;s <strong>free</strong> to attend (and free to become a member in order to attend)!</p>
<p>I&#8217;ll be answering chat questions &#8220;live&#8221; as the session unfolds. I hope you can join us!</p>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2010/03/10/tech-talk-on-database-connected-forms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing HTTP Submit Buttons</title>
		<link>http://forms.stefcameron.com/2009/12/16/testing-http-submit-buttons/</link>
		<comments>http://forms.stefcameron.com/2009/12/16/testing-http-submit-buttons/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 18:08:08 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Tables]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XFA]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2009/12/16/testing-http-submit-buttons/</guid>
		<description><![CDATA[While recently helping a few people with some issues related to HTTP submissions from XFA forms, I ended-up creating new Data Service that helps with testing HTTP Submit Buttons.
The service is quite simple: It displays what you submit to it. Since Designer&#8217;s &#8220;PDF Preview&#8221; tab is actually an instance of Internet Explorer hosting a PDF [...]]]></description>
			<content:encoded><![CDATA[<p>While recently helping a few people with some issues related to HTTP submissions from XFA forms, I ended-up creating new <a href="http://forms.stefcameron.com/data-services/">Data Service</a> that helps with <a href="http://forms.stefcameron.com/services/http-submit-test/">testing HTTP Submit Buttons</a>.</p>
<p>The service is quite simple: It displays what you submit to it. Since Designer&#8217;s &#8220;PDF Preview&#8221; tab is actually an instance of Internet Explorer hosting a PDF version of the form you&#8217;re previewing (a temporary PDF if your form is saved as an XDP or is new), the results are conveniently displayed within the tab itself after clicking on the submit button.</p>
<p>To use the service, simply use either an http submit button (or a regular button with its &#8220;Object palette &gt; Field tab &gt; Control Type property&#8221; set to &#8220;Submit&#8221;) and set its URL to:</p>
<p><a href="http://forms.stefcameron.com/services/http-submit-test/">http://forms.stefcameron.com/services/http-submit-test/</a></p>
<p>The idea is to use this service as a means to test/debug your forms before spending time writing the actual server code that will receive the data. You can also use it to ensure that you are submitting the correct data to a third-party service (for which you don&#8217;t control the server-side code).</p>
<p><span id="more-484"></span><br />
<h2>Choosing a Submit Format</h2>
<p>HTTP submissions can be made in various formats: XML, XDP, URL-encoded (HTTP post), and PDF. If you are using Reader instead of Acrobat, you will not be able to submit in PDF format unless you have enabled this ability using <a href="http://www.adobe.com/products/livecycle/readerextensions/">LC Reader Extensions</a>.</p>
<p>The most important difference amongst the formats is that <strong>repeating data sets cannot be submitted using URL-encoding</strong> (HTTP post). This is due to the way the data is submitted: In HTTP post directives, data is submitted in &#8220;name=value&#8221; pairs. Since repeating data sets involve hierarchy and duplicate data node names, only the last instance of the repeating set will be submitted.</p>
<p>To submit repeating data sets, you must choose either XML, XDP or PDF as the data format.</p>
<h2>Sample Form</h2>
<p>The following sample form demonstrates the use of the HTTP Submit Test service.</p>
<p><a href="http://forms.stefcameron.com/samples/data-binding/HttpSubmitTest.pdf">Download Sample [pdf]</a>
<p>The form has a typical address block as well as a table to demonstrate both static and repeating data sets. Use the submit buttons to try submitting in different formats (XML, XDP, PDF, URL-encoded). Notice how only the last table row is submitted when using the URL-encoded (&#8220;post&#8221;) submit button.
<p><strong>Minimum Sample Requirements:</strong> I created this sample using Designer 9.0 and Acrobat 9.2 however it should work fine back to Acrobat 7.0.5.</p>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2009/12/16/testing-http-submit-buttons/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>PDF Forms Bible Review</title>
		<link>http://forms.stefcameron.com/2009/11/13/pdf-forms-bible-review/</link>
		<comments>http://forms.stefcameron.com/2009/11/13/pdf-forms-bible-review/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 23:13:18 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Acrobat]]></category>
		<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Designer]]></category>
		<category><![CDATA[FormCalc]]></category>
		<category><![CDATA[LiveCycle]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tables]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2009/11/13/pdf-forms-bible-review/</guid>
		<description><![CDATA[
Last February, I announced the release of a new Designer book titled, &#8220;PDF Forms Using Acrobat and LiveCycle Designer Bible&#8221;.
Over the past few months, I had the opportunity to review it and I thought I would share my comments, with respect to the second-half of the book, which deals with LiveCycle Designer ES and XFA [...]]]></description>
			<content:encoded><![CDATA[<p><img style="float: left; margin-right: 15px" alt="PDF Forms Using Acrobat and LiveCycle Designer Bible" src="http://forms.stefcameron.com/images/pdfforms-book-cover.jpg">
<p>Last February, I announced the release of a <a href="http://forms.stefcameron.com/2009/02/05/new-book-acrobat-and-designer-bible/">new Designer book</a> titled, &#8220;PDF Forms Using Acrobat and LiveCycle Designer Bible&#8221;.</p>
<p>Over the past few months, I had the opportunity to review it and I thought I would share my comments, <strong>with respect to the second-half of the book</strong>, which deals with LiveCycle Designer ES and XFA forms, to help you decide if it would be a good book for you*. (The first-half deals with authoring PDF forms, a.k.a. AcroForms, in Acrobat and is beyond the scope of my blog.)</p>
<div style="clear: both; display: block">
<p><strong>Pros:</strong></p>
<ul>
<li>explains the licensing agreement involved when enabling forms for Reader using Acrobat&#8217;s &#8220;Extend Features in Adobe Reader&#8221; feature (p. 268) &#8212; this is a frequently-used feature however its governing licensing agreement, seldom understood, is explained here in &#8220;plain English&#8221;;
<li>presents and reasons through different design approaches for a particular solution, helping the reader make an informed decision on the best course of action;
<li>anything and everything you could possibly want to know is dealt with in some way;
<li>lots of important, time-saving insights in the inline &#8216;notes&#8217;;
<li>something for everyone from beginner to advanced;
<li>many cross-references between various topics, making it very easy to start in any chapter and still find all the information you need.</li>
</ul>
<p><strong>Caution:</strong></p>
<ul>
<li>risk of &#8220;information overload&#8221; &#8212; use this book as <em>reference</em> since it&#8217;s not a light read, though their goal is simply to present a myriad of options and let you pick the one that best suits your needs.</li>
</ul>
<p><strong>Overview of Topics Covered:</strong></p>
<ul>
<li>all about tables (from simple layouts to advanced);
<li>data merging with bindings;
<li>Designer user interface details and lots of tips and tricks for accelerating form layout tasks;
<li>working with static forms (with PDF backgrounds) and dynamic forms, highlighting the differences;
<li>great details on all sorts of pagination options;
<li>lots of detail on JavaScript and FormCalc, good scripting exercises with explanations, debugging tips, table with JavaScript and equivalent FormCalc functions to make it easy to script in either language if the other is more familiar to you;
<li>form deployment options;
<li>when and how to use data connections in your forms (XML, schema, database, web service) and setting data bindings;
<li>great overview of LiveCycle ES, its components (e.g. LC Forms, LC Reader Extensions, LC Rights Management, LC Content Services, etc.) and what they do.</li>
</ul>
<p>This book is available now on <a href="http://www.amazon.com/Forms-Using-Acrobatand-LiveCycle-Designer/dp/047040017X">Amazon.com</a> and <a href="http://www.wiley.com/WileyCDA/WileyTitle/productCd-047040017X.html">Wiley.com</a> and <a href="http://www.google.ca/search?q=pdf+forms+bible">elsewhere</a>.</p>
<p style="font-size: 9px">* Please note that these opinions are not necessarily those of Adobe Systems Incorporated.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2009/11/13/pdf-forms-bible-review/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Multi-Selection List Schema Definition</title>
		<link>http://forms.stefcameron.com/2009/09/30/multi-selection-list-schema-definition/</link>
		<comments>http://forms.stefcameron.com/2009/09/30/multi-selection-list-schema-definition/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 14:07:02 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Acrobat]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XFA]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2009/09/30/multi-selection-list-box-schema-definition/</guid>
		<description><![CDATA[After spending some time, recently, showing you how to connect your form to a schema and highlighting Designer&#8217;s support for schema metadata, I thought I would round-off my current train of thought on schemas by tackling multi-selection listboxes. Since they store their selected data in &#60;value&#62; nodes, once you think about it, their schema definition [...]]]></description>
			<content:encoded><![CDATA[<p>After spending some time, recently, showing you how to <a href="http://forms.stefcameron.com/2009/08/03/connecting-to-a-schema/">connect your form to a schema</a> and highlighting Designer&#8217;s support for <a href="http://forms.stefcameron.com/2009/08/28/schema-metadata/">schema metadata</a>, I thought I would round-off my current train of thought on schemas by tackling multi-selection listboxes. Since they store their selected data in &lt;value&gt; nodes, once you think about it, their schema definition may not be obvious. When you&#8217;re working with XML data that isn&#8217;t governed by a schema and namespaces, it is perhaps easier to work with (i.e. accept) these &lt;value&gt; nodes however things change when you have a schema telling you how your data must be structured and scoped (with namespaces).</p>
<p><span id="more-449"></span><br />
<h2>How Lists Store Selected Items</h2>
<p>To better understand the challenges, let&#8217;s look at how list form objects store a multi-selection of values.</p>
<p>In the <strong>Form DOM</strong>, the list&#8217;s value actually contains XML rather than plain text. For example, say we have a multi-selection list named &#8220;ListBox1&#8243; (bound to a data element named &#8220;list1&#8243;) with items (&#8220;item1&#8243;, &#8220;item2&#8243;, &#8220;item3&#8243;). If the first two items were selected, the list&#8217;s value would look like this:</p>
<pre><code>&lt;value&gt;
    &lt;exData contentType="text/xml"&gt;
        &lt;list1&gt;
            &lt;value&gt;item1&lt;/value&gt;
            &lt;value&gt;item2&lt;/value&gt;
        &lt;/list1&gt;
    &lt;/exData&gt;
&lt;/value&gt;</code></pre>
<p>In <strong>script</strong>, this would translate to a newline (\n)-delimited string when the list&#8217;s value is accessed from its rawValue property:</p>
<pre><code>item1\nitem2</code></pre>
<p>In the <strong>Data DOM</strong>, the list&#8217;s value is stored in a series of repeating &lt;value&gt; nodes, much like the way it is stored in the Form DOM:</p>
<pre><code>&lt;list1&gt;
    &lt;value&gt;item1&lt;/value&gt;
    &lt;value&gt;item2&lt;/value&gt;
&lt;/list1&gt;</code></pre>
<h2>Adding the Schema</h2>
<p>A schema describes how XML should be structured. Given how lists store their selected items in data, a schema that includes a multi-selection list will need to allow for the element that defines the list to contain zero or more repeating &lt;value&gt; nodes. Unfortunately, this means modifying your schema, if you already have one. (I can think of at least one workaround to this which would involve script and a sibling, hidden text field bound to the element in your schema, as opposed to the list itself, in which you would store the selection, but I won&#8217;t get into that in this article.)</p>
<p>For example, <a href="http://forms.stefcameron.com/samples/data-binding/schema-multisellist/multiSelList-schema.xsd">this</a> is a schema that defines a &#8220;form1&#8243; root element that contains an element named &#8220;list1&#8243; that has a multi-selection list type (the way XFA expects it).</p>
<h3>Data Connection</h3>
<p>The problem with this structure is that once you connect your form to this schema, the Data View connection tree will interpret the list1 element as being a subform that contains a repeating node, &#8220;value&#8221;:</p>
<p><img src="http://forms.stefcameron.com/images/SchemaMultiSelList/SchemaMultiSelList-1.jpg"> </p>
<p>Because of this interpretation, you cannot simply drag &amp; drop the &#8220;list1&#8243; schema element into your form in order to get a multi-selection list. Instead, you&#8217;ll get a subform with a text field inside of it instead (only an element with a simple type restricted to an enumeration of values would be interpreted as a list). Furthermore, you cannot use the picker widget next to the &#8220;Object palette &gt; Binding tab &gt; Data Binding property&#8221; to assign &#8220;list1&#8243; to a listbox you have already dragged into your form from the Object Library palette because &#8220;list1&#8243; is viewed as a container (subform) and the form object you are attempting to bind is a field.</p>
<h3>Data Binding</h3>
<p>In order to bind the listbox to the &#8220;list1&#8243; schema element, you must manually type its binding expression into the &#8220;Object palette &gt; Binding tab &gt; Data Binding property&#8221;. For our simple use case, <strong>the binding is &#8220;list1&#8243;</strong>. Note that you don&#8217;t actually bind the repeating &#8220;value&#8221; element to anything and that the schema data connection tree will not actually indicate that the &#8220;list1&#8243; element is bound to a form object even though it now is (I&#8217;m guessing this is because it is expecting &#8220;list1&#8243; to be bound to a subform, not a field).</p>
<p>The result of binding the listbox to the &#8220;list1&#8243; schema element is that the Form DOM output changes slightly to include the proper namespace on the bound element within the field&#8217;s value:</p>
<pre><code>&lt;value&gt;
    &lt;exData contentType="text/xml"&gt;
        &lt;scof:list1 xmlns:scof="http://forms.stefcameron.com/ns/2009/"&gt;
            &lt;value&gt;item1&lt;/value&gt;
            &lt;value&gt;item2&lt;/value&gt;
        &lt;/scof:list1&gt;
    &lt;/exData&gt;
&lt;/value&gt;</code></pre>
<h2>Submitting Data</h2>
<p>As you might expect, given the structure of the listbox&#8217;s selection in the Form DOM above, the list&#8217;s selection data will be similar in the Data DOM (which is what would be submitted from the form as well):</p>
<pre><code>&lt;xfa:data xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"&gt;
    &lt;scof:form1 xmlns:scof="http://forms.stefcameron.com/ns/2009/"&gt;
        &lt;scof:list1&gt;
            &lt;value&gt;item1&lt;/value&gt;
            &lt;value&gt;item2&lt;/value&gt;
        &lt;/scof:list1&gt;
    &lt;/scof:form1&gt;
&lt;/xfa:data&gt;</code></pre>
<p>The only problem with the data above is that it isn&#8217;t valid against the schema which specifies that the repeating &lt;value&gt; nodes inside the list1 element are part of the &#8220;scof&#8221; (target) namespace and should therefore look like &lt;scof:value&gt;.</p>
<h2>Submitting Namespaced Data</h2>
<p>In order to get the namespace into the repeating &lt;value&gt; nodes for the selected values, we have to use a workaround that involves a little script in the listbox&#8217;s Exit event (though the script could be triggered elsewhere, if you like):</p>
<pre><code>// JavaScript:

// schema namespace info
var nspace = "scof";
var nsUri = "http://forms.stefcameron.com/ns/2009/";

// get selection from list
var selection = this.rawValue.split("\n");

// build XML string for new selection in *Form DOM*
//  that includes the namespace in the &lt;value&gt;
//  nodes the outer &lt;listSelection&gt; node is
//  essentially irrelevant and will be discarded later
//  however it is required so that we can load its
//  content into the listbox's exData value node where
//  its value is set in the Form DOM
// this.dataNode.name gets the listbox's bound data
//  element name from the Data DOM which is required
//  in the XML
var newSelection = "&lt;listSelection&gt;\n&lt;" + nspace +
    ":" + this.dataNode.name + " xmlns:" + nspace +
    "='" + nsUri + "'&gt;";

// add selected items into the new selection -- take
//  note of the newline character at the beginning of
//  the string as it is critical to the XML loading
//  properly later on (it shouldn't be but it is...)
for (var i = 0; i &lt; selection.length; i++)
{
    newSelection += "\n&lt;" + nspace + ":value&gt;" +
        selection[i] + "&lt;/" + nspace + ":value&gt;";
}

// close the XML elements in the string
newSelection += "\n&lt;/" + nspace + ":" +
    this.dataNode.name + "&gt;\n&lt;/listSelection&gt;";

// load the new namespaced selection into the listbox's
//  exData value node, ignoring the root &lt;listSelection&gt;
//  node in the XML string
this.value.exData.loadXML(newSelection, true, true);</code></pre>
<p>The script above essentially builds an XML string equivalent in structure to that which is normally used to store the list&#8217;s selection in the Form DOM however it adds the necessary namespaces to the repeating &lt;ns:value&gt; nodes. This change is then replicated in the list1 data node in the Data DOM and the form&#8217;s exported data changes to this (which can be successfully validated against the schema we&#8217;re using):</p>
<pre><code>&lt;xfa:data xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"&gt;
    &lt;scof:form1 xmlns:scof="http://forms.stefcameron.com/ns/2009/"&gt;
        &lt;scof:list1&gt;
            &lt;scof:value&gt;item1&lt;/scof:value&gt;
            &lt;scof:value&gt;item2&lt;/scof:value&gt;
        &lt;/scof:list1&gt;
    &lt;/scof:form1&gt;
&lt;/xfa:data&gt;</code></pre>
<h3><a name="warning"></a>Import Data Warning</h3>
<p>Unfortunately, I have to point-out a bug as it relates to exporting multi-selection values with namespaces (which you only get when you use the above workaround script): Importing data with namespaced &lt;ns:value&gt; nodes for multi-selection lists &#8212; though valid XML and valid against the schema &#8212; will fail in Acrobat 9.1.3 (I have not done any testing on previous versions). My testing has revealed that the form doesn&#8217;t render properly after the data merge. Importing namespaced data that does not have namespaced &lt;value&gt; nodes for multi-selection lists, however, does work fine (this is the default way data is exported, unless you use the script I outlined above).</p>
<h2>Sample Form</h2>
<p>I have built a sample form that demonstrates how multi-selection lists store their data in the Form DOM and Data DOM. The form also includes a checkbox which, when checked, will generate namespaced &lt;ns:value&gt; nodes instead of the default &lt;value&gt; nodes, using the workaround script in the list&#8217;s Exit event.</p>
<p>Download:</p>
<ul>
<li><a href="http://forms.stefcameron.com/samples/data-binding/schema-multisellist/SchemaMultiSelList.pdf">Form [pdf]</a>
<li><a href="http://forms.stefcameron.com/samples/data-binding/schema-multisellist/multiSelList-schema.xsd">Schema [xsd]</a>
<li><a href="http://forms.stefcameron.com/samples/data-binding/schema-multisellist/sampleData.xml">Sample Data [xml]</a>
<li><a href="http://forms.stefcameron.com/samples/data-binding/schema-multisellist/sampleData-ns.xml">Sample Data (namespaced) [xml]</a></li>
</ul>
<p><strong>Minimum Requirements:</strong> I designed this form using Designer ES2 Beta and Acrobat 9.1.3 however the form should work all the way back to Designer 7.1 and Acrobat 7.0.5.</p>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2009/09/30/multi-selection-list-schema-definition/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Schema Metadata</title>
		<link>http://forms.stefcameron.com/2009/08/28/schema-metadata/</link>
		<comments>http://forms.stefcameron.com/2009/08/28/schema-metadata/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 03:48:35 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Designer]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XFA]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2009/08/28/schema-metadata/</guid>
		<description><![CDATA[My first tutorial on XML schemas explained how to connect your form to a schema however it did not show some of the Data View palette&#8217;s special features with respect to metadata in XML schemas.
Using metadata based on a combination of the XML Schema appInfo and Dublin Core title and description elements, you can influence [...]]]></description>
			<content:encoded><![CDATA[<p>My first tutorial on XML schemas explained how to <a href="http://forms.stefcameron.com/2009/08/03/connecting-to-a-schema/">connect your form to a schema</a> however it did not show some of the Data View palette&#8217;s special features with respect to metadata in XML schemas.</p>
<p>Using metadata based on a combination of the XML Schema <a href="http://www.w3.org/TR/xmlschema-1/#element-appinfo">appInfo</a> and Dublin Core <a href="http://dublincore.org/documents/2008/01/14/dcmi-terms/#elements-title">title</a> and <a href="http://dublincore.org/documents/2008/01/14/dcmi-terms/#elements-description">description</a> elements, you can influence how the Data Connection Wizard creates fields you drag and drop from the data connection tree into your form. You can also direct the Data View palette to show some of this information in the Schema Data Connection Tree that it displays.</p>
<p><span id="more-428"></span>
<p>To demonstrate these features, I have <a href="http://forms.stefcameron.com/samples/data-binding/schema/movieRental-meta.xsd">extended</a> the <a href="http://forms.stefcameron.com/samples/data-binding/schema/movieRental.xsd">original</a> movie rental schema by adding metadata to its schema elements.<br />
<h2>Element Descriptions</h2>
<p>Sometimes a schema element&#8217;s purpose or use isn&#8217;t immediately clear based on its name and/or other properties. For example, just by seeing that the schema has a &#8220;customer.account&#8221; element wouldn&#8217;t necessarily tell you that this element should be empty for a new customer (since the system would generate a new account number upon submission of the customer&#8217;s first movie rental order). To clarify the use of this element, a description can be provided:</p>
<pre><code>&lt;xs:element name="account" type="xs:string" minOccurs="0"&gt;
    &lt;xs:annotation&gt;
        &lt;xs:appinfo&gt;
            &lt;dc:description&gt;
                Customer's account number (empty if it's a new customer).
            &lt;/dc:description&gt;
        &lt;/xs:appinfo&gt;
    &lt;/xs:annotation&gt;
&lt;/xs:element&gt;</code></pre>
<p>Using the &#8220;Data View palette &gt; Palette menu button <img style="display: inline" src="http://forms.stefcameron.com/images/SchemaMetadata/SchemaMetadata-6.jpg"> &gt; &#8216;Show Both&#8217; command&#8221;, you can see the &lt;dc:description&gt; metadata associated with elements in your schema right within the Data Connection Tree:</p>
<p><img src="http://forms.stefcameron.com/images/SchemaMetadata/SchemaMetadata-1.jpg"> </p>
<p>Note that the description element must be part of the Dublin Core namespace (xmlns:dc=http://purl.org/dc/elements/1.1/) and must be contained within the XML Schema //annotation/appInfo elements in order for the Data Connection Wizard to detect it.</p>
<h2>Form Object Captions</h2>
<p>Often times schemas are defined at a corporate level because they can represent business rules and describe how data must be structured for its systems to be able to interpret it correctly. In those cases, it may be useful to also influence/dictate the labels that should be used to identify fields associated to certain schema elements. This can also be achieved with metadata by specifying title information:</p>
<pre><code>&lt;xs:element name="account" type="xs:string" minOccurs="0"&gt;
    &lt;xs:annotation&gt;
        &lt;xs:appinfo&gt;
            &lt;dc:title&gt;Account #:&lt;/dc:title&gt;
        &lt;/xs:appinfo&gt;
    &lt;/xs:annotation&gt;
&lt;/xs:element&gt;</code></pre>
<p>Unlike descriptive metadata, this information is not displayed in the Data Connection Tree. Instead, it is used by the Data Connection Wizard when you drag and drop a schema element from the Data Connection Tree into your form.</p>
<p>Remember that, in the original schema (that didn&#8217;t have any metadata), generated fields would get captions similar to their schema element names. For example,&nbsp; dragging the &#8220;customer.account&#8221; field into a form would produce a field with &#8220;Account&#8221; as its caption. With the title metadata as defined above, the generated caption would instead be &#8220;Account #:&#8221;, as in the image below:</p>
<p><img src="http://forms.stefcameron.com/images/SchemaMetadata/SchemaMetadata-2.jpg"> </p>
<p>Note that the title element must be part of the Dublin Core namespace (xmlns:dc=http://purl.org/dc/elements/1.1/) and must be contained within the XML Schema //annotation/appInfo elements in order for the Data Connection Wizard to detect it.</p>
<h2>Extra Descriptive Information</h2>
<p>There&#8217;s one more way to provide information related to an element in a schema: Any text that is directly specified within an XML Schema //annotation/appInfo element (and is <strong>not</strong> further contained within a Dublin Core title or description element) is interpreted as extra &#8220;descriptive information&#8221;. When the element is dragged and dropped into a form, this text will be imported and associated with the generated field within a &lt;desc&gt; node, <em>provided the &#8220;Generate Descriptive Information&#8221; option is checked</em> in the &#8220;Data View palette &gt; Palette menu button <img style="display: inline" src="http://forms.stefcameron.com/images/SchemaMetadata/SchemaMetadata-6.jpg"> &gt; Options dialog&#8221;:</p>
<p><img src="http://forms.stefcameron.com/images/SchemaMetadata/SchemaMetadata-3.jpg"> </p>
<p>For example, the &#8220;returnBy&#8221; schema element is defined as follows:</p>
<pre><code>&lt;xs:element name="returnBy" type="xs:date"&gt;
    &lt;xs:annotation&gt;
        ...
        &lt;xs:appinfo&gt;
            Movies must be returned by 11:59pm on this date.
        &lt;/xs:appinfo&gt;
        &lt;xs:appinfo&gt;
            The late fee is is $0.50/hour.
        &lt;/xs:appinfo&gt;
    &lt;/xs:annotation&gt;
&lt;/xs:element&gt;</code></pre>
<p>Dragging and dropping that element into your form would produce the following field mark-up in the XML Source view:</p>
<pre><code>&lt;field name="returnBy"&gt;
    ...
    &lt;desc&gt;
        &lt;text name="Schema Annotation"&gt;
            Movies must be returned by 11:59pm on this date.
        &lt;/text&gt;
        &lt;text name="Schema Annotation 2"&gt;
            The late fee is is $0.50/hour.
        &lt;/text&gt;
    &lt;/desc&gt;
&lt;/field&gt;</code></pre>
<p>The Info palette could then be used to view the contents of all nodes inside the selected field&#8217;s &lt;desc&gt; node:</p>
<p><img src="http://forms.stefcameron.com/images/SchemaMetadata/SchemaMetadata-4.jpg"> </p>
<p>You can associate any number of extra &#8220;descriptive information&#8221; metadata elements to a single schema element definition. Each one would be imported as a separate &lt;text name=&#8221;Schema Annotation {x}&#8221;&gt; node within the associated form object&#8217;s &lt;desc&gt; node</p>
<p>This information can also be viewed within the Data View palette by choosing the &#8220;Palette menu button <img style="display: inline" src="http://forms.stefcameron.com/images/SchemaMetadata/SchemaMetadata-6.jpg"> &gt; &#8216;Show Info&#8217; command&#8221;. This will display the Info Panel in the bottom half of the palette. Selecting the &#8220;returnBy&#8221; schema element would then display the following information:</p>
<ul>
<li>XFA object name (returnBy);
<li>associated XFA object type (date/time field, since the schema defines its type as &#8220;date&#8221;);
<li>occurrence (single since it isn&#8217;t repeatable like the &#8220;movie&#8221; element, for example); and
<li>all other metadata identified as &#8220;descriptive info&#8221; associated to the element.</li>
</ul>
<p><img src="http://forms.stefcameron.com/images/SchemaMetadata/SchemaMetadata-5.jpg"> </p>
<h3>Script Access</h3>
<p>Finally, the descriptive information found in a form object&#8217;s //desc/text nodes can be retrieved via script by accessing its &#8220;desc&#8221; node property and iterating through all child nodes that are of type &#8220;text&#8221; and have &#8220;Schema Annotation&#8221; in their name.</p>
<p>For example, to output the two descriptions in the &#8220;returnBy&#8221; field (as defined above) to the <a href="http://forms.stefcameron.com/2006/05/05/debugging-scripts/">JavaScript Console</a> in Acrobat, you could use the following script:</p>
<pre><code>// JavaScript:
var descList = returnBy.desc.nodes;
for (var i = 0; i &lt; descList.length; i++)
{
    var item = descList.item(i);
    if (item.className == "text" &amp;&amp; item.name &amp;&amp;
            item.name.indexOf("Schema Annotation") == 0)
    {
        console.println(item.value);
    }
}</code></pre>
<p>With a little more effort, you could parse-out all sorts of useful information provided by this type of metadata in your schemas though, most of the time, that information can usually be expressed in real schema mark-up (e.g. list items and values).</p>
<h2>Combinations</h2>
<p>Of course, you aren&#8217;t limited to using a single type of metadata on a given schema element: You can combine them to provide the degree of information you require for any element. For example, the &#8220;customer.account&#8221; element specifies both title and description metadata while the &#8220;returnBy&#8221; element specifies title and descriptive information metadata.</p>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2009/08/28/schema-metadata/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Connecting to a Schema</title>
		<link>http://forms.stefcameron.com/2009/08/03/connecting-to-a-schema/</link>
		<comments>http://forms.stefcameron.com/2009/08/03/connecting-to-a-schema/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 11:00:19 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Designer]]></category>
		<category><![CDATA[FormCalc]]></category>
		<category><![CDATA[Instance Manager]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XFA]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2009/08/03/connecting-to-a-schema/</guid>
		<description><![CDATA[In previous tutorials, I have shown how to connect an XFA form to a database and a web service. Next on the list is the XML Schema (XSD).
If you work in an enterprise setting, chances are you may have already had to deal with corporate data schemas that define how that data is structured. The [...]]]></description>
			<content:encoded><![CDATA[<p>In previous tutorials, I have shown how to connect an XFA form to a <a href="http://forms.stefcameron.com/2006/09/18/connecting-a-form-to-a-database/">database</a> and a <a href="http://forms.stefcameron.com/2007/05/21/connecting-to-a-web-service/">web service</a>. Next on the list is the <a href="http://www.w3.org/TR/xmlschema-1/">XML Schema</a> (XSD).</p>
<p>If you work in an enterprise setting, chances are you may have already had to deal with corporate data schemas that define how that data is structured. The goal, when connecting a form to a schema, is to ensure that the data consumed and output by the form adheres to a specific structure. Any fields bound to data nodes that aren&#8217;t inside the schema space are basically ignored at time of submission.</p>
<p><span id="more-410"></span>
<p>To help put things in context, I decided to define a schema for <a href="http://forms.stefcameron.com/samples/data-binding/schema/movieRental.xsd">movie rentals</a> to stay on the movie theme for the data I used in my other data connection tutorials.</p>
<p>Before we get started, I wanted to point-out that there are so many features related to schema handling and the Data View palette that I won&#8217;t be able to cover them all in a single tutorial. It would be information overload. Instead, I&#8217;ll try to point-out the basics along with a few interesting things that I think would be useful.</p>
<h2>Creating the Connection</h2>
<p><strong><font color="#008000">Right-click in the Data View palette and choose &#8220;New Data Connection&#8221;.</font></strong> In the first step, set the name for the connection (I set it to &#8220;SchemaConnection&#8221; so it&#8217;s easy to identify amongst any other connections that might be defined) and choose &#8220;XML Schema&#8221; as the type.</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-1.jpg"></p>
<p>Note that while you may have multiple ODBC and web service data connections, <strong>a form may be connected to only one schema</strong>. As of <a href="http://forms.stefcameron.com/2009/03/24/xfa-30-specification-available/">XFA 3.0</a> (Designer ES2, Acrobat/Reader 9.1), however, you can inject foreign data nodes into the Data DOM even if they aren&#8217;t in the schema, which may help if you need to get extra data into/out of your form (a topic for another post; for now, have a look at the new &#8220;File &gt; Form Properties &gt; Data Binding tab&#8221; in the <a href="http://blogs.adobe.com/mtg/2009/07/adobe_livecycle_es_next_beta_a.html">Designer ES2 Beta</a>). You can also merge schemas into a single, larger schema (&lt;import/&gt; nodes are supported which can make importing multiple schemas into a single one relatively easy).</p>
<p>The next and final steps are to choose the schema file and root element. Depending on how your schema is defined, you may have a choice of elements to use as the root. In the Movie Rental schema, there is only one root: &#8220;movieRental&#8221;.</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-2.jpg"></p>
<p>There are other options that let you embed the schema definition into your form (good if you don&#8217;t want to have to keep the schema file along with your form; otherwise, whenever you edit your form, you will need to ensure that the schema file is available) as well as specify XSLT files for incoming and outgoing data transformations (also a topic for another post).</p>
<h2>Data View Palette</h2>
<p>The initial view of the connection in the palette shows the data structure specified by the schema as represented in XFA. Each node in the schema gets an associated XFA object type depending on its description.</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-3.jpg"></p>
<p>For example:</p>
<ul>
<li>Complex types (such as {customer} and {movie}) are usually mapped to subforms since they are elements that contain child elements.
<li>Simple types (such as {customer.firstName} and {customer.prov}) are typically mapped to XFA fields. As you can see, the Schema Data Connection Wizard detected various types and assigned specific XFA fields to each data node: The {customer.prov} node is set to a drop down list because that element is defined as an enumeration of specific values in the schema while {movie.cost} is set to a decimal field because it&#8217;s type is &#8220;xs:decimal&#8221;.</li>
</ul>
<h2>Root Subform Name</h2>
<p>When the connection was created, there was a change to your form&#8217;s root subform name. Note how it has changed to <strong>movieRental</strong>:</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-4.jpg"></p>
<p>The name of the root subform is always the name of the root data element in the Data DOM. This ensures that data submitted from the form has the correct root element to match the schema and data merged into the form will be bound first by the root subform, then by child form objects.</p>
<h2>Inserting New Fields</h2>
<p>The Data View palette can be used to insert fields into your form. This makes it really easy to create new fields with the proper settings and bindings. You can customize how fields are created by choosing &#8220;Palette menu button <img style="display: inline" src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-5.jpg"> &gt; Options&#8221;:</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-6.jpg"></p>
<ul>
<li><strong>Generate Validation Scripts: </strong>If enabled, field Validate events will be generated for elements that have certain restraints defined in the schema.
<li><strong>Generate Descriptive Information:</strong> If enabled, particular forms of <a href="http://forms.stefcameron.com/2009/08/28/schema-metadata/">metadata in the schema</a> will be inserted into the field definitions in your form (more on this in another post).
<li><strong>Allow Tables to be Generated:</strong> If enabled, tables, rather than plain subforms, will be generated where appropriate.
<li><strong>Show Binding Properties Dialog Box:</strong> If enabled and you drag &amp; drop an element onto an existing field, a customization dialog will be displayed (more on this later).</li>
</ul>
<p>To insert a <em>new</em> field configured according to an element in the schema, just drag &amp; drop the element from the connection tree (in the Data View palette) into your form. You can add an entire group in a single operation by dragging a container element (e.g. {customer}). When a field in your form is bound to an element in the schema, a binding icon is displayed next to the element in the connection tree.</p>
<p><strong><font color="#008000">Start by dragging and dropping the {customer} element into your form.</font></strong> The result should be a new subform named &#8220;customer&#8221; containing 8 fields named according to the schema&#8217;s {customer} element and the elements it contains.</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-7.jpg"><br />New fields on the form.</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-8.jpg"><br />New structure in the hierarchy.</p>
<p>Notice how each leaf element in the connection now has a binding icon next to it in the connection tree (indicating that fields on the form are bound to those schema elements). The inserted fields were also customized according to their schema element counterparts. For example:</p>
<ul>
<li>All fields have appropriate data bindings (see &#8220;Object palette &gt; Bindings tab &gt; Data Binding property&#8221;).
<li>The {customer.prov} element became a drop down list which has a list of items, as well as a default value of &#8220;ON&#8221;, as specified in the schema.
<li>All new form objects have names that match their schema element names (though these could be altered, if better suited to any scripting requirements you may have, without affecting the data bindings).
<li>All new form objects have captions that are derived from their schema element names (e.g. the field bound to {customer.firstName} has a caption of &#8220;First Name&#8221;).</li>
</ul>
<p><strong><font color="#008000">Next, drag and drop the {movie} element from the connection into your form.</font></strong> This element is special because the schema specifies it may have multiple occurrences:</p>
<pre><code>&lt;xs:element
    name="movie"
    type="movieDef"
    minOccurs="1"
    maxOccurs="unbounded"/&gt;</code></pre>
<p>The result is a <strong>repeatable subform</strong>, as you can see by the Binding properties that were automatically set on the new &#8220;movie&#8221; subform (bound to movie[*] with Min Count set to 1 and no specified Max Count (unbounded)):</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-9.jpg"></p>
<p>(Note that if the properties on the &#8220;Object palette &gt; Binding tab&#8221; are partially disabled, it&#8217;s most likely because the movie subform is not inside a <em>flowed</em> container. Though it isn&#8217;t an XFA requirement, Designer requires it in order to enable the repeatable properties. We&#8217;ll fix this shortly.)</p>
<h2>Binding to Existing Fields</h2>
<p>There are two ways to bind an <em>existing</em> field to an element in the schema connection:</p>
<ol>
<li>Simply choose the element to which the field should bind by using the &#8220;Object palette &gt; Binding tab &gt; Data Binding property&#8221; pop-out menu (button to the right-hand side of the property) and selecting it from within the schema connection&#8217;s sub menu.
<li>Drag an element from the connection tree, in the Data View palette, and drop it on top of a field in your form. Doing so will &#8212; if it&#8217;s enabled &#8212; cause the <strong>Binding Properties Dialog Box</strong> to be displayed, giving you some options to further customize the existing field with information from the schema.</li>
</ol>
<h3><strong>Binding Properties Dialog Box</strong></h3>
<p><strong><font color="#008000">Add a new text field to your form.</font></strong> This will be for the {returnBy} schema element. Ensure that the &#8220;Show Binding Properties Dialog Box&#8221; option is checked in the &#8220;Data View palette &gt; Palette menu button &gt; Options&#8221; dialog. Now drag the {returnBy} element from the connection and drop it on top of the new text field.</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-10.jpg"></p>
<p>The above dialog will be displayed, giving you the option to update the following properties of the field on which the schema element was dropped:</p>
<ul>
<li><strong>Name:</strong> Updates the name of the field to match the name of the schema element (from &#8220;TextField1&#8243; to &#8220;returnBy&#8221;).
<li><strong>Caption:</strong> Updates the caption of the field to one derived from the schema (from &#8220;Text Field&#8221; to &#8220;Return By&#8221;).
<li><strong>Type:</strong> Updates the field&#8217;s object type to match the kind of data represented by the schema element (from text field to date/time field).
<li><strong>Type &gt; Default Value:</strong> Updates the field&#8217;s default value to that specified in the schema, if any (in the case of {returnBy}, the schema does not specify a default value).
<li><strong>Type &gt; Data Pattern:</strong> Updates the field&#8217;s patterns (display, edit, validate, data) according to the schema (in the case of {returnBy}, no pattern restrictions are specified in the schema, though a &#8220;date{YYYY-MM-DD}&#8221; data pattern will be set by default).</li>
</ul>
<p>Choose the &#8220;Update all related properties&#8221; option and click on the &#8220;OK&#8221; button. The text field you previously inserted will be updated to a date/time field with various properties matching the {returnBy} schema element&#8217;s specifications (as detailed above).</p>
<h2>Finishing Touches</h2>
<p>At this point, your form should look something like this (the positioning of the objects may differ depending on where you dropped them):</p>
<p><img src="http://forms.stefcameron.com/images/SchemaConnection/SchemaConnection-11.jpg"></p>
<p>Here are a few steps you can take to make things look a little nicer (in my opinion) as well as enable instances of the movie subform to be added for new movies to rent:</p>
<ol>
<li>Place the movie subform in a flowed subform so that <a href="http://forms.stefcameron.com/2006/11/11/instance-manager-object-reference/">multiple instances</a> can be added, flowed one after the other.
<li>Place an &#8220;add&#8221; button next to the movie subform to add instances as well as a &#8220;remove&#8221; button inside the movie subform to remove specific instances.
<li>Add an unbound decimal field that shows the running total cost for the movie rental order.
<li>Add script to the returnBy field to automatically set the date 30 days in the future.
<li>Add a submit button to submit the order.</li>
</ol>
<p>Following the steps above should yield a more functional movie rental form. The only thing missing is the ability to pull-in movies to choose from. For one thing, this would avoid having to specify the movie ID, title and cost when choosing a movie to rent. To accomplish this, you could connect the form to a movie <a href="http://forms.stefcameron.com/2006/10/12/displaying-all-records-from-an-odbc-data-connection/">database</a> (note that the database in the linked tutorial doesn&#8217;t have a &#8220;cost&#8221; column associated to a movie so you&#8217;ll have to add one) or, the <strong>more secure and recommended way</strong>, to a movie <a href="http://forms.stefcameron.com/2007/05/21/connecting-to-a-web-service/">web service</a>.</p>
<p><a href="http://forms.stefcameron.com/samples/data-binding/schema/ConnectingToSchema.pdf">Download Sample Form [pdf]</a></p>
<p><a href="http://forms.stefcameron.com/samples/data-binding/schema/movieRental.xsd">Download Schema [xsd]</a></p>
<p><a href="http://forms.stefcameron.com/samples/data-binding/schema/movieRental-Sample.xml">Download Sample Data [xml]</a></p>
<p><strong>Minimum Requirements:</strong> I designed this form using Designer ES2 Beta and Acrobat 9.1 however the principals should work all the way back to Designer 7.1 and Acrobat 7.0.5. There may be a few features lacking from the Data View palette if you go back that far but the concept of connecting the form to a schema remains the same.</p>
<p class="postUpdate"><strong>Aug 28, 2009</strong> &mdash; Added link to <a href="http://forms.stefcameron.com/2009/08/28/schema-metadata/">Schema Metadata</a> tutorial.</p>
<p class="postUpdate"><strong>Nov 4, 2009</strong> &mdash; Fixed a couple of syntax errors in the sample PDF form that were preventing the addition and removal of movie subform instances.</p>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2009/08/03/connecting-to-a-schema/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Creating and Loading Data Nodes</title>
		<link>http://forms.stefcameron.com/2009/06/01/creating-and-loading-data-nodes/</link>
		<comments>http://forms.stefcameron.com/2009/06/01/creating-and-loading-data-nodes/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 11:00:03 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[CM]]></category>
		<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Instance Manager]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tables]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XFA]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2009/06/01/creating-and-loading-data-nodes/</guid>
		<description><![CDATA[Have you ever needed to create data nodes in the Data DOM on-the-fly? How about a need to parse XML obtained from a web service data connection in order to find a value for some property? If that&#8217;s the case, then you&#8217;ll find-out how to do it in this article.
Creating Data Nodes
In a similar article, [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever needed to create data nodes in the Data DOM on-the-fly? How about a need to parse XML obtained from a <a href="http://forms.stefcameron.com/2009/03/23/pre-process-web-service-responses/">web service data connection</a> in order to find a value for some property? If that&#8217;s the case, then you&#8217;ll find-out how to do it in this article.</p>
<h2>Creating Data Nodes</h2>
<p>In a similar article, John Brinkman showed how to <a href="http://blogs.adobe.com/formfeed/2008/10/form_variables.html">create form variable nodes</a> at runtime however what&#8217;s not obvious about data nodes is that you don&#8217;t create them using
<p>a class name of your choice.</p>
<p>For example, say you wanted to create the following data node structure:</p>
<pre><code>&lt;custom&gt;
    &lt;message&gt;Hello World!&lt;/message&gt;
&lt;/custom&gt;</code></pre>
<p>&nbsp;</p>
<p><span id="more-372"></span></p>
<p>Given the following syntax for the createNode() function (see page 367 in the <a href="http://www.adobe.com/go/learn_lc_scriptingReference_82">LC Designer 8.2 Scripting Reference</a>):</p>
<pre><code>{model}.createNode(className [, name, namespace]);</code></pre>
<p>you might try the following, using &#8220;custom&#8221; as the class name for the first group/container node &lt;custom&gt;:</p>
<pre><code>var n = createNode("custom");</code></pre>
<p>Doing the above would generate an error stating that &#8220;custom&#8221; is an invalid node type.</p>
<p>The trick here is that only specific XFA class names are allowed, such as &#8220;text&#8221;, &#8220;decimal&#8221;, &#8220;integer&#8221;, etc., as well as two special types for data nodes: &#8220;dataGroup&#8221; and &#8220;dataValue&#8221;. When you need a data node that will <em>contain</em> other data nodes, you use the &#8220;dataGroup&#8221; class. When you need a data node to which you will <em>assign</em> a value, you use the &#8220;dataValue&#8221; node.</p>
<p>The JavaScript code to create the above XML structure then becomes:</p>
<pre><code>// &lt;custom&gt; data group to contain &lt;message&gt;
var c = xfa.datasets.createNode("dataGroup", "custom");
// &lt;message&gt; data value for "Hello World!"
var m = xfa.datasets.createNode("dataValue", "message");
// set value of data value node
m.value = "Hello World!";
// add &lt;message&gt; into &lt;custom&gt; data group node
c.nodes.append(m);
// print result to JavaScript Console (Ctrl + J in Acrobat)
console.println(c.saveXML("pretty"));

// output from above:

&lt;custom&gt;
    &lt;message&gt;Hello World!&lt;/message&gt;
&lt;/custom&gt;
</code></pre>
<p>Note that the createNode() function must be called on an object of type &#8220;model&#8221; and since we&#8217;re creating nodes in the Data DOM, we use the xfa.datasets object since it is a model object.</p>
<p>At this point, the &lt;custom&gt; node (and the nodes/values it contains) is <strong>not</strong> part of the Data DOM. To add it to the Data DOM so that it gets submitted with the rest of the form&#8217;s data, you would have to append it to a node <strong>inside</strong> the root subform (the top-most subform in the Hierarchy palette). For example, if your root subform&#8217;s name was &#8220;form1&#8243;, you would do the following to add the &lt;custom&gt; node to the form&#8217;s data:</p>
<pre><code>xfa.datasets.data.form1.nodes.append(c);</code></pre>
<p>This can be very useful. In fact, this technique is used in <a href="http://forms.stefcameron.com/category/cm/">Correspondence Management</a> letter template scripts to flag a selection list&#8217;s default selection as having been processed. Since the data merged into the list to print the letter based on the template may not match the default selection in the list, the scripts generate a special node directly in the Data DOM to indicate whether the list&#8217;s data value should be processed instead of its default selection (i.e. no need for a hidden checkbox field for each list, keeping the form/PDF file size down).</p>
<p>The &#8220;Custom Node Example&#8221; section in my <a href="http://forms.stefcameron.com/samples/data-binding/CreateLoadDataNode.pdf">sample form</a> demonstrates the above technique.</p>
<h2>Loading Data Nodes</h2>
<p>Another use for creating data nodes is they give you the ability to load existing XML in order to parse it without having to modify the Data DOM. If you create a new, orphaned (un-parented) data group node, you can use its loadXML() function to load an XML string into it, thereby giving you a handy XML node structure for parsing.</p>
<p>One example use of this would be in a form that retrieves various settings from a web service data connection where the structure of the returned data is determined by the input parameters (i.e. you have a single data connection which you can use to retrieve various data). You can return the XML data as a string and load it into a new data group node in order to parse-out the value you&#8217;re looking for without ever touching the Data DOM.</p>
<p>Another example would be an extension of my <a href="http://forms.stefcameron.com/2006/10/20/using-url-requests-in-pdf-forms/">URL Request</a> article. In this case, you could pass some XML data into your form via the URL request if your PDF form was available on a website. <font color="#ff0000"><strong>Warning:</strong> This is a not a secure method to pass data to your form.</font></p>
<p>Clicking the following link will load some movie data into the &#8220;Movies loadXML() Example&#8221; section of my sample form:</p>
<p><a href="http://forms.stefcameron.com/samples/data-binding/CreateLoadDataNodes.pdf?data=%3Cmovies%3E%3Cmovie%3E%3Ctitle%3ENacho%20Libre%3C%2Ftitle%3E%3Ccost%3E21.00%3C%2Fcost%3E%3CactorName%3EJack%20Black%3C%2FactorName%3E%3CcatName%3EComedy%3C%2FcatName%3E%3C%2Fmovie%3E%3Cmovie%3E%3Ctitle%3EMI%3AI%3C%2Ftitle%3E%3Ccost%3E20.00%3C%2Fcost%3E%3CactorName%3ETom%20Cruise%3C%2FactorName%3E%3CcatName%3EAction%3C%2FcatName%3E%3C%2Fmovie%3E%3C%2Fmovies%3E">CreateLoadDataNodes.pdf?data=%3Cmovies%3E%3Cmovie%3E%3Ctitle%3ENacho%20Libre%3C%2Ftitle%3E%3Ccost%3E21.00%3C%2Fcost%3E%3CactorName%3EJack%20Black%3C%2FactorName%3E%3CcatName%3EComedy%3C%2FcatName%3E%3C%2Fmovie%3E%3Cmovie%3E%3Ctitle%3EMI%3AI%3C%2Ftitle%3E%3Ccost%3E20.00%3C%2Fcost%3E%3CactorName%3ETom%20Cruise%3C%2FactorName%3E%3CcatName%3EAction%3C%2FcatName%3E%3C%2Fmovie%3E%3C%2Fmovies%3E</a></p>
<p>Finally, my sample form also has a handy &#8220;URI Encoding Tool&#8221; section which you can use to encode various strings to include in a website address (as above).</p>
<h2>Samples</h2>
<p><a href="http://forms.stefcameron.com/samples/data-binding/CreateLoadDataNodes.pdf">Download Sample [pdf]</a></p>
<p><strong>Minimum Sample Requirements:</strong> I created this sample using Designer 8.2 and Acrobat 9 however it should work fine back to Acrobat 7.0.5.</p>
<p>There is also a great example on creating data nodes on page 438 of the <a href="http://www.adobe.com/go/learn_lc_scriptingReference_82">LiveCycle Designer ES Scripting Reference for Designer 8.2</a> &#8212; an excellent XFA form scripting reference.</p>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2009/06/01/creating-and-loading-data-nodes/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Tip: Default Subform Binding Options</title>
		<link>http://forms.stefcameron.com/2009/04/23/tip-default-subform-binding-options/</link>
		<comments>http://forms.stefcameron.com/2009/04/23/tip-default-subform-binding-options/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 21:07:18 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Designer]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[XFA]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2009/04/23/tip-default-subform-binding-options/</guid>
		<description><![CDATA[Did you know that you can specify the default data binding for new subforms in the &#8220;Tools menu &#62; Options dialog &#62; Data Binding panel&#8221;?
&#160;
The default setting is &#8220;Normal&#8221; which means that all new subforms you insert from the Object Library with have their &#8220;Object palette &#62; Binding tab &#62; Default Binding property&#8221; set to [...]]]></description>
			<content:encoded><![CDATA[<p>Did you know that you can specify the default data binding for new subforms in the &#8220;Tools menu &gt; Options dialog &gt; Data Binding panel&#8221;?</p>
<p>&nbsp;<img src="http://forms.stefcameron.com/images/DefaultSubformBindingOption.jpg"></p>
<p>The default setting is &#8220;Normal&#8221; which means that all new subforms you insert from the Object Library with have their &#8220;Object palette &gt; Binding tab &gt; Default Binding property&#8221; set to &#8220;Normal&#8221; which means implicit binding using the subform&#8217;s name.</p>
<p>Setting this to &#8220;None&#8221; will result in all new subforms having a default binding of &#8220;None&#8221; which means they don&#8217;t participate in data binding by default. Since subforms are often needed purely for form layout, I find it&#8217;s safer to use this setting because I know inserting new subforms will not affect any bindings I have already setup in my form. (While it&#8217;s true that unnamed subforms don&#8217;t participate in data binding, I always name my subforms for clarity and scripting purposes so setting the binding to &#8220;None&#8221; ensures they don&#8217;t mess with data binding unless I want them to.)</p>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2009/04/23/tip-default-subform-binding-options/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Pre-Process Web Service Responses</title>
		<link>http://forms.stefcameron.com/2009/03/23/pre-process-web-service-responses/</link>
		<comments>http://forms.stefcameron.com/2009/03/23/pre-process-web-service-responses/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 11:00:56 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Designer]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XFA]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2009/03/23/pre-process-web-service-responses/</guid>
		<description><![CDATA[Connecting to a web service usually means using import bindings to capture the response but what if you wanted to do something special with the response? What if the response wasn&#8217;t exactly correct for merging into your form (e.g. you just wanted to extract a part of the response and push it into your form [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://http://forms.stefcameron.com/2007/05/21/connecting-to-a-web-service/">Connecting to a web service</a> usually means using import bindings to capture the response but what if you wanted to do something special with the response? What if the response wasn&#8217;t exactly correct for merging into your form (e.g. you just wanted to extract a part of the response and push it into your form somewhere)?
<p>You could still use import bindings (set using the &#8220;Object palette &gt; Bindings tab &gt; Import/Export Bindings property&#8221;) but then you might have to hide most &#8212; or all &#8212; of those fields if you didn&#8217;t want to expose them. You would wait for the web service data connection to execute, then find the field that contains the piece of data you want and set it where it needs to go. This isn&#8217;t ideal, however, because Designer doesn&#8217;t expose any events, in the Script Editor, that would let you know that you now have data from the web service response in your form. Furthermore, having those extra fields around could add a lot of unnecessary weight (read: file size). </p>
<p><span id="more-356"></span><br />
<h2>postExecute event</h2>
<p>Unless you have studied the <a href="http://partners.adobe.com/public/developer/xml/index_arch.html">XML spec</a> or deciphered its existence from the <a href="http://www.adobe.com/devnet/livecycle/designing_forms.html">LC Designer ES Scripting Reference</a>, you most likely don&#8217;t know about this event because Designer&#8217;s Script Editor palette doesn&#8217;t expose it. It&#8217;s purpose is to allow for post-processing of a web service response after a particular data connection is executed. The response data is temporarily stored in </p>
<pre><code>xfa.datasets.connectionData.{DataConnectionName}</code></pre>
<p>for the duration of the data connection&#8217;s PostExecute event. </p>
<p>Since it&#8217;s not exposed in the Script Editor, you have to go to the <strong>XML Source</strong> in order to set it on a form object &#8212; typically a subform. What follows is the XFA definition for the <em>WebServiceDC</em> data connection&#8217;s PostExecute event:</p>
<pre><code>&lt;event activity="postExecute" ref="$connectionSet.WebServiceDC"&gt;
    &lt;script contentType="application/x-javascript"&gt;
        // do something...
    &lt;/script&gt;
&lt;/event&gt;</code></pre>
<p>Note that I said this was the PostExecute event <strong>for the WebServiceDC data connection</strong>. The PostExecute event on its own &#8212; without a <em>ref </em>attribute that specifies the data connection to which it pertains &#8212; is of no use. In this case, the ref attribute specifies the WebServiceDC node inside the form&#8217;s connectionSet DOM (where web service data connections are defined). (Normally, the ref attribute has a value of &#8220;$&#8221;, which means &#8220;this&#8221;, since the event usually pertains to the form object on which it is defined.) </p>
<p>If you combine this with the ability to get web service response data without merging it into the form (either by not checking the &#8220;Object palette &gt; Execute tab &gt; Re-merge Form Data property&#8221; of the execute button for the data connection or you call xfa.connectionSet.{DataConnectionName}.execute(false)), you can essentially call a web service, get data back and do whatever you like with it. </p>
<h2>Sample Form</h2>
<p>To demonstrate how this works, I&#8217;ve created a sample form that has two web service data connections. <strong>It may take some time to open</strong> in Designer depending on how fast Designer can download the WSDL files from the two web services.</p>
<p>The first one, <a href="http://www.whitemesa.net/wsdl/r3/compound2.wsdl">EchoEmployee</a>, submits data from the top 4 fields and, using the PostExecute event, adds an item for each piece of data received in the response inside the list box. The following screen shot shows a sample result:</p>
<p><img src="http://forms.stefcameron.com/images/WSResponsePreProc/WSResponsePreProc-1.jpg"> </p>
<p>The second one, <a href="http://www.webservicex.net/ConvertTemperature.asmx?wsdl">ConvertTemperature</a>, obtains the conversion of the specified temperature from the web service response and displays it in a message box.</p>
<p><a href="http://forms.stefcameron.com/samples/data-binding/WebServiceResponsePreProc.pdf">Download Sample [pdf]</a></p>
<p><strong>Sample Minimum Requirements:</strong> While I developed the sample using Designer 8.2 and Acrobat 9.1, I have set the compatibility level to Acrobat/Reader 7.0.5 since this should work back to that version however I have not tested the form with anything earlier than 8.2/9.1.</p>
<h2>Pre-Processor Library Snippet</h2>
<p>In an effort to ease the pain of having to edit the XML Source, I have created an Object Library Snippet (XFO) that you can use to easily plug into this functionality: </p>
<p><a href="http://forms.stefcameron.com/samples/data-binding/WSResponsePreProcessor.xfo">Library Snippet [xfo]</a> </p>
<p>In order to use it, you will have to save it to your Object Library&#8217;s Custom snippet folder which, depending on whether you installed Designer on XP or Vista, will be in one of the following locations: </p>
<p align="left">C:\Documents and Settings\{username}\Application Data\Adobe\Designer\{version}\EN\Objects\Custom </p>
<p align="left">C:\Users\{username}\AppData\Roaming\Adobe\Designer\{version}\EN\Objects\Custom </p>
<p>Once inserted into your form, the snippet will look like this:</p>
<p><img src="http://forms.stefcameron.com/images/WSResponsePreProc/WSResponsePreProc-2.jpg"> </p>
<p>By default, it&#8217;s set to work with a web service data connection that is named &#8220;WebServiceDC&#8221;. When that data connection&#8217;s PostExecute event is fired, the script inside will find the response data and call the <em>processResponse()</em> function defined on a script object that is included in the snippet (which is essentially a subform). Finally, the subform hides itself when the form is initialized so you don&#8217;t have to worry about hiding it yourself.</p>
<p>Now all you have to do is put script in the processResponse() function <strong>using the Script Editor</strong>!</p>
<p>If you need to use a different data connection name, you&#8217;ll have to go to the XML Source and make two modifications, highlighted here:</p>
<p><img src="http://forms.stefcameron.com/images/WSResponsePreProc/WSResponsePreProc-3.jpg"> </p>
<p>The first modification is to the ref attribute of the &lt;event&gt; node that defines the PostExecute event and the second is in the script itself where it extracts the XML of the response.</p>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2009/03/23/pre-process-web-service-responses/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Expandable Table with Totals</title>
		<link>http://forms.stefcameron.com/2009/02/25/expandable-table-with-totals/</link>
		<comments>http://forms.stefcameron.com/2009/02/25/expandable-table-with-totals/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 12:00:27 +0000</pubDate>
		<dc:creator>Stefan Cameron</dc:creator>
				<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Designer]]></category>
		<category><![CDATA[FormCalc]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tables]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XFA]]></category>

		<guid isPermaLink="false">http://forms.stefcameron.com/2009/02/25/expandable-table-with-totals/</guid>
		<description><![CDATA[It seems that calculating totals in an expandable table with a dynamic/repeatable row is a hot topic lately so I thought I would post a tutorial on how to build a simple table which represents a purchase order where you can add and remove line items and various totals are automatically calculated.
Before we get started, [...]]]></description>
			<content:encoded><![CDATA[<p>It seems that calculating totals in an expandable table with a dynamic/repeatable row is a hot topic lately so I thought I would post a tutorial on how to build a <strong>simple</strong> table which represents a purchase order where you can add and remove line items and various totals are automatically calculated.</p>
<p>Before we get started, you&#8217;ll need Designer 7.1 and Acrobat/Reader 7.0.5 at minimum to run through this tutorial. <strong>I will be using Designer 8.2</strong> and Acrobat/Reader 9.0 to do the tutorial so it&#8217;s possible some of the UI may have changed slightly since the 7.1/7.05 versions.</p>
<p><span id="more-325"></span><br />
<h2>Creating the Table</h2>
<p>The first step is to drag a Table object from the Object Library palette&#8217;s Standard tab into your form. When you drop it, the <em>Insert Table</em> wizard appears. Set it to include 5 columns, 1 body row and header and footer rows:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-1.jpg"> </p>
<h2>Headers and Footers</h2>
<p>Double-click on the first &#8220;Header&#8221; cell and set the text/caption to &#8220;#&#8221;. This will be the dynamic index column. Set the remaining headers as follows: &#8220;Item&#8221;, &#8220;Quantity&#8221;, &#8220;Cost&#8221;, &#8220;Total&#8221;.</p>
<p>In the footer, we only need two cells: One with a caption that reads, &#8220;Grant Total:&#8221;, and the other for the calculated grand total. To start, select the first 4 footer cells by Ctrl-Clicking each one:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-2.jpg"> </p>
<p>Then, with the cells selected, click on the &#8220;Merge Table Cells&#8221; button in the Object palette (highlighted in yellow in the image below):</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-3.jpg"> </p>
<p>The 4 cells will merge into one and you can then double-click the text to change it to &#8220;Grand Total:&#8221;. While you&#8217;re editing the text, use the Paragraph palette to align it to the right.</p>
<p>We&#8217;re pretty much done with the header and footer row except for the grand total cell but we&#8217;ll come back to that after the body row has been defined.</p>
<h2>Body Row</h2>
<p>What we want for the body row is a text/read-only cell for the row number, a drop down list for selecting items, a numeric cell for the quantity, a numeric/read-only cell for the cost (which will be set depending on the selection in the item list) and a numeric/read-only cell for the calculated total (quantity * cost) for the row.</p>
<p>The first thing to do is to rename the &#8220;Row1&#8243; body row to &#8220;Item&#8221; and set it to repeat for each data item by selecting Row1 from the Hierarchy palette and setting its &#8220;Object palette &gt; Binding tab &gt; Name property&#8221; to &#8220;Item&#8221;, its &#8220;Default Binding&#8221; property to &#8220;Normal&#8221; and checking its &#8220;Repeat Row for Each Data Item&#8221; property to enable it to repeat so that the table can expand.</p>
<h3>Row Number Cell</h3>
<p>While this may sound complex, it&#8217;s actually quite easy to achieve. Since the Item body row will repeat, it will have an index and the index will be zero-based. This means our row number is simply the index of the cell&#8217;s parent (Item body row) + 1.</p>
<p>Drop a text field from the Object palette onto the first cell in the Item row. This will change the cell&#8217;s type to a text field. Set its name to &#8220;ItemIndex&#8221; (not just &#8220;index&#8221; because it&#8217;ll cause conflicts with the Item row&#8217;s index property), its &#8220;Object palette &gt; Binding tab &gt; Default Binding property&#8221; to &#8220;None&#8221; (since we don&#8217;t need the index in the submitted data &#8212; it&#8217;s just for show, after all) and its &#8220;Object palette &gt; Value tab &gt; Type property&#8221; to &#8220;Calculated &#8211; Read-Only&#8221;.</p>
<p>Finally, using the Script Editor, set its Calculate event to the following JavaScript code:</p>
<pre><code>(this.parent.index + 1) + ".";</code></pre>
<p>If we preview the form at this point, we&#8217;ll see that the first and only Item row has a row number of &#8220;1.&#8221;:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-4.jpg"> </p>
<h3>Item Name and Cost Cells</h3>
<p>The next cell is the item name. This will be a drop down list with a few items to choose from which have associated values that we&#8217;ll use to set the cost field depending on the selected item.</p>
<p>Drop a down down list from the Object Library onto the second cell in the Item row. Set its name to &#8220;ItemName&#8221; (not just &#8220;name&#8221; because it&#8217;ll cause conflicts with the Item row&#8217;s name property) and add some items to it using the Object palette&#8217;s Field tab. The following image shows that I&#8217;ve populated my list with 3 items:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-5.jpg"> </p>
<blockquote><p>An important thing to note here is that we did <strong>not</strong> associate values as costs to each item. It would&#8217;ve been convenient to store them like that however if we had, the item&#8217;s name would&#8217;ve shown-up as the cost in the submitted data which is not the intention. When data is submitted, we want the item&#8217;s name (e.g. &#8220;Helmet&#8221;) to show-up as the ItemName data.</p></blockquote>
<p>With the ItemName cell defined, we need to setup the Cost cell so that we can set its value to the value associated with the selected item.</p>
<p>Drop a numeric field from the Object Library onto the <strong>4th cell</strong> (the 3rd cell is for quantity) in the Item row. Set its name to &#8220;Cost&#8221;, its &#8220;Object Palette &gt; Binding tab &gt; Data Format property&#8221; to &#8220;Float&#8221;, its Value Type to &#8220;Read-Only&#8221; and its Display Pattern to &#8220;num{($zzzz9.99)}&#8221; (without the double-quotes).</p>
<p>Now set the following JavaScript code as the Change event for the <strong>ItemName</strong> drop down list:</p>
<pre><code>switch (xfa.event.newText)
{
    case "Helmet":
        Cost.rawValue = 45.00;
        break;

    case "Pedals":
        Cost.rawValue = 150.00;
        break;

    case "Wheel":
        Cost.rawValue = 225.00;
        break;

    default:
        // unknown item
        Cost.rawValue = 0;
        break;
}</code></pre>
<p>This simple script looks at the new selection in the ItemName list and sets the Cost field&#8217;s value accordingly.</p>
<p>At this point, we preview the form and choose &#8220;Pedals&#8221; as the item, the Cost cell will get a value of &#8220;150&#8243; which will be displayed as &#8220;$150.00&#8243; because of the display pattern we set on the Cost field earlier:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-6.jpg"> </p>
<h3>Quantity Cell</h3>
<p>With the ItemName and Cost cells defined, we can now define the Quantity cell. This one is quite simple: Drop a numeric field onto the 3rd cell in the Item row. Set its name to &#8220;Quantity&#8221; and its default value to 1.</p>
<h3>Total Cell</h3>
<p>The last cell in the Item row is the one what will show the total for that Item which is &#8220;Quantity * Cost&#8221;. This is another easy one: Drop a numeric field onto the 5th cell in the Item row. Set its name to &#8220;Total&#8221;, its Default Binding to &#8220;None&#8221; (no need to store calculated values in the data), its Value Type to &#8220;Calculated &#8211; Read-Only&#8221;, its Display Pattern to &#8220;num{($zzzz9.99)}&#8221; (without the double-quotes) and its Calculate event to the following <strong>FormCalc</strong> code:</p>
<pre><code>Quantity * Cost</code></pre>
<p>That&#8217;s it! FormCalc is great for spreadsheet-like calculations and in FormCalc, the &#8220;rawValue&#8221; property is implied so you don&#8217;t need to specify it like in JavaScript.</p>
<p>At this point, previewing our form and choosing &#8220;Pedals&#8221; as the item shows a calculated total of &#8220;$150.00&#8243; for that item:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-7.jpg"> </p>
<h2>Footer Grand Total Cell</h2>
<p>Now that the Item body row is complete, we just have a couple of things left to do. First, we need to setup the Grand Total field in the footer row which will display the grand total of the order. This field will be a numeric/read-only field which will calculate the sum of all Total fields in all Item rows that exist. This may sound complicated but once again, FormCalc makes this easy-as-pie.</p>
<p>Drop a numeric field onto the 5th (or 2nd or last) cell in the footer row. Set its name to &#8220;GrandTotal&#8221;, its Default Binding to &#8220;None&#8221; (no need to store calculated values in the data), its Value Type to &#8220;Calculated &#8211; Read-Only&#8221;, its Display Pattern to &#8220;num{($zzzz9.99)}&#8221; (without the double-quotes) and its Calculate event to the following <strong>FormCalc</strong> code:</p>
<pre><code>Sum(Item[*].Total)</code></pre>
<p>You&#8217;re probably wondering what the &#8220;[*]&#8221; notation means. Put simply, it&#8217;s like a wildcard for the index of the Item row and it tells the Sum function to get the value of the Total field in <strong>all instances </strong>of the Item row and calculate their sum.</p>
<p>At this point, if we preview our form and choose &#8220;Pedals&#8221; as the item, the total and grand total values are automatically calculated:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-8.jpg"> </p>
<h2>Adding/Removing Items</h2>
<p>Last but not least, we need a mechanism to add and remove items in the table. Since we had the Item row repeatable, this will be done using the <a href="http://forms.stefcameron.com/2006/11/11/instance-manager-object-reference/">Instance Manager</a> associated with the Item row.</p>
<p>To start, place two buttons on the form and name them &#8220;Add&#8221; and &#8220;Remove&#8221;. Set their captions to &#8220;Add&#8221; and &#8220;Remove&#8221; as well.</p>
<p>Using the Script Editor palette, set the Add button&#8217;s Click event to the following JavaScript code:</p>
<pre><code>Table1._Item.addInstance(0);</code></pre>
<p>This script will add a new instance of the Item row to the table. Now set the Remove button&#8217;s Click event to the following JavaScript code:</p>
<pre><code>if (Table1._Item.count &gt; 1)
    Table1._Item.removeInstance(Table1._Item.count - 1);</code></pre>
<p>This script will first make sure that there are at least 2 rows (since the minimum row count is 1) and if so, will remove the last row that was added to the table. Note the use of the <a href="http://forms.stefcameron.com/2006/05/18/instantiating-subforms-at-runtime/">underscore prefix</a> to access the Item row&#8217;s Instance Manager.</p>
<p>Previewing the form at this point will now allow you to add/remove rows and you&#8217;ll see that the totals are all being calculated automatically as you choose items and set quantities:</p>
<p><img src="http://forms.stefcameron.com/images/ExpandableTable/ExpandableTable-9.jpg"> </p>
<h2>Sample Form</h2>
<p>Here is the sample form for the tutorial. It&#8217;s the solution so try not to look at it until you&#8217;ve run through the tutorial on your own&#8230;</p>
<p><a href="http://forms.stefcameron.com/samples/tables/ExpandableTable.pdf">Download Sample [pdf]</a></p>
<p><strong>Sample Minimum Requirements:</strong> Designer 7.1/Acrobat 7.0.5</p>
<h2><a name="pageTotals"></a>Sample Form with Page Totals</h2>
<p>Reader Sandeep Goli asked an <a href="#comment-56711">interesting question</a>: How would one show a page total?</p>
<p>This can be done by adding first allowing the table&#8217;s content to break across pages by checking the &#8220;Object palette > Table tab > &#8216;Allow Page Breaks within Content&#8217; property&#8221;. The next step is to add another footer row to the table, located prior to the existing &#8220;FooterRow&#8221; row object (select the FooterRow row, duplicate it, rename it to &#8220;PageFooter&#8221;, rename the field inside to &#8220;PageTotal&#8221; and drag it up above the FooterRow object in the Hierarchy palette), and set it up to be included on subsequent pages as well as a the final page (rather than just the final page) using the &#8220;Object palette > Pagination tab > &#8216;Include Footer Row in Subsequent Pages&#8217; and &#8216;Include Footer Row in Final Page&#8217; properties&#8221;. This will cause the footer row to appear at the bottom of all pages, including the last page, as the table breaks across pages.</p>
<p>The final step in making this work is to set script in the new footer row&#8217;s Layout:Ready event. (If you duplicated the FooterRow, make sure you remove its Calculate script.) In this event, you can use APIs from the Layout Model which will let you determine the set of table &#8220;Item&#8221; rows that are on a given page &#8212; that being the page on which the PageTotal field was rendered:</p>
<pre><code>xfa.layout.page(object)</code></pre>
<p>will return the 1-based page number on which that object (field, subform, etc.) was rendered and</p>
<pre><code>xfa.layout.pageContent(zeroBasedPageIndex, type, 0)</code></pre>
<p>will return a list of all form objects of the specified type (a string, such as &#8217;subform&#8217;, &#8216;field&#8217;, &#8216;draw&#8217;, etc.) on the specified 0-based page index.</p>
<p>The following script finds all Item rows on the same page as the rendered PageTotal field, sums their Total child field values and sets that number as the PageTotal&#8217;s value:</p>
<pre><code>// get a list of all subforms rendered on the same page as this field
var pageSFList = xfa.layout.pageContent(xfa.layout.page(this) - 1, "subform", false);
var pageTotal = 0;
// for each subform named "Item", sum its "Total" field value
for (var i = 0; i &lt; pageSFList.length; i++)
{
    var sf = pageSFList.item(i);
    if (sf.name == "Item")
        pageTotal += sf.Total.rawValue;
}
// set this field's value to the sum of all Item rows on this page
this.rawValue = pageTotal;</code></pre>
<p><a href="http://forms.stefcameron.com/samples/tables/ExpandableTable-PageSum.pdf">Download Sample [pdf]</a><br /><a href="http://forms.stefcameron.com/samples/tables/ExpandableTable-PageSum-Data.xml">Download Sample Data [xml]</a></p>
<p><strong>Sample Minimum Requirements:</strong> Designer 8.0/Acrobat 8.0. This is due to bug fixes with table footer placement which are available only as of Acrobat 8.0.</p>
<h2><a name="removeThisRow"></a>Sample Form with Row Remove Button</h2>
<p>Reader Susila asked an <a href="#comment-60114">interesting question</a>: How do you remove a specific row using a &#8220;remove&#8221; button in the row to be removed?</p>
<p>This is done simply by inserting a column to the left of the ItemIndex column in the table and setting the cell&#8217;s type to a button. The button&#8217;s Click event is then set to remove the row to which it belongs.</p>
<p>The trick lies in writing the script in such a way that it automatically relates to (i.e. removes) the row instance to which it belongs. This is achieved by retrieving the instance index of the row to which it belongs and using that as the parameter supplied to the Item row <a href="http://forms.stefcameron.com/2006/11/11/instance-manager-object-reference/">Instance Manager</a>&#8217;s removeInstance([index]) method. The method expects a zero-based index and that&#8217;s what the index property gives you:</p>
<pre><code>// JavaScript:
this.parent.parent._Item.removeInstance(this.parent.index);</code></pre>
<p>The script above first accesses the button&#8217;s grand-parent (Table1). Then it gets the Instance Manager for the Item row and executes its removeInstance() method providing it with the index property value of its (the button&#8217;s) parent (Item row instance). By accessing parent objects, you ensure that you&#8217;re doing things with relation to the button that was clicked and, therefore, the row that contains the button.</p>
<p>Note that simply removing the row won&#8217;t necessarily update the ItemIndex field in other row instances that remain. If your form&#8217;s <a href="http://forms.stefcameron.com/2009/02/11/careful-with-form-compatibility-settings/">Target Version</a> is set to Acrobat/Reader 7.0.5, you will have to explicitly tell the form to <a href="http://forms.stefcameron.com/2006/05/20/add-recalculate/">re-execute calculations</a>. This issue was addressed in Acrobat 8.0+ so there&#8217;s no need to do this if your form targets a later version of Acrobat/Reader.</p>
<p><a href="http://forms.stefcameron.com/samples/tables/ExpandableTable-RemThisRow.pdf">Download Sample [pdf]</a></p>
<p><strong>Sample Minimum Requirements:</strong> Designer 7.1/Acrobat 7.0.5</p>
<h2>Further Improvements</h2>
<p>As I mentioned earlier, this is a <strong>simple </strong>dynamic table with a repeatable row. It demonstrates only the basics. You could enhance it further by including row shading options, validations, better formatting (font and paragraph), smarter add/remove buttons (e.g. put the remove button on the Item row in order to delete that specific row instead of always deleting from the end), put an insert button on the Item row in order to insert new items instead of always appending to the end of the list, etc.</p>
<p>For an advance sample, you should check-out the &#8220;Dynamic Interactive Purchase Order&#8221; sample that ships with Designer. Depending on how Designer was installed (either with Acrobat, with LiveCycle or standalone) and whether it was on Win 2k, XP or Vista, you should be able to find it in one of the following folders:</p>
<p align="left"><strong>C:\Program Files\Adobe\LiveCycle ES\Workbench ES\Designer ES\</strong><em>{version}</em>\EN\Samples\Forms\Purchase Order\Dynamic Interactive\Forms</p>
<p align="left"><strong>C:\Program Files\Adobe\LiveCycle Designer ES\</strong><em>{version}</em>\EN\Samples\Forms\Purchase Order\Dynamic Interactive\Forms</p>
<p align="left"><strong>C:\Program Files\Adobe\Acrobat\Designer\</strong><em>{version}</em>\EN\Samples\Forms\Purchase Order\Dynamic Interactive\Forms</p>
<p align="left">(Note that in Vista, &#8220;Program Files&#8221; changes to &#8220;Program Files (x86)&#8221;.)</p>
<p class="postUpdate"><strong>Updated Oct 2, 2009</strong> &#8212; Added <a href="#pageTotals">page totals</a> sample.</p>
<p class="postUpdate"><strong>Updated Oct 2, 2009</strong> &#8212; Added <a href="#removeThisRow">row remove button</a> sample.</p>
]]></content:encoded>
			<wfw:commentRss>http://forms.stefcameron.com/2009/02/25/expandable-table-with-totals/feed/</wfw:commentRss>
		<slash:comments>93</slash:comments>
		</item>
	</channel>
</rss>
