I have a file with the following structure
<admin>
<sampleName>Willow oak leaf</sampleName>
<sampleDescription comment="Total genes">
<cvParam cvLabel="Bob" accession="123" name="Oak" />
</sampleDescription>
</admin>
I'm trying to get out the text "Total genes" after the sampleDescription comment, and I have used the following code:
sampleDescription = doc.xpath( "/admin/Description/#comment" )
sampleDescription = doc.xpath( "/admin/Description" ).text
But neither work. What am I missing?
might be a typo... have you tried doc.xpath("/admin/sampleDescription/#comment").text?
It's not working because there's no Description element. As mentioned by Iwe, you need to do something like sampleDescription = doc.xpath("/admin/sampleDescription/#comment").to_s
Also, if it were me, I would just do sampleDescription = doc.xpath("//sampleDescription/#comment").to_s. It's a simpler xpath, but it might be slower.
And as a note, something that trips up a lot of people are namespaces. If your xml document uses namespaces, do sampleDescription = doc.xpath("/xmlns:admin/sampleDescription/#comment").to_s. If your doc uses namespaces and you don't specify it with xmlns:, then Nokogiri won't return anything.
Try this:
doc.xpath("//admin/sampleDescription/#comment").to_s
doc.xpath returns a NodeSet which acts a bit like an array. So you need to grab the first element
doc.xpath("//admin/sampleDescription").first['comment']
You could also use at_xpath which is equivalent to xpath(foo).first
doc.at_xpath("//admin/sampleDescription")['comment']
Another thing to note is that attributes on nodes are accessed like hash elements--with [<key>]
Related
I need to get one date in one format like this:
2020-06-03T06:14:00.000+0100.
following this documentation page [1], I tried to do with this expression, but always get an error:
format-dateTime(current-dateTime(), "[Y0001]-[M01]-[D01]-[H01]:[m01]:[s][Z0000]")
I tried to put with this mask too:
format-dateTime(current-dateTime(), "[Y0001]-[M01]-[D01]-[H01]:[m01]:[s][Z0001]")
but the result is 2020-06-03-14:39:50+02:00
I need to delete the ":" on the offset, ¿Which mask may I use?
[1]https://www.rfc-editor.org/rfc/rfc3339#section-5.6
A workaround for your problem could be splitting the output of format-dateTime into two parts and remove the colon on the second expression:
concat(format-dateTime(current-dateTime(), "[Y0001]-[M01]-[D01]-[H01]:[m01]:[s]"),translate(format-dateTime(current-dateTime(), "[Z0001]"),":",""))
Maybe this works for you.
I tried making a nest on a :
$where = new Where();
I also tried making multiple :
$where->like
Could someone please provide me an example of how I can make multiple like ?
I would like to search two different fields with the same value %$value%
Thank you and best regards
Within the Where object you can NEST (wrap in parenthesis) your options and specify an operator (in this case, OR):
$where = new Where();
$where->NEST
->like('field1', '%value%')
->OR
->like('field2', '%value%')
->UNNEST;
This will generate:
... WHERE (`field1` LIKE '%value%' OR `field2` LIKE '%value%')
Found solution by using OR on the final select. The following example shows it :
$where = array();
$where[] = $rel->field1 . " LIKE '%". $value ."%' ";
$select->where($where, 'OR');
I'm not including the foreach but you get the idea, add your queries in where and use OR as predicates will generated for multiple entries.
Extending above solution with more functional way :
$where = new Where();
$where->nest()
->like('field1', '%value%')
->OR
->like('field2', '%value%')
->unnest();
In django's urls.py I got this:
url(r'^main$', 'views.send_partial', name='main'),
url(r'^login$', 'views.send_partial', name='login'),
url(r'^signup$', 'views.send_partial', name='signup'),
url(r'^help$', 'views.send_partial', name='help'),
And I hate repeating code, so I would like to get rid of repeating the same function on and on for every url that should be handled by it. I can not find out how this is done anywhere. So what I am expecting is something like:
url('views.send_partial',
r'^main$', name='main,
r'^login$', name='login',
r'^signup$', name='signup',
r'^help$', name='help')
Ideas?
I have found nothing in the documentation (django.conf.urls), but I think it could be solved with a list/dict of patterns and names.
url_dict = {'main': 'r'^main$', 'login': r'^login$',
'signup': r'^signup$', 'help': r'^help$'}
# This part could also be put into a function taking the
# dictionary and the handler and returning urlpatterns
urls = []
for name, pattern in url_dict.items():
urls.append(url(pattern, 'views.send_partial', name=name))
urlpatterns = patterns('', *urls)
First you create a dictionary mapping the names to patterns (could also be something like a list of lists). Then you loop through the dictionary, creating a list of urlpatterns using url(). Finally you let them trough patterns() or do what else you was doing with them.
How can I get all nodes by specific Document Type?
For example, I want to get in code behind all of nodes with Document Type: s3Article. How can I do this?
New informations:
IEnumerable<Node> nodes = uQuery.GetNodesByType("s3Article").Where(x => x.NiceUrl.Contains("en"));
lvArticles.DataSource = nodes;
lvArticles.DataBind();
This is my code. I had to use Where(x => x.NiceUrl.Contains("en")), because I have 2 language version- without Where I receive nodes from all catalogues with doctype s3Article, but I want to get only from one language version.
Problem is here:
<a href='<%# umbraco.library.NiceUrl(Tools.NumericTools.tryParseInt( Eval("id"))) %>'><%# Eval("title")%></a>
<%# Tools.TextTools.makeIMGHTML("../.."+ Eval("img").ToString(),"180") %>
<%# umbraco.library.StripHtml(Limit(Eval("Article"), 1000))%>
<%# Eval("author")%>
System.Web.HttpException: DataBinding:
'umbraco.presentation.nodeFactory.Node' does not contain a property named 'title'.
The same problem happens with the title, img, article, author. Only ID works nice. How to resolve it?
You can use the uQuery GetNodesByType(string or int) method:
IEnumerable<Node> nodes = uQuery.GetNodesByType("s3Article");
Alternatively, you can use an extension method to get all descendant nodes and then query them by type as in the following answer:
Umbraco 4.6+ - How to get all nodes by doctype in C#?
You could use this to databind to a control within a usercontrol like so:
lvArticles.DataSource = nodes.Select(n => new {
ID: n.Id,
Title: n.GetProperty("title").Value,
Author: n.GetProperty("author").Value,
Article: n.GetProperty("article").Value,
Image: n.GetProperty("img").Value,
});
lvArticles.DataBind();
Only you would need to strip the html, convert the image id to a url, etc. within the select statement as well...
As Shannon Deminick mentions, uQuery is somewhat obsolete. ExamineManager will be the fastest execution time. https://our.umbraco.org/forum/developers/api-questions/45777-uQuery-vs-Examine-vs-IPublishedContent-for-Querying
I also found it to be the easiest and most readable approach to use ExamineManager's search builder. Very flexible, and has the added benefit of being very readable due to the Fluent Builder pattern the U Team used.
This will search ALL nodes, so if you need within a specific branch, you can use .ParentId(1234) etc.
var query = ExamineManager.Instance.CreateSearchCriteria()
.NodeTypeAlias("yourDocumentType")
.Compile();
IEnumerable<IPublishedContent> myNodes = Umbraco.TypedSearch(query);
I prefer typed nodes, but you can also just use "Search()" instead of "TypedSearch()" if you prefer dynamic nodes.
Another example including a specific property value "myPropValue" == "ABC",
var query = ExamineManager.Instance.CreateSearchCriteria()
.NodeTypeAlias("yourDocumentType")
.Or() //Other predicate .And, .Not etc.
.Field("myPropValue", "ABC")
.Compile();
Ref - https://our.umbraco.org/documentation/reference/querying/umbracohelper/
I would like to add an URL fragment #top to a HtmlOutcomeTargetLink, but cant figure out how. For an HtmlOutputLink I just use the following:
HtmlOutputLink link = new HtmlOutputLink();
String urlWithFragment = url + "#top";
link.setValue(urlWithFragment);
How to acomplish this for a HtmlOutcomeTargetLink?
Unfortunately the following does not work:
HtmlOutcomeTargetLink link = new HtmlOutcomeTargetLink();
String urlWithFragment = context.getViewRoot().getViewId() + "#top";
link.setOutcome(urlWithFragment);
Thanks for your help!
The outcome of HtmlOutcomeTargetLink (the <h:link>) only takes navigation case outcomes, not URLs. The navigation case outcomes do not support URL fragments. Those needs to be set as a separate fragment attribute.
link.setFragment("top");
See also:
<h:link> tag documentation
Update: wait, there's no setter for that on the UIOutcomeTarget parent class. I suspect an oversight in the generated code (funnily it's mentioned here in Mojarra snapshot docs and here in a MyFaces testcase). You should be able to set it directly on the attribute map:
link.getAttributes().put("fragment", "top");