How do I access an attribute from the outer node inside an inner condition? - xpath

I want to perform the following XPath:
/Configs/Category/InputMenu/Config[#Value = 'DualPack' and (/Configs/Category/MasterSlave/Config[#No = ./#No]/#Value = 'Master')]
Where the "./#No" in the part"[#No = ./#No]" is from /Configs/Category/InputMenu/Config#No, not from /Configs/Category/MasterSlave/Config#No
How can I specify that the ./#No is from that outer node?
thanks - dave

In XPath 2, you could use for to simulate the let of XQuery:
/Configs/Category/InputMenu/Config[#Value = 'DualPack' and (for $no in ./#No return /Configs/Category/MasterSlave/Config[#No = $no]/#Value = 'Master')]
Otherwise you could turn it around, and check if the No attribute is equal to the No attribute of one element that has a Master value (instead checking if the value of one element with the same No attribute is Master).
/Configs/Category/InputMenu/Config[#Value = 'DualPack' and #No = /Configs/Category/MasterSlave/Config[#Value = 'Master']/#No]

Related

Use of regEx with multiple categories

I need to rotate the match through variables Cat1 to Catx as long as there is data for the Cat'x'. Whenever I do this this, it does not consider as a variable but the literal Cat4 or Cat5 instead of the variable Cat4 & Cat5 when I try to compile the new category label. Such as the following with i increasing until there is no value to the variable searched for .. i.e. Cat57 has nothing assigned.
category = "Cat"+i
This is the portion of my code I believe that needs to be adjusted .. essentially based on the category I am going to assign it a specific column in my spreadsheet (this part hasn't been added yet) .. still stuck on the matching through multiple categories
if(studentmarks && studentmarks.length > 0 && assign.maxPoints > 0){
for (d = 0; d < studentmarks.length; d++){
var marks = studentmarks[d];
if(student.userId == marks.userId){
var ss = SpreadsheetApp.openByUrl(url).getSheetByName(shet);
var re = RegExp(Cat1);
if (assign.title.match(re))
ss.appendRow([category, assign.title, marks.assignedGrade,
assign.maxPoints]);
As we have talked in the comments there is no need to actually use RegEx in this case.
Just define an array and iterate through. Without the need
// Define new array
var Cat = [];
//
// Do stuf to populate Cat array
//
//Access your data
for(var i = 0; i < Cat.length; i++){
var element = Cat[i];
}

XML - How to pull out repeating children nodes

I am trying to extract repeating child elements from an xpath.
This is a sample of the XML:
<productDetail>
<productTypeCode>123</productTypeCode>
<productPrice currency="EUR">13.27</productPrice>
<productPrice currency="US">15</productPrice>
</productDetail>
As you can see the productPrice currency node is repeating.
I am able to pull out one of them by looping through each of the elements:
#node.children.each do |c|
if c.name == "productDetail"
info = {}
productTypeCode = nil
c.children.each do |gc|
name = gc.name
if name == "productTypeCode"
productTypeCode = gc.text
elsif name == "productPrice"
info["productPrice"] = gc.text
attrs = gc.attributes
info["productPrice_cur"] = attrs["currency"].value
end
As you can see I only have the "productPrice" information once in the loop, but there are two of them in the XML data.
How do I access both of the values, seen as the xpath and value names are the same?
I am coding this in Ruby.

How to populate the table dynamically and correctly with Ajax?

I have a form where the user submits a query and then have a Servlet that processes this query and returns the results in XML. With this result trying to populate a table dynamically via Ajax, for such, I use the following code below.
var thead = $("<thead>");
var rowsTHead = $("<tr>");
var tbody = $("<tbody>");
var numberOfColumns;
$(xml).find("head").each(function(){
var variable = $(this).find("variable");
numberOfColumns = variable.length;
for (var i = 0; i < variable.length; i++){
var name = $(variable[i]).attr("name");
rowsTHead.append($("<th>").html(name));
}
});
thead.append(rowsTHead);
$(xml).find("result").each(function(){
var literal = $(this).find("literal");
var rowsTBody = $("<tr class=\"even\">");
literal.length = numberOfColumns;
for (var j = 0; j < literal.length; j++){
var tdBody = $("<td>");
tdBody.html($(literal[j]).text());
rowsTBody.append(tdBody);
}
tbody.append(rowsTBody);
});
$(".tablesorter").empty()
.append(thead)
.append(tbody);
This code works perfectly until it was used in a UNION query. When using a UNION the returned xml comes in the following way http://pastebin.com/y7hXK1Zy
As can be observed, this query has 4 variables that are: gn1, indication1, gn2, indication2.
What is going wrong is that the values of all the variables being written in columns corresponding to gn1 and indication1.
What I wish I was to write the value of each variable in its corresponding column. I wonder what should I change in my code to make this possible.
You need to respect the name values of the binding elements, and relate them back to the columns that you correctly built from parsing the element. When you are doing the find "literal", you are skipping the parsing of the binding elements. You should find "binding", respect the name and look up which column to use based on that, and then for each of those, find the "literal" elements for the actual values.

Multiple Counts within a single query

I want a list of counts for some of my data (count the number of open.closed tasks etc), I want to get all counts inside 1 query, so I am not sure what I do with my linq statement below...
_user is an object that returns info about the current loggedon user
_repo is am object that returns an IQueryable of whichever table I want to select
var counters = (from task in _repo.All<InstructionTask>()
where task.AssignedToCompanyID == _user.CompanyID || task.CompanyID == _user.CompanyID
join instructions in _repo.GetAllMyInstructions(_user) on task.InstructionID equals
instructions.InstructionID
group new {task, instructions}
by new
{
task
}
into g
select new
{
TotalEveryone = g.Count(),
TotalMine = g.Count(),
TotalOpen = g.Count(x => x.task.IsOpen),
TotalClosed = g.Count(c => !c.task.IsOpen)
}).SingleOrDefault();
Do I convert my object to single or default? The exception I am getting is, this sequence contains more than one element
Note: I want overall stats, not for each task, but for all tasks - not sure how to get that?
You need to dump everything into a single group, and use a regular Single. I am not sure if LINQ-to-SQL would be able to translate it correctly, but it's definitely worth a try.
var counters = (from task in _repo.All<InstructionTask>()
where task.AssignedToCompanyID == _user.CompanyID || task.CompanyID == _user.CompanyID
join instructions in _repo.GetAllMyInstructions(_user) on task.InstructionID == instructions.InstructionID
group task by 1 /* <<=== All tasks go into one group */ into g select new {
TotalEveryone = task.Count(),
TotalMine = task.Count(), // <<=== You probably need a condition here
TotalOpen = task.Count(x => x.task.IsOpen),
TotalClosed = task.Count(c => !c.task.IsOpen)
}).Single();
From MSDN
Returns the only element of a sequence, or a default value if the
sequence is empty; this method throws an exception if there is more
than one element in the sequence.
You need to use FirstOrDefault. SingleOrDefault is designed for collections that contains exactly 1 element (or none).

How to order integers according to size and track their positions by variable name

I have a program with multiple int variables where individual counts are added to the specific variable each time a set fail condition is encountered. I want the user to be able to track how many failures of each category they have encountered by a button click. I want to display the range on a datagridview in order from highest value integer down to lowest. I also need to display in the adjacent column the name of the test step that relates to the value. My plan was to use Array.sort to order the integers but i then lose track of their names so cant assign the adjacent string column. I tried using a hashtable but if i use the string as a key it sorts alphabetically not numerically and if i use the integer as a key i get duplicate entries which dont get added to the hash table. here is some of the examples i tried but they have the aforementioned problems. essentially i want to end with two arrays where the order matches the naming and value convention. FYI the variables were declared before this section of code, variables ending in x are the string name for the (non x) value of the integer.
Hashtable sorter = new Hashtable();
sorter[download] = downloadx;
sorter[power] = powerx;
sorter[phase] = phasex;
sorter[eeprom] = eepromx;
sorter[upulse] = upulsex;
sorter[vpulse] = vpulsex;
sorter[wpulse] = wpulsex;
sorter[volts] = voltsx;
sorter[current] = currentx;
sorter[ad] = adx;
sorter[comms] = commsx;
sorter[ntc] = ntcx;
sorter[prt] = prtx;
string list = "";
string[] names = new string[13];
foreach (DictionaryEntry child in sorter)
{
list += child.Value.ToString() + "z";
}
int[] ordered = new int[] { download, power, phase, eeprom, upulse, vpulse, wpulse, volts, current, ad, comms, ntc, prt };
Array.Sort(ordered);
Array.Reverse(ordered);
for (int i = 0; i < sorter.Count; i++)
{
int pos = list.IndexOf("z");
names[i] = list.Substring(0, pos);
list = list.Substring(pos + 1);
}
First question here so hope its not too longwinded.
Thanks
Use a Dictionary. And you can order it by the value : myDico.OrderBy(x => x.Value).Reverse(), the sort will be numerical descending. You just have to enumerate the result.
I hope I understand your need. Otherwise ignore me.
You want to be using a
Dictionary <string, int>
to store your numbers.I'm not clear on how you're displaying results at the end - do you have a grid or a list control?
You ask about usings. Which ones do you already have?
EDIT for .NET 2.0
There might be a more elegant solution, but you could implement the logic by putting your rows in a DataTable. Then you can make a DataView of that table and sort by whichever column you like, ascending or descending.
See http://msdn.microsoft.com/en-us/library/system.data.dataview(v=VS.80).aspx for example.
EDIT for .NET 3.5 and higher
As far as sorting a Dictionary by its values:
var sortedEntries = myDictionary.OrderBy(pair => pair.Value);
If you need the results to be a Dictionary, you can call .ToDictionary() on that. For reverse order, use .OrderByDescending(pair => pair.Value).

Resources