Re: Unique html name, id, and for form fields2005-05-11 10:55:15I have come up with a solution to the problem. I would appreciate any tips on how to optimize/clean up/clear up/shorten my xsl file. I would like to have 2 seperate xsl files one that has the generic repeat, and bind templates and another that calls the repeat and bind templates for the specific page. Thanks Jeremy -------- test.xsl -------- <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" /> <xsl:variable name="data_bind" select="document('data.xml')" /> <xsl:template match="/"> <xsl:apply-templates /> </xsl:template> <!-- Repeat Example --> <xsl:template match="*[(_at_)id = 'status_table']"> <xsl:param name="path" /> <xsl:param name="data_id" /> <xsl:call-template name="repeat"> <xsl:with-param name="nodeset" select="$data_bind//xml/data[(_at_)id = 'statuses']/status" /> <xsl:with-param name="path" select="$path" /> </xsl:call-template> </xsl:template> <!-- Repeat Example --> <xsl:template match="*[(_at_)id = 'ac_request']"> <xsl:param name="path" /> <xsl:param name="data_id" /> <xsl:variable name="data_row" select="$data_bind//*[generate-id() = $data_id]" /> <xsl:call-template name="repeat"> <xsl:with-param name="nodeset" select="$data_bind//xml/data[(_at_)id = 'ac_requests']/ac_request[(_at_)status = $data_row/@status]" /> <xsl:with-param name="path" select="$path" /> </xsl:call-template> </xsl:template> <!-- Repeat Example --> <xsl:template match="*[(_at_)id = 'action_list']"> <xsl:param name="path" /> <xsl:param name="data_id" /> <xsl:call-template name="repeat"> <xsl:with-param name="nodeset" select="$data_bind//xml/data[(_at_)id = 'actions']/action" /> <xsl:with-param name="path" select="$path" /> </xsl:call-template> </xsl:template> <!-- Bind Example (mode??? any other options?) --> <xsl:template match="*[(_at_)id = 'action_list']" mode="bind_repeat"> <xsl:param name="data_id" /> <xsl:attribute name="value"> <xsl:value-of select="$data_bind//*[generate-id() = $data_id]/@action_id" /> </xsl:attribute> <xsl:value-of select="$data_bind//*[generate-id() = $data_id]/@action" /> </xsl:template> <!-- Bind Example (use mode here?) --> <xsl:template match="h2[(_at_)id = 'status_header']"> <xsl:param name="path" /> <xsl:param name="data_id" /> <xsl:copy> <xsl:apply-templates select="@*"> <xsl:with-param name="path" select="$path" /> </xsl:apply-templates> <xsl:attribute name="class">dark</xsl:attribute> <xsl:value-of select="$data_bind//*[generate-id() = $data_id]/@status" /> </xsl:copy> </xsl:template> <!-- Bind Example (use mode here?) --> <xsl:template match="td[(_at_)id = 'request_date']"> <xsl:param name="path" /> <xsl:param name="data_id" /> <xsl:copy> <xsl:apply-templates select="@*"> <xsl:with-param name="path" select="$path" /> </xsl:apply-templates> <xsl:attribute name="class">dark</xsl:attribute> <xsl:value-of select="$data_bind//*[generate-id() = $data_id]/@request_date" /> </xsl:copy> </xsl:template> <!-- Bind Example (use mode here?) --> <xsl:template match="td[(_at_)id = 'change_title']"> <xsl:param name="path" /> <xsl:param name="data_id" /> <xsl:copy> <xsl:apply-templates select="@*"> <xsl:with-param name="path" select="$path" /> </xsl:apply-templates> <xsl:attribute name="class">dark</xsl:attribute> <xsl:value-of select="$data_bind//*[generate-id() = $data_id]/@change_title" /> </xsl:copy> </xsl:template> <!-- Bind Example (use mode here?) --> <xsl:template match="td[(_at_)id = 'status']"> <xsl:param name="path" /> <xsl:param name="data_id" /> <xsl:copy> <xsl:apply-templates select="@*"> <xsl:with-param name="path" select="$path" /> </xsl:apply-templates> <xsl:attribute name="class">dark</xsl:attribute> <xsl:value-of select="$data_bind//*[generate-id() = $data_id]/@status" /> </xsl:copy> </xsl:template> <!-- Add the path to for, id, and name --> <xsl:template match="@for|@id|@name"> <xsl:param name="path" /> <xsl:param name="position" /> <xsl:choose> <xsl:when test="$path"> <xsl:attribute name="{name()}"><xsl:value-of select="concat($path, ':', ., $position)" /></xsl:attribute> </xsl:when> <xsl:otherwise> <xsl:attribute name="{name()}"><xsl:value-of select="concat(., $position)" /></xsl:attribute> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- generic repeat --> <xsl:template name="repeat"> <xsl:param name="nodeset" /> <xsl:param name="path" /> <xsl:param name="current" select="." /> <xsl:for-each select="$nodeset"> <xsl:element name="{name($current)}"> <xsl:apply-templates select="$current/@*"> <xsl:with-param name="path" select="$path" /> <xsl:with-param name="position" select="concat('_', position())" /> </xsl:apply-templates> <xsl:apply-templates select="$current" mode="bind_repeat"> <xsl:with-param name="data_id" select="generate-id(.)" /> </xsl:apply-templates> <xsl:choose> <xsl:when test="$path"> <xsl:apply-templates select="$current/*"> <xsl:with-param name="data_id" select="generate-id(.)" /> <xsl:with-param name="path" select="concat($path, ':', $current/@id, '_', position())" /> </xsl:apply-templates> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="$current/*"> <xsl:with-param name="data_id" select="generate-id(.)" /> <xsl:with-param name="path" select="concat($current/@id, '_', position())" /> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:element> </xsl:for-each> </xsl:template> <!-- identity match --> <xsl:template match="node()|@*"> <xsl:param name="data_id" /> <xsl:param name="path" /> <xsl:copy> <xsl:apply-templates select="@*|node()"> <xsl:with-param name="data_id" select="$data_id" /> <xsl:with-param name="path" select="$path" /> </xsl:apply-templates> </xsl:copy> </xsl:template> <!-- identity match for bind_repeat --> <xsl:template match="node()|@*" mode="bind_repeat"> </xsl:template> </xsl:stylesheet> -------- test.xml -------- <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="test.xsl"?> <html> <head> <title>Test</title> </head> <body> <form action="transform.aspx" method="post"> <div id="status_table"> <h2 id="status_header"></h2> <table border="1" cellpadding="4" width="650px"> <tr> <td width="25%"> <strong>Request Date</strong> </td> <td width="15%"> <strong>Change Title</strong> </td> <td width="15%"> <strong>Status</strong> </td> <td width="45%"> <strong>Action</strong> </td> </tr> <tr id="ac_request"> <td id="request_date">a</td> <td id="change_title">b</td> <td id="status"></td> <td> <label for="action">Action:</label> <select id="action" name="action"> <option value="0">Select an action</option> <option id="action_list" value=""></option> </select> <span id="test">test:</span> <input type="submit" name="submit" value="Go" /> </td> </tr> </table> <br /> </div> </form> </body> </html> -------- data.xml -------- <?xml version="1.0"?> <xml> <data id="ac_requests"> <ac_request id="1" request_date="2005-03-09 9:11:56" change_title="test" status="Completed"/> <ac_request id="2" request_date="2005-03-09 9:12:44" change_title="test2" status="Approved"/> <ac_request id="3" request_date="2005-03-09 9:14:15" change_title="test3" status="Denied"/> <ac_request id="4" request_date="2005-03-09 9:14:15" change_title="test23" status="Approved"/> </data> <data id="actions"> <action action_id="1" action="View"/> <action action_id="2" action="Delete"/> <action action_id="3" action="Approve"/> <action action_id="4" action="Deny"/> <action action_id="5" action="Add Comments"/> <action action_id="6" action="Add Attachments"/> </data> <data id="statuses"> <status status_id="1" status="Deleted"/> <status status_id="2" status="Approved"/> <status status_id="3" status="Denied"/> <status status_id="4" status="Completed"/> </data> </xml> -------- output.html -------- <?xml-stylesheet type="text/xsl" href="test.xsl"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Test</title> </head> <body> <form action="transform.aspx" method="post"> <div id="status_table_1"> <h2 id="status_table_1:status_header" class="dark">Deleted</h2> <table border="1" cellpadding="4" width="650px"> <tr> <td width="25%"> <strong>Request Date</strong> </td> <td width="15%"> <strong>Change Title</strong> </td> <td width="15%"> <strong>Status</strong> </td> <td width="45%"> <strong>Action</strong> </td> </tr> </table> <br> </div> <div id="status_table_2"> <h2 id="status_table_2:status_header" class="dark">Approved</h2> <table border="1" cellpadding="4" width="650px"> <tr> <td width="25%"> <strong>Request Date</strong> </td> <td width="15%"> <strong>Change Title</strong> </td> <td width="15%"> <strong>Status</strong> </td> <td width="45%"> <strong>Action</strong> </td> </tr> <tr id="status_table_2:ac_request_1"> <td id="status_table_2:ac_request_1:request_date" class="dark">2005-03-09 9:12:44</td> <td id="status_table_2:ac_request_1:change_title" class="dark">test2</td> <td id="status_table_2:ac_request_1:status" class="dark">Approved</td> <td> <label for="status_table_2:ac_request_1:action">Action:</label> <select id="status_table_2:ac_request_1:action" name="status_table_2:ac_request_1:action"> <option value="0">Select an action</option> <option id="status_table_2:ac_request_1:action_list_1" value="1">View</option> <option id="status_table_2:ac_request_1:action_list_2" value="2">Delete</option> <option id="status_table_2:ac_request_1:action_list_3" value="3">Approve</option> <option id="status_table_2:ac_request_1:action_list_4" value="4">Deny</option> <option id="status_table_2:ac_request_1:action_list_5" value="5">Add Comments</option> <option id="status_table_2:ac_request_1:action_list_6" value="6">Add Attachments</option> </select> <span id="status_table_2:ac_request_1:test">test:</span> <input type="submit" name="status_table_2:ac_request_1:submit" value="Go"> </td> </tr> <tr id="status_table_2:ac_request_2"> <td id="status_table_2:ac_request_2:request_date" class="dark">2005-03-09 9:14:15</td> <td id="status_table_2:ac_request_2:change_title" class="dark">test23</td> <td id="status_table_2:ac_request_2:status" class="dark">Approved</td> <td> <label for="status_table_2:ac_request_2:action">Action:</label> <select id="status_table_2:ac_request_2:action" name="status_table_2:ac_request_2:action"> <option value="0">Select an action</option> <option id="status_table_2:ac_request_2:action_list_1" value="1">View</option> <option id="status_table_2:ac_request_2:action_list_2" value="2">Delete</option> <option id="status_table_2:ac_request_2:action_list_3" value="3">Approve</option> <option id="status_table_2:ac_request_2:action_list_4" value="4">Deny</option> <option id="status_table_2:ac_request_2:action_list_5" value="5">Add Comments</option> <option id="status_table_2:ac_request_2:action_list_6" value="6">Add Attachments</option> </select> <span id="status_table_2:ac_request_2:test">test:</span> <input type="submit" name="status_table_2:ac_request_2:submit" value="Go"> </td> </tr> </table> <br> </div> <div id="status_table_3"> <h2 id="status_table_3:status_header" class="dark">Denied</h2> <table border="1" cellpadding="4" width="650px"> <tr> <td width="25%"> <strong>Request Date</strong> </td> <td width="15%"> <strong>Change Title</strong> </td> <td width="15%"> <strong>Status</strong> </td> <td width="45%"> <strong>Action</strong> </td> </tr> <tr id="status_table_3:ac_request_1"> <td id="status_table_3:ac_request_1:request_date" class="dark">2005-03-09 9:14:15</td> <td id="status_table_3:ac_request_1:change_title" class="dark">test3</td> <td id="status_table_3:ac_request_1:status" class="dark">Denied</td> <td> <label for="status_table_3:ac_request_1:action">Action:</label> <select id="status_table_3:ac_request_1:action" name="status_table_3:ac_request_1:action"> <option value="0">Select an action</option> <option id="status_table_3:ac_request_1:action_list_1" value="1">View</option> <option id="status_table_3:ac_request_1:action_list_2" value="2">Delete</option> <option id="status_table_3:ac_request_1:action_list_3" value="3">Approve</option> <option id="status_table_3:ac_request_1:action_list_4" value="4">Deny</option> <option id="status_table_3:ac_request_1:action_list_5" value="5">Add Comments</option> <option id="status_table_3:ac_request_1:action_list_6" value="6">Add Attachments</option> </select> <span id="status_table_3:ac_request_1:test">test:</span> <input type="submit" name="status_table_3:ac_request_1:submit" value="Go"> </td> </tr> </table> <br> </div> <div id="status_table_4"> <h2 id="status_table_4:status_header" class="dark">Completed</h2> <table border="1" cellpadding="4" width="650px"> <tr> <td width="25%"> <strong>Request Date</strong> </td> <td width="15%"> <strong>Change Title</strong> </td> <td width="15%"> <strong>Status</strong> </td> <td width="45%"> <strong>Action</strong> </td> </tr> <tr id="status_table_4:ac_request_1"> <td id="status_table_4:ac_request_1:request_date" class="dark">2005-03-09 9:11:56</td> <td id="status_table_4:ac_request_1:change_title" class="dark">test</td> <td id="status_table_4:ac_request_1:status" class="dark">Completed</td> <td> <label for="status_table_4:ac_request_1:action">Action:</label> <select id="status_table_4:ac_request_1:action" name="status_table_4:ac_request_1:action"> <option value="0">Select an action</option> <option id="status_table_4:ac_request_1:action_list_1" value="1">View</option> <option id="status_table_4:ac_request_1:action_list_2" value="2">Delete</option> <option id="status_table_4:ac_request_1:action_list_3" value="3">Approve</option> <option id="status_table_4:ac_request_1:action_list_4" value="4">Deny</option> <option id="status_table_4:ac_request_1:action_list_5" value="5">Add Comments</option> <option id="status_table_4:ac_request_1:action_list_6" value="6">Add Attachments</option> </select> <span id="status_table_4:ac_request_1:test">test:</span> <input type="submit" name="status_table_4:ac_request_1:submit" value="Go"> </td> </tr> </table> <br> </div> </form> </body> </html> On 5/10/05, omprakash(_dot_)v(_at_)polaris(_dot_)co(_dot_)in <omprakash(_dot_)v(_at_)polaris(_dot_)co(_dot_)in> wrote: Hi, Please find below the xsl stylesheet that does what you want. Since you want 1 select box for each AC_Request in your source xml, there has to be a distinguishing factor in your source xml. I could only find that the time differed in the RequestDate attribute in the 3 AC_Request elements. Therefore I have used the concatenation of the RequestDate and the result of the generate-id function as the value of each option. The only problem I can foresee is due to the ever-changing nature of time, the option values too would keep changing resulting in your having to change your server side programs each time the source xml changes. I would suggest you add another attribute to that is unique across AC_REQUEST and is more deterministic. Hope this helps. <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system ="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" /> <xsl:variable name="priorities" select="document ('priority.xml')" /> <xsl:template match="/"> <html> <head> <title>Test</title> </head> <body> <form action="transform.aspx" method="post"> <table border="1" cellpadding="4"> <tr> <td><strong>Request Date</strong></td> <td><strong>Change Title</strong></td> <td><strong>Status</strong></td> <td><strong>Action</strong></td> </tr> <xsl:for-each select ="/AC_Requests/AC_Request"> <xsl:variable name="acreq" select ="."/> <tr> <td><xsl:value-of select="@RequestDate" /></td> <td><xsl:value-of select="@ChangeTitle" /></td> <td><xsl:value-of select="@StatusID" /></td> <td> <label for ="action">Priority: </label> <select id ="action" name="action"> <option value="0">Select an action</option> <xsl:for-each select="$priorities/priorities/priority"> <xsl:message><td><xsl:value-of select="concat(normalize-space ($acreq/@RequestDate), generate-id(.))" /></td></xsl:message> <option value="{concat(generate-id(.), normalize-space ($acreq/@RequestDate))}"><xsl:value-of select="@priority" /></option> </xsl:for-each> </select> <input type ="submit" name="submit" value="Go" /> </td> </tr> </xsl:for-each> </table> </form> </body> </html> </xsl:template> </xsl:stylesheet> Cheers, Omprakash.V Jeremy Marzka <jmarzka(_at_)gmai To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com l.com> cc: (bcc: omprakash.v/Polaris) Subject: [xsl] Unique html name, id, and for form fields 05/11/2005 02:00 AM Please respond to xsl-list I am working on creating an html report from two xml sources. The first source is the xml that needs to be displayed in the report. The second source is used to populate a select box. The problem i'm having is with the multiple for-each's necessary to do this. In the for-each's I need to generate unique id's, name's and for's in a way that they can be processed server side later. Here is what I currently have: ----test.xsl---- <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl=" http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system=" http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" /> <xsl:variable name="priorities" select="document ('priorities.xml')" /> <xsl:template match="/"> <html> <head> <title>Test</title> </head> <body> <form action="transform.aspx" method="post"> <table border="1" cellpadding="4"> <tr> <td><strong>Request Date</strong></td> <td><strong>Change Title</strong></td> <td><strong>Status</strong></td> <td><strong>Action</strong></td> </tr> <xsl:for-each select ="/AC_Requests/AC_Request"> <tr> <td><xsl:value-of select="@RequestDate" /></td> <td><xsl:value-of select="@ChangeTitle" /></td> <td><xsl:value-of select="@StatusID" /></td> <td> <label for ="action">Priority: </label> <select id ="action" name="action"> <option value="0">Select an action</option> <xsl:for-each select="$priorities/priorities/priority"> <option value="{(_at_)priority_id}"><xsl:value-of select="@priority" /></option> </xsl:for-each> </select> <input type ="submit" name="submit" value="Go" /> </td> </tr> </xsl:for-each> </table> </form> </body> </html> </xsl:template> </xsl:stylesheet> ---- test.xml ---- <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="test.xsl"?> <AC_Requests> <AC_Request RequestDate="2005-03-09 9:11:56" ChangeTitle="test" StatusID="3"/> <AC_Request RequestDate="2005-03-09 9:12:44" ChangeTitle="test" StatusID="3"/> <AC_Request RequestDate="2005-03-09 9:14:15" ChangeTitle="test" StatusID="2"/> </AC_Requests> ---- priorities.xml ---- <?xml version="1.0"?> <priorities> <priority priority_id="1" priority="Hold"/> <priority priority_id="2" priority="High"/> <priority priority_id="3" priority="Med"/> <priority priority_id="4" priority="Low"/> </priorities> ---- OUTPUT ---- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Test</title> </head> <body> <form action="transform.aspx" method="post"> <table border="1" cellpadding="4"> <tr> <td> <strong>Request Date</strong> </td> <td> <strong>Change Title</strong> </td> <td> <strong>Status</strong> </td> <td> <strong>Priority</strong> </td> <td> <strong>Action</strong> </td> </tr> <tr> <td>2005-03-09 9:11:56</td> <td>test</td> <td>3</td> <td> <select id="action" name="action"> <option value="0">Select an action</option> <option value="1">Hold</option> <option value="2">High</option> <option value="3">Med</option> <option value="4">Low</option> </select> </td> <td> <input type="submit" name="submit" value="Go"> </td> </tr> <tr> <td>2005-03-09 9:12:44</td> <td>test</td> <td>3</td> <td> <select id="action" name="action"> <option value="0">Select an action</option> <option value="1">Hold</option> <option value="2">High</option> <option value="3">Med</option> <option value="4">Low</option> </select> </td> <td> <input type="submit" name="submit" value="Go"> </td> </tr> <tr> <td>2005-03-09 9:14:15</td> <td>test</td> <td>2</td> <td> <select id="action" name="action"> <option value="0">Select an action</option> <option value="1">Hold</option> <option value="2">High</option> <option value="3">Med</option> <option value="4">Low</option> </select> </td> <td> <input type="submit" name="submit" value="Go"> </td> </tr> </table> </form> </body> </html> --~------------------------------------------------------------------ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/ or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com> --~-- This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately. You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited. Visit Us at http://www.polaris.co.in --~------------------------------------------------------------------ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/ or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com> --~-- --~------------------------------------------------------------------ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/ or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com> --~--
|
|