Can someone help me in sorting a channel listing by Related entry title please?
I have a channel 'teams' which has a field team_region which depends on another channel 'regions'.
My code is as follows:
{exp:channel:entries channel="teams" orderby="" sort="asc" limit="10"}
<tr>
<td>{title}</td>
<td>{team_address}</td>
<td>{related_entries id="team_region"}{title}{/related_entries}</td>
</tr>
{/exp:channel:entries}
I want to Sort the result by 'team_region >> title'. Is it possible to achieve this?
Thank You!
Why not take the opposite approach - start with the Regions channel, then use reverse related entries to list your teams. Add a query tag to grab the region's title from within the {reverse_related_entries} loop, and you should be good to go:
{exp:channel:entries channel="regions" orderby="title" sort="asc"}
{reverse_related_entries orderby="title" sort="asc" channel="teams"}
<tr>
<td>{title}</td>
<td>{team_address}</td>
<td>{exp:query sql="SELECT title as region_title FROM exp_channel_titles t LEFT JOIN exp_relationships r ON r.rel_child_id = t.entry_id WHERE r.rel_id = {team_region} LIMIT 1"}{region_title}{/exp:query}</td>
</tr>
{/reverse_related_entries}
{/exp:channel:entries}
You might want to look at Playa from Pixel and Tonic. It's a powerful multi-relational tool with tons more options than the built-in relationships field.
You'll probably want to use something like
{exp:playa:parents channel="channelname" orderby="title"}
Link to docs →
Related
I am trying to fetch the numeric value after strong tag, as its not an web element, I am not able to get the value 123456789 in to variable:
If I use Get Text xpath=//*[#id='referral-or-navinet-reference-number'] then the result is "Referral #: 123456789"
Please help me in getting only numeric value in to variable.
HTML Code:
<td class="normal-text" id="referral-or-navinet-reference-number" align="right">
<strong>Referral #:</strong> 123456789
</td>
You can directly use split method of python
Like :-
x.split(":") // x is a string variable of your gettext
http://www.tutorialspoint.com/python/string_split.htm
http://www.pythonforbeginners.com/dictionary/python-split
Hope it will help you :)
If your td only contains the wanted text as content text you may use the following xpath:
//*[#id='referral-or-navinet-reference-number']/text()
This should return 123456789 (perhaps with some whitespace)
You can use given xpath :
//td[#id="referral-or-navinet-reference-number"]/text()[normalize-space()]
Hello I want to ask a question
I scrape a website with xpath ,and the result is like this:
[u'<tr>\r\n
<td>address1</td>\r\n
<td>phone1</td>\r\n
<td>map1</td>\r\n
</tr>',
u'<tr>\r\n
<td>address1</td>\r\n
<td>telephone1</td>\r\n
<td>map1</td>\r\n
</tr>'...
u'<tr>\r\n
<td>address100</td>\r\n
<td>telephone100</td>\r\n
<td>map100</td>\r\n
</tr>']
now I need to use xpath to analyze this results again.
I want to save the first to address,the second to telephone,and the last one to map
But I can't get it.
Please guide me.Thank you!
Here is code,it's wrong. it will catch another thing.
store = sel.xpath("")
for s in store:
address = s.xpath("//tr/td[1]/text()").extract()
tel = s.xpath("//tr/td[2]/text()").extract()
map = s.xpath("//tr/td[3]/text()").extract()
As you can see in scrappy documentation to work with relative XPaths you have to use .// notation to extract the elements relative to the previous XPath, if not you're getting again all elements from the whole document. You can see this sample in the scrappy documentation that I referenced above:
For example, suppose you want to extract all <p> elements inside <div> elements. First, you would get all <div> elements:
divs = response.xpath('//div')
At first, you may be tempted to use the following approach, which is wrong, as it actually extracts all <p> elements from the document, not only those inside <div> elements:
for p in divs.xpath('//p'): # this is wrong - gets all <p> from the whole document
This is the proper way to do it (note the dot prefixing the .//p XPath):
for p in divs.xpath('.//p'): # extracts all <p> inside
So I think in your case you code must be something like:
for s in store:
address = s.xpath(".//tr/td[1]/text()").extract()
tel = s.xpath(".//tr/td[2]/text()").extract()
map = s.xpath(".//tr/td[3]/text()").extract()
Hope this helps,
I have a table containing tds like the one below.
Im trying to get a hold of the href-part only.
Now i got something like this:
var aTags = htmlDocument.DocumentNode.SelectNodes("//td//a[#href]");
It seems to be returning all the info in the td. How can I specify that I only want that href? There are many similar questions here but I cant seem to get it to work.
<tbody>
<tr>
<td colspan="1" rowspan="1">
<a shape="rect" id="ctl00_mainCPH_ResultListUC_ResultList_ctl04_hlRubrik" href="/sitevision/proxy/4.38a41afd11d99fbdb65800016.html/svid12_38a41afd11d99fbdb65800021/-123388378/Standard/Platsannonser/VisaFritextAnnonser.aspx?ids=2499859&q=s%28sn%28systemutvecklare%29sida%281%29ar%2820%29%29" style="display:inline-block;width:160px;">Systemutvecklare</a>
</td>
</tr>
</tbody>
. Every objects has for example an outerHtml-property looking like the a tag above,
what I need is yo get the hrefs and collect sthem in a list of strings..
The image below shows that the value i want actually exists in the objects im getting, i want the value of the hrefs...
EDIT:
I seem to be able to get the innerhtml like this:
var bTags = htmlDocument.DocumentNode.SelectNodes("//td//a/#href").Select(o => o.InnerHtml).ToList();
But I still dont know how to get the hrefs...
Your XPath will get you all a elements that have an attribute named href. To get the attribute itself, you need to use //td//a/#href.
This code seems to do what i wanted:
var bTags = htmlDocument.DocumentNode.SelectNodes("//td//a/#href").Select(o => o.Attributes["href"].Value).ToList();
Please take a look at the snippet of html below:
<tr class="clickable">
<td id="7b8ee8f9-b66f-4fba-83c1-4cf2827130b5" class="clickable">
<a class="editLink" href="#">Single</a>
</td>
<td class="clickable">£14.00</td>
</tr>
I'm trying to assert the value of td[2] when td[1] contains "Single". I've tried assorted variants of:
//td[2][(contains(text(),'£14.00'))]/../td[1][(contains(text(),'Single'))]
I've used similar notation elsewhere successfully - but to no avail here... I think it's down to td[1] having the nested element, but not sure.
Can someone enlighten as to what I'm getting wrong? :)
Cheers!
What about:
//tr[contains(td[1], "Single")]/td[2]
First select the <tr> containing the <td> matching the text, and then select td[2].
Then,
contains(//tr[contains(td[1], "Single")]/td[2], "£14.00")
should return True.
Or, closer to the expression you tried, you could test if this matches:
//tr[contains(td[1], "Single")]/td[2][contains(., "£14.00")]
See #JensErat's answer to find xth td with td contains in same tr xpath python .
Why not make it simple on yourself, do the if statement in your code. Psuedocode:
Select the top level tr.
Find first td within tr, check to see if it contains Single.
If it does, assert that it contains £14.00
Alternatively, you could just get the text of the top level tr and perform the checks on that text.
I want to get an td element without attributes.
For example:
My code:
<td class="yyy">1234</td>
<td>5678</td>
I want to get: 5678
What the XPath for that?
Thank,
Chani
I think this is a duplicate of several other SO questions:
See this:
XPath: How to select nodes which have no attributes?
Which recommends:
//node[not(#*)]
Where node is your nodename.
try the following
/td[not(#class)]
how about
.//td[. = '5678']
or
.//td[text() = '5678']
--
if it is important that there are no attributes then,
.//td[text() = '5678' and not(#*)]
--
or, if you want to get the inner text of the first td with no attributes.
.//td[not(#*)][1]/text()