SQL Server : count XML Children within Parent - sql

Hi I have been looking for a way of updating xml produced in sql to add the count of child nodes and add the count to the parent node using SQL
I've tried various workings around
SET #xml.modify
(
'insert attribute TrCount {count(/XML/Acs/Act/Trs/Tr)} into (./XML/Acts/Act/Trs)'
);
<XML>
<Id>12</Id>
<DateCreated>2013-04-11</DateCreated>
<Acts AccountCount="4">
<Act RowID="1>
<Trs>
<Tr RowID="1" />
</Trs>
</Act>
<Act RowID="2">
<Trs>
<Tr RowID="1" />
<Tr RowID="2" />
</Trs>
</Act>
<Act RowID="3">
<Trs>
<Tr RowID="1"/>
</Trs>
</Act>
<Act RowID="4">
<Trs>
<Tr RowID="1" />
<Tr RowID="2" />
<Tr RowID="3" />
<Tr RowID="4" />
<Tr RowID="5" />
<Tr RowID="6" />
<Tr RowID="7" />
</Trs>
</Act>
</Acts>
</XML>
above is a sample, I have got it to give me a count of within but I can not seem to get a count of each within each , so the result would look like
<Act RowID="1>
<Trs Trcount="1">
<Tr RowID="1" />
</Trs>
</Act>
<Act RowID="2">
<Trs Trcount="2">
<Tr RowID="1" />
<Tr RowID="2" />
</Trs>
</Act>
<Act RowID="3">
<Trs Trcount="1">
<Tr RowID="1"/>
</Trs>
</Act>
<Act RowID="4">
<Trs Trcount="7">
<Tr RowID="1" />
<Tr RowID="2" />
<Tr RowID="3" />
<Tr RowID="4" />
<Tr RowID="5" />
<Tr RowID="6" />
<Tr RowID="7" />
</Trs>
Cheers

Try something like this, which assumes each Act contains only one Trs:
declare #count int = #xml.value('count(/XML/Acts/Act)', 'int'),
#counter int = 1;
while (#counter <= #count)
begin
SET #xml.modify ('insert attribute TrCount {count(/XML/Acts/Act[sql:variable("#counter")]/Trs/Tr)} into (/XML/Acts/Act[sql:variable("#counter")]/Trs)[1]');
set #counter += 1;
end
insert() can only modify a singleton, not a whole set of nodes in one go.

Related

struts2 if condition with property or tiles-attribute doesnt work

i have problems with my if condition using struts 2.
I have a tiles-definition using a put-list-attribute with list-attributes as menuitems.
In my menu.jsp i iterate over the put-list to render all my menuitems.
Now i try to check if the value of my item contains a special character.
Here is my code:
tiles.xml
<definition name="start.menu" template="/WEB-INF/menu.jsp" >
<put-attribute name="title" value="Hauptmenu" />
<put-list-attribute name="webThemes" >
<add-list-attribute>
<add-attribute value="Test" id="name"/>
<add-attribute value="atest" />
</add-list-attribute>
<add-list-attribute>
<add-attribute value="System Test"/>
<add-attribute value="test" />
</add-list-attribute>
</put-list-attribute>
</definition>
menu.jsp
<%# taglib prefix="s" uri="/struts-tags" %>
<%# taglib uri="/tiles-jsp" prefix="tiles" %>
<%# taglib uri="/tiles-extras-jsp" prefix="tilesx" %>
<%-- Push tiles attributes in page context --%>
<tiles:importAttribute />
<tilesx:useAttribute id="list" name="webThemes" classname="java.util.List" />
<table class="menu" border="0px" width="100%" cellspacing="0px" cellpadding="0px" >
<s:iterator value="#attr.webThemes" var="item">
<s:if test="%{#item.value[1].contains('a')}">
<s:set var="link" value="111"/>
</s:if>
<s:else>
<s:set var="link" value="#item.value[1]"/>
</s:else>
<tr>
<td valign="top" >
<font size="-1">
<a href="<s:property value="#link"/>"
class="menu">
<s:property value="#item.value[0]" />
<s:property value="#item.value[1]" />
</a>
</font>
</td>
</tr>
</s:iterator>
But the condition is always false.
I tried different approaches but nothing worked:
<s:if test="%{#item.value[1].contains('a')}">
<s:if test="#item.value[1].contains('a')">
<s:if test='%{#item.value[1].contains("a")}'>
<s:set var="itemvalue" value="#item.value[1]"/>
<s:property value="itemvalue"/>
<s:if test='%{#itemvalue.contains("a")}'>
<s:if test='#itemvalue.contains("a")'>
<s:if test='%{#item.value[1] == "atest"}'>
So how can i get the condition working? What am i doing wrong?
Thanks!!!
Ok... i tried 2 days until i posted my question.
but after posting a found a working solution in an hour...:
<s:if test='(#item.value[1].value).startsWith("a")'>

Vertical Table in Material-UI

By vertical table, I mean the first column of each row is a header. Like this:
<table>
<tr>
<th>Header 1</th>
<td>Cell 1</td>
</tr>
<tr>
<th>Header 2</th>
<td>Cell 2</td>
</tr>
</table>
The problem is, to render the <TableCell /> to be a <th /> we need to wrap it inside <TableHead />, otherwise it will render to <td />. But <TableHead /> also cannot be placed inside <TableRow /> since it renders to <thead />. So the following code won't work:
<Table>
<TableRow>
<TableHead>
<TableCell>Header 1</TableCell>
</TableHead>
<TableCell>Cell 1</TableCell>
</TableRow>
<TableRow>
<TableHead>
<TableCell>Header 1</TableCell>
</TableHead>
<TableCell>Cell 2</TableCell>
</TableRow>
</Table>
Then, how can I achieve the first example using Material-UI?
Ohh, Nevermind.
I am not keen enough to see the variant property in the documentation. All you need to do is set the variant property of <TableCell /> to head.
<Table>
<TableRow>
<TableCell variant="head">Header 1</TableCell>
<TableCell>Cell 1</TableCell>
</TableRow>
<TableRow>
<TableCell variant="head">Header 1</TableCell>
<TableCell>Cell 2</TableCell>
</TableRow>
</Table>
The approved solution will generate the following error message :
<tr> cannot appear as a child of <table>. Add a <tbody>, <thead> or <tfoot> to your code to match the DOM tree generated by the browser
So you may want to add a <tbody> tag as below :
<Table>
<TableBody>
<TableRow>
<TableCell variant="head">Header 1</TableCell>
<TableCell>Cell 1</TableCell>
</TableRow>
<TableRow>
<TableCell variant="head">Header 1</TableCell>
<TableCell>Cell 2</TableCell>
</TableRow>
</TableBody>
</Table>

<JSP> How to display one data from multiple row

I have a query that gives me 3 data under 1 parameter. Following is query.
Query
SELECT
VQ_CD
, FILE_NM
FROM
TB_POT_ECD_VQ_INFO
WHERE
PRCS_SNO = '1'
When I run the query, the data I get is..
PRCS_SNO VQ_CD FILE_NM
1 500K A
1 1000K B
1 2000K C
And in JSP page, I want to display all three data.
What I did in JSP is this.
Oh, table is being called encVqList.
JSP
<td class="typeFD bgN"">
<c:set var="V500K" value="" />
<c:forEach var="encVqList" items="${encVqList}" >
<input type="text" id="500K" name="500K" value="<c:out value="${encVqList.fileNm}" />"/>
</c:forEach>
<td class="typeFD bgN"">
<c:set var="V1000K" value="" />
<c:forEach var="encVqList" items="${encVqList}" >
<input type="text" id="1000K" name="1000K" value="<c:out value="${encVqList.fileNm}" />"/>
</c:forEach>
<td class="typeFD bgN"">
<c:set var="V2000K" value="" />
<c:forEach var="encVqList" items="${encVqList}" >
<input type="text" id="2000K" name="2000K" value="<c:out value="${encVqList.fileNm}" />"/>
</c:forEach>
And did the same thign for 1000K and 2000K. But it doesn't work.
I think I should distinguish each input box does not recognize which one since it has 3 data under 1 same parameter. So can anyone help?
<td class="typeFD bgN"">
<c:forEach var="encVqList" items="${encVqList}" >
<c:choose>
<c:when test="${encVqList.VQ_CD eq '500K'}">
<input type="text" id="500K" name="500K" value="<c:out value='${encVqList.fileNm}' />" />
</c:when>
<c:when test="${encVqList.VQ_CD eq 'V1000K'}">
<input type="text" id="1000K" name="1000K" value="<c:out value='${encVqList.fileNm}' />" />
</c:when>
<c:otherwise>
<input type="text" id="2000K" name="2000K" value="<c:out value='${encVqList.fileNm}' />" />
<c:otherwise>
</c:choose>
</c:forEach>
</td>

"onclick=submit()" in a <table> created through struts taglib <html:forEach>

I'd like to send information to an ActionFormBean using a jsp page that contains a structured code like this:
<table>
<thead>
<tr>
<th>ID Turno</th>
<th>Data e Ora</th>
<th>Sede turno</th>
</tr>
</thead>
<tbody>
<html:form action="/dettagliTurno">
<c:forEach var="ris" items="${usersession.searchResult}" >
<tr>
<td><c:out value="${ris.idTurno}"/></td>
<td><c:out value="${ris.dataOra}"/></td>
<td><c:out value="${ris.luogo}"/></td>
<td>
<html:hidden property="id" value="${ris.idTurno}"/>
<html:submit value="Dettagli" property="id" />
</td>
</tr>
</c:forEach>
</html:form>
</tbody>
</table>
The only one information that I need to send to the ActionFormBean is "id" but if click on the button "Dettagli" of the second/third row (of the example showed in this image) the value sent to the ActionForm is always the one of the first row (that is '5')!
How can I solve this problem and set the correct value of id according to the row-button "Dettagli" selected?
GOT IT!
I solved the problem by changing code this way:
<c:forEach var="ris" items="${usersession.searchResult}" >
<tr>
**<html:form action="/dettagliTurno">**
<td><c:out value="${ris.idTurno}"/></td>
<td><c:out value="${ris.dataOra}"/></td>
<td><c:out value="${ris.luogo}"/></td>
<td>
<html:hidden property="id" value="${ris.idTurno}"/>
<html:submit value="Dettagli" property="id" />
</td>
**</html:form>**
</tr>
With this version, form set the correct value of "idTurno" and properly send it to ActionForm.
Hope it will be helpfulto someone with the same problem of mine.
Calc

Coldfusion/SQL: Element Id is not defined in form

When I attempt to delete an item in a shopping cart. I get a "Element Id is not defined in form" error.
Where am I going wrong?
I'm using MSSQL 2008 r2, Coldfusion 10.
Summary:
tickets.cfm this is the page that displays the products, also contains a form with hidden values to be passed on to cart_manage.cfm.
cart_manage.cfm is an action page for both tickets.cfm and cartlist.cfm
cartlist.cfm is a page that displays the shopping cart contents.
application.cfm session variables.
application.cfm
<cfapplication sessionmanagement="yes">
<cfapplication name="cart" clientmanagement="Yes"
sessionmanagement="Yes"
sessiontimeout="#CreateTimeSpan(0,0,15,0)#"
applicationtimeout="#CreateTimeSpan(0,2,0,0)#">
<cfparam name="session.allowin" default="false">
tickets.cfm
<cfif NOT IsDefined('SESSION.cart')>
<cfset SESSION.cart = ArrayNew(1) />
</cfif>
<cfquery datasource="sqltest" name="getTickets">
select *,
CASE WHEN Friday=1and Saturday=1and Sunday=1
THEN 'All three days'
WHEN Friday=0and Saturday=0and Sunday=0
THEN 'None'
ELSE
STUFF(
case when Friday=1 then ',Friday' else '' end
+ case when Saturday=1 then ',Saturday' else '' end
+ case when Sunday=1 then ',Sunday' else '' end, 1,1,'')
END WhichDays
from tickets_performances;
</cfquery>
<table width="600" border="0">
<tr>
<td>Day</td>
<td>Price</td>
<td>How Many Left</td>
<td>Quantity</td>
</tr>
<p> You can only purchase a maximum of two tickets at a time. Having a ticket limit ensures fairness to all those buying tickets. The ticket limit applies per account, billing address, and/or credit card. Please observe the ticket limit as over purchases may be cancelled without notice or warning. </p>
<cfform action="cart_manage.cfm" name="form" method="post">
<cfoutput query="getTickets">
<tr>
<td>#WhichDays#</td>
<td>£#price#</td>
<td>#stock#</td>
<td><cfinput type="text" id="quantity" name="quantity" size="5" class="field" maxlength="1" value="0"/></td>
<td><cfinput type="hidden" name="id" value="#getTickets.ticket_performanceID#" />
<cfinput type="hidden" name="item" value="#getTickets.WhichDays#" />
<cfinput type="hidden" name="price" value="#getTickets.price#" />
<cfinput type="submit" name="add_button" value="Add to Cart"></td>
</tr>
</cfoutput>
</cfform>
</table>
cart_manage.cfm
<cfset newitem = 0>
<cfloop from="1" to="#arrayLen(session.cart)#" index="i">
<cfif session.cart[i].itemid EQ #form.id#>
<cfset session.cart[i].quantity = session.cart[i].quantity + #form.quantity#>
<cfset newitem = 1>
<cfbreak>
</cfif>
</cfloop>
<cfif newitem EQ 0>
<cfset temp = arrayAppend(session.cart, structNew())>
<cfset session.cart[arrayLen(session.cart)].itemid = #form.id#>
<cfset session.cart[arrayLen(session.cart)].item = #form.item#>
<cfset session.cart[arrayLen(session.cart)].quantity = #form.quantity#>
<cfset session.cart[arrayLen(session.cart)].price = #form.price#>
<cflocation url="cartlist.cfm">
</cfif>
<cfif IsDefined('FORM.delete_button.y')>
<cfloop from="#ListLen(FORM.delete_index)#" to="1" index="i" step="-1">
<cfset ArrayDeleteAt(SESSION.cart, ListGetAt(FORM.delete_index, i))>
</cfloop>
<cflocation url="cartlist.cfm">
<cfelseif IsDefined('FORM.update_button.y')>
<cfloop from="1" to="#ArrayLen(SESSION.cart)#" index="i">
<cfset SESSION.cart[i].quantity = FORM["quantity_" & i] >
</cfloop>
<cflocation url="cartlist.cfm">
<cfelseif IsDefined('FORM.checkout_button.y')>
<cflocation url="checkout.cfm">
</cfif>
<cflocation url="cartlist.cfm">
cartlist.cfm
<cfset nTotal = 0 />
<cfform action="cart_manage.cfm" method="post">
<table width="100%">
<tr valign="top">
<td>
<table width="100%" class="white">
<tr>
<td class="tblehead"> </td>
<td class="tblehead">Item</td>
<td class="tblehead">Price Per Item</td>
<td class="tblehead">Quantity</td>
<td class="tblehead">Price</td>
</tr>
<cfoutput>
<cfloop from="1" to="#ArrayLen(SESSION.cart)#" index="i">
<tr>
<td height="40" width="40" align="center" class="dkturq">
<cfinput type="checkbox" name="delete_index" value="#i#" />
</td>
<td height="40" class="dkturq">
#SESSION.cart[i].item#
</td>
<td height="40" class="dkturq">
£#(SESSION.cart[i].price)#
</td>
<td height="40" class="dkturq">
<cfinput type="text" name="quantity_#i#" value="#SESSION.cart[i].quantity#" size="5" class="field" />
</td>
<td height="40" class="dkturq">
<cfset nPrice = SESSION.cart[i].quantity * SESSION.cart[i].price />
<cfset nTotal = nTotal + nPrice />
£#(nPrice)#
</td>
</tr>
</cfloop>
</cfoutput>
</table>
</td>
<td></td>
<td>
<table width="100%" height="100%" class="white">
<tr>
<td class="tblehead">
Summary
</td>
</tr>
<tr>
<td class="dkturq">
total:
<cfoutput>£#(nTotal)#</cfoutput>
<br /><br /><br /><br />
Clear Shopping Cart
<cfinput type="submit" name="update_button" id="update_button" value="update" />
<cfinput type="submit" name="delete_button" id="delete_button" value="delete" /><br /></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</cfform>
CFDUMP of FORM
CFDUMP of Session.cart
Error:
CFDUMP when I press delete
Thanks to Leigh, I managed to get it to work.
(Summary from the chat ..)
The original problem is pretty much what the error message said. ie Trying to use a form field that does not exist. The cause is "tickets.cfm" and "cartList.cfm" contain different form fields. The form in "cartList.cfm" does not contain a field named form.id, hence the error when that form is submitted. To avoid the error you need to verify the form.id exists, with structKeyExists() before accessing it.
However, we decided to revise the action page code instead. Also, the "cartList.cfm" form was changed to use itemID instead of index. Reason being, the index can change which might cause the wrong item to be deleted or updated. There is still room for improvement, but here is the jist of the changes:
*cartList.cfm (form fields) *
<!--- use itemID's instead of "index" in all form fields --->
<cfinput type="checkbox" name="delete_itemID" value="#SESSION.cart[i].itemid#" />
<cfinput type="text" name="quantity_#SESSION.cart[i].itemid#" value="#SESSION.cart[i].quantity#" size="5" class="field" /><br>
cartManage.cfm
<!--- ADD item to cart ---->
<cfif structKeyExists(FORM, "add_button")>
... code to add items here ...
<!--- DELETE from cart ---->
<cfelseif structKeyExists(FORM, "delete_button")>
<!---
Ensure the field exists to prevent errors. Note: A more
efficient option is to test the field's existence in the cfelseif
--->
<cfparam name="FORM.delete_itemID" default="">
<cfloop from="#ArrayLen(SESSION.cart)#" to="1" index="i" step="-1">
<!--- if this item was marked as "deleted", remove it --->
<cfif listFind(FORM.delete_itemID, SESSION.cart[i].itemID)>
<cfset arrayDeleteAt(SESSION.cart, i)>
</cfif>
</cfloop>
<!--- UPDATE item in cart ---->
<cfelseif structKeyExists(FORM, "update_button")>
<cfloop from="1" to="#ArrayLen(SESSION.cart)#" index="i">
<cfset currentItem = session.cart[i]>
<!--- Note: For safety, verify the field exists first --->
<cfset currentItem.quantity = FORM["quantity_" & currentItem.itemID] >
</cfloop>
</cfif>