I have a homework assignment that asks to create an order form. The order form is supposed to include a list of items available for the user to purchase, which comes from an XML document and is styled with an XSL file.
I got through this part fine, and loaded the XSL into the HTML DOM using JavaScript. Now I’m stuck trying to figure out how I will process the data. I need to multiply the quantity (which is inputted by the user), and the price (which is inputted from the XML).
I am completely stuck and would greatly appreciate any hints on where to go from here.
My XSLT:
<table width="0" border="1" cellspacing="3" cellpadding="5">
<tr>
<td>Qty</td>
<td>Item Description</td>
<td>Price</td>
</tr>
<xsl:for-each select="//item">
<tr>
<td><input name="top" type="text" onchange="calculate()" value="0" size="4" /></td>
<td>Item <xsl:value-of select="id"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
</table>
Some XML:
<productcatalog>
<item>
<id>001</id>
<name>Item #1</name>
<price>10</price>
</item>
<item>
<id>002</id>
<name>Item #2</name>
<price>20</price>
</item>
<item>
<id>003</id>
<name>Item #3</name>
<price>30</price>
</item>
<item>
<id>004</id>
<name>Item #4</name>
<price>40</price>
</item>
</productcatalog>
Since the quantity is from the user you can’t implement this with XSL. Your XSL stylesheet should output a script which does the calculation.
If you can use a javascript library (e.g. jQuery) this task is considerably simpler, but you can certainly do this without it.
Your calculate function should:
Storing the price may be an issue. The new way (inaugurated with HTML5) is to use an attribute with a
data-prefix. You can adddata-priceto the html output as a handy place to get the price. You can even put this on the input node itself, so you don’t have to navigate nodes to find the price.