Sorry even trying to watch tutorials I am just trying to understand the difference between the data() and the text() functions in XQuery.
Any clarification is appreciated.
text() is used to match something. For example if we have this structure:
<a>
<b>hello <c>world</c></b>
</a>
Doing //b/text() will return the text node 'hello ' just like //b/element() will return the element c.
data($arg) is a function that returns the atomic value of a node, for example data(//b) will return 'hello world'. If you use the data($arg) function on a document with a schema then the type will be kept intact.
Related
So I have to go through a bunch of code to get some data from an iframe. the iframe has a lot of data but in there is an object called '_name'. the first key of name is 'extension_id' and its value is a big long string. the json object is enclosed in apostrophes. I have tried removing the apostrophes but still instead of 'extension_id_output' I get a single curly bracket. the json object looks something like this
Frame {
...
...
_name: '{"extension_id":"a big huge string that I need"} "a bunch of other stuff":"this is a valid json object as confirmed by jsonlint", "globalOptions":{"crev":"1.2.50"}}}'
}
it's a whole big ugly paragraph but I really just need the extension_id. so this is the code I'm currently using after attempt 100 or whatever.
var frames = await page.frames();
// I'm using puppeteer for this part but I don't think that's relevant overall.
var thing = frames[1]._name;
console.log(frames[1])
// console.log(thing)
thing.replace(/'/g, '"')
// this is to remove the apostrophes from the outside of the object. I thought that would change things before. it does not. still outputs a single {
JSON.parse(thing)
console.log(thing[0])
instead of getting a big huge string that I need or whatever is written in extension_id. I get a {. that's it. I think that is because the whole object starts with a curly bracket. this is confirmed to me because console.log(thing[2]) prints e. so what's going on? jsonlint says this is a valid json object but maybe it's just a big string and I should be doing some kind of split to grab whaat's between the first : and the first ,. I'm really not sure.
For two reasons:
object[0] doesn't return the value an object's "first property", it returns the value of the property with the name "0", if any (there probably isn't in your object); and
Because it's JSON, and when you're dealing with JSON in JavaScript code, you are by definition dealing with a string. (More here.) If you want to deal with the object that the JSON describes, parse it.
Here's an example of parsing it and getting the value of the extension_id property from it:
const parsed = JSON.parse(frames[1]._name);
console.log(parsed.extension_id); // The ID
I wrote a helper function to help me format URL, which is the combination of some object attributes. How do I concatenate this attribute in handlebars view?
Helper function
const url = (link)=>{
return process.env.URL+'/'+link.replace(/ /gi,'-').toLowerCase();
};
My view
{{this.name}}
What you can do to accomplish this is to create a helper to concatenate your strings and pass the concatenated string to the url helper.
In JavaScript, every non-arrow function has a local variable named arguments assigned an object, in this object you'll find (you guessed it) the arguments passed to the function when it was invoked.
Using this arguments object we can create a helper for Handlebars.js that lets us concatenate as many strings as we want.
Because (as described by the documentation) the arguments object is an array-like object, we have to create an actual array from it that we can use to concatenate everything together using Array.join to make it as simple as possible.
Handlebars.registerHelper('concat', function() {
return [...arguments].join('');
});
But why would it be that simple, right?
What I discovered when I was trying out this solution is that the last element in the arguments object is an object with some information about the helper that was used, like the name.
To make this work with that little bit of information in mind we have to slice the array so that the last element gets removed and then concatenate it.
Handlebars.registerHelper('concat', function() {
arguments = [...arguments].slice(0, -1);
return arguments.join('');
});
We now have a helper that we can use to concatenate whatever we want, the only thing left to do is pass it to the other helper, the url helper in this case.
{{url (concat 'samples/' this.name '/' this.class '/' this.id)}}
^ I found a comment on an GitHub issue regarding chaining helpers and that Handlebars.js apparently has native support for this.
I hope this helped and that I didn't spoon-feed to much without explaining it properly.
When I call:
db.any('SELECT (col1, col2, col3) FROM myTable....[other conditions]')
where db is an instance of pg-promise connection, as a promise result for
.then(function(data)) {
I get an array with objects like { row: '(ans1,ans2,ans3)' }
It is also written in pg-promise documentation
Is there any convenient way (i.e. without string triming and coma splitting) to parse it to JS object? I would like to use it in views (.pug files), for example element.col1 which will print ans1.
By wrapping column names into (), you are specifically requesting exactly what you are getting back.
Without () you will get an array of JSON objects.
I'm working with expressJS and handlebar as an engine template in my index.hbs I have a JS script in which I need to get a value of an array of object, here is the code of my script
<script >
new Morris.Line({
element: 'myfirstchart',
parseTime:false,
data: {{graph}},
xkey: 'version',
ykeys: ['success'],
labels: ['Success']
});
</script>
but the array graph does not pass, in my log console it is shown like
this
What should I do to get the value of my {{graph}} ?
Please add a sample of your data in {{graph}} and also what you expect instead of [object Object]. You'll need to format your data with the handlebar template {{graph}} contains one array of 5 elements containing objects.
If your data is let say {'x': '100', 'y':200} and that you want the same output with handlebar then you should put this instead of {{graph}}:
[{{#each graph}}{'x': {{x}}, 'y': {{y}} }{{/each}}]
If you put your data and the expected output format I'll may give a more accurate answer.
You need to stringify your array object i.e. JSON.stringify(graph) from server side and while accessing it use triple brackets {{{graph}}} in your javascript script tag code.
I'm fairly new to groovy, looking at some existing code, and I see this:
def timestamp = event.timestamp[]
I don't understand what the empty square brackets are doing on this line. Note that the timestamp being def'd here should receive a long value.
In this code, event is defined somewhere else in our huge code base, so I'm not sure what it is. I thought it was a map, but when I wrote some separate test code using this notation on a map, the square brackets result in an empty value being assigned to timestamp. In the code above, however, the brackets are necessary to get correct (non-null) values.
Some quick Googling didn't help much (hard to search on "[]").
EDIT: Turns out event and event.timestamp are both zero.core.groovysupport.GCAccessor objects, and as the answer below says, the [] must be calling getAt() on these objects and returning a value (in this case, a long).
The square brackets will invoke the underlying getAt(Object) method of that object, so that line is probably invoking that one.
I made a small script:
class A {
def getAt(p) {
println "getAt: $p"
p
}
}
def a = new A()
b = a[]
println b.getClass()
And it returned the value passed as a parameter. In this case, an ArrayList. Maybe that timestamp object has some metaprogramming on it. What does def timestamp contains after running the code?
Also check your groovy version.
Empty list, found this. Somewhat related/possibly helpful question here.
Not at a computer, but that looks like it's calling the method event.timestamp and passing an empty list as a parameter.
The same as:
def timestamp = event.timestamp( [] )