In a related post,
How to select specified node within Xpath node sets by index with Selenium?,
it is mentioned that there is "no index i in xpath".
I am trying to use an index in an R loop within an XPath expression such as
getNodeSet(xmlfile, '//first[i]/second/third')
Clearly, according to the above post it works perfectly when replacing 'i' with '1', but not e.g. for i <- 1.
However, the workaround in the above post (i.e. using ['+i+']) does not seem to work.
Any ideas on how to make indices work in XPath expressions?
'//first[i]/second/third' is just a string. Therefore you can use the R string building function paste0() to make your own (R doesn't use + for string concatenation).
getNodeSet(xmlfile, paste0('//first[', i, ']/second/third'))
Related
xquery version "1.0-ml";
declare function local:sortit(){
for $i in ('a','e','f','b','d','c')
order by $i
return
element Result{
element N{1},
element File{$i}
}
};
local:sortit()
the above code is sample, I need the data in this format. This sorting function is used multiple places, and I need only element N data some places and only File element data at other places.
But the moment I use the local:sortit()//File. It removes the sorting order and gives the random output. Please let me know what is the best way to do this or how to handle it.
All these data in File element is calculated and comes from multiple files, after doing all the joins and calculation, it will be formed as XML with many elements in it. So sorting using index and all is not possible here. Only order by clause can be used.
XPath expressions are always returned in document order.
You lose the sorting when you apply an XPath to the sequence returned from that function call.
If you want to select only the File in sorted order, try using the simple mapping operator !, and then plucking the F element from the item as you are mapping each item in the sequence:
local:sortit() ! File
Or, if you like typing, you can use a FLWOR to iterate over the sequence and return the File:
for $result in local:sortit()
return $result/File
In my ML db, we have documents with distributor code like 'DIST:5012' (DIST:XXXX) XXXX is a four-digit number.
currently, in my TDE, the below code works well.
However instead of concat all the raw distributor codes, I want to simply concat the number part only. I used the fn:substring-after XQuery function. However, it won't work. It won't show that distributorCode column in the SQL View anymore. (Below code does not work.)
What is wrong? How to fix that?
Both fn:substring-after and fn:string-join is in TDE Dialect page.
https://docs.marklogic.com/9.0/guide/app-dev/TDE#id_99178
substring-after() expects a single string as input, not a sequence of strings.
To demonstrate, this will not work:
let $dist := ("DIST:5012", "DIST:5013")
return substring-after($dist, "DIST:")
This will:
for $dist in ("DIST:5012", "DIST:5013")
return substring-after($dist, "DIST:")
I need to double check what XPath expressions will work in a DTE, you might be able to change it to apply the substring-after() function in the last step:
fn:string-join( distributors/distributor/urn/substring-after(., 'DIST:'), ';')
I'm parsing xml with R's getNodeSet function by attribute value with the following code:
getNodeSet(doc, "/body//*[#attribution='HM'][#*='checkmark'][#*='underline']")
The code above returns node content that includes all three of the above values (effectively, 'HM' And 'checkmark' And 'underline').
I'd like the function to return nodes in which the first value remains constant, but for which additional values are EITHER/OR (effectively, 'HM' AND 'checkmark' OR 'underline').
Grateful for any help.
The solution is to combine the type attribute values to be OR'd within a single set of square brackets, and supply 'or' without quotes:
getNodeSet(doc, "/body//*[#attribution='HM'][#*='underline' or #*='checkmark']")
So I have this list of names:
names <- c("stewart,pat", "peterson,greg")
from which I extract only the lastname,firstname items with the following regular expression:
myregexpr <- "(\\w+),(\\w+)?"
str_view(str_extract_all(names, myregexpr), myregexpr)
This yields a view like:
stewart,pat
peterson,greg
My question: Is there a way for me to write the regular expression such that the result would instead look like:
pat_stewart
greg_peterson
i.e. where the result of is first_last? I believe there is a way to do it as I've seen on other, similar questions. I've tried:
myregexpr <- "(\\w+),(\\w+)?\\2_\\1"
but that returns only `character(0)'. I've attempted many versions - some of which crash R studio. Any ideas?
I am trying to use the hash package in R to replicate dictionary behavior in python. I have created it like this,
library(hash)
titles = hash(NAME = list("exact"=list('NAME','Age'), "partial"=list()),
Dt = list("exact"=list('Dt'), "partial"=list()),
CC = list("exact"=list(), "partial"=list()))
I can access the keys in the hash using keys(titles) , values using values(titles), and access values for a particular key using values(titles['Name']).
But how can I access the elements of the inner list? e.g. list('NAME','Age') ?
I need to access the elements based on its names, in this case - "exact" or else I need to know which element of the outer list this element belong to, whether its "exact" or "partial".
Simply:
titles[["NAME"]][["exact"]]
as hrbmstr wrote. There's nothing special about this whatsoever.
In your nested-list, "exact" and "partial" are simply two string keys. Again, there's no special magic significance to their names.
Also, this is in fact the recommended proper R syntax (esp. when the key is variable), it's not "bringing gosh-awful Python syntax".