How to select immediate preceding sibling - xpath

How to select previous tr?
I am trying
//tr[#id='20-covers']/preceding-sibling::tr
But it select 1st tr

Actually your XPath returns the list of preceding tr siblings while the tool you're using might select the first element from this list
You might need to explicitly specify the index of element ([1]) to get immediate preceding tr:
//tr[#id='20-covers']/preceding-sibling::tr[1]

Related

Replacing only 3rd to 6th position of a String

I am new to SQL. I want to replace only 3rd to 6th(only 1st 1234) position of below string with '1219', but its replacing whole new string:
SELECT REPLACE('DD123412341234',SUBSTR('DD123412341234',3,4),'1219' ) FROM DUAL;
Kindle suggest on the same.
You can do it with function regexp_replace.
select regexp_replace('DD123412341234','....','1219',3,1) as RESULT from DUAL
As stated in the documentation, the second function parameter is the regular expression to search for which, in this case, is any four characters. The third parameter is the replacement string. The fourth [optional] parameter indicates to start the search from the third character and the final [also optional] parameter means only search for the first occurrence and replace that first occurrence with 1219.
The result of the above query is DD121912341234
Regular expressions tend to be relatively slow. Since you know the specific location to replace an alternative would be sub the parts to be kept and rebuild with the concatenation operator. The following may be faster, but it does involve more operations.
with test(t) as
(select 'DD123412341234' from dual)
select substr(t,1,2) || '1219' || substr(t,7)
from test;

Find table by column header using xpath

I have the HTML in the screenshot, I can get the table using:
//table[contains('#class','table')]
but there are several similar tables on the page. Now I want to make sure I have the right table by checking that its ths have a specific column header ( In this case 'Sqft)'.
I tried:
//table[contains('#class','table')]//th[contains(text(),'Sqft")
but this is failing. How to I get this working?
//table[contains(#class, 'table') and .//th[contains(., 'Sqft')]]
or the other way around
//th[contains(., 'Sqft')]/ancestor::table[contains(#class, 'table')][1]
On a general note, in order to prevent partial attribute matches, include the token delimiter in the search. For CSS class names, the delimiter is a space:
//table[contains(concat(' ', #class, ' '), ' table '))]

XPath: Limit cell content, if delimiter exists

I need to get the content of a cell, which occasionally contains a ',' character. If so, I need to isolate the content to the portion before the ',' character.
substring-before(//td[contains(text(),'Dokumentnummer')]/following-sibling::td[1],\",\")
This gives me the desired substring, but only if a ',' exists. How can I make it return the whole string, if it does'nt exist?
You can add a ',' before calling substring-before, thus making sure there will allways be at least one comma:
substring-before(concat(//td[contains(text(),'Dokumentnummer')]/following-sibling::td[1],
','),
',')

how does oracle contain query work?

I have this query:
select id from mytable where contains(all_text,'('||?||' within name)*2,
('||?||' within description)',1)>0
How does the contains in the where clause work?
Thanks,
If the first parameter occurs in the name section, then its weight is twice the weight of the second parameter occurring in the description section.
This "contains" operator will set the score variable. Without it doubling the weight has no meaning for ">0" condition. However,
SELECT id FROM mytable WHERE CONTAINS(all_text,'('||?||' WITHIN name)*2,
('||?||' WITHIN description)',1)>0 ORDER BY SCORE(1) DESC
would totally make sense and will order id's first by those rows where search term is found in the name section.
Here is a useful reference, just in case: http://docs.oracle.com/cd/B19306_01/text.102/b14218/cqoper.htm

Swap fields inside an Oracle record using couple separator * and elements separator |

In an Oracle table, I have record with COUPLES (string,number) so separated:
Abc|3456*Def|7890*Ghi|9430*Jkl|3534
In the previous example, the couples are:
(Abc,3456)
(Def,7890)
(Ghi,9430)
(Jkl,3534)
I would like to modify each record swapping the order of every couple (first the number, then the string):
3456|Abc*7890|Def*9430|Ghi*3534|Jkl
The separator of the two elements of a couple is pipe (|).
The SEPARATOR BETWEEN COUPLES is asterisk (*).
How can I achieve my objective to swap the order of every couple?
Thank you in advance for your kind cooperation!
Try using regular expressions...now you've got two problems:
select
cola,
regexp_replace(cola, '([^*|]*)\|([^*|]*)(\*|$)','\2|\1\3') as swapped_col
from (
select '3456|Abc*7890|Def*9430|Ghi*3534|Jkl' cola from dual
)
Basically the regex is saying search for everything that isn't a | or a * until you find |, then find everything that isn't a | or * until you find a * or then end of the string. Then swap the two bits and terminate it with the character you found as the final separator (either * or EOL). The bits that are swapped are grouped by the round brackets then in the replace string the numbers denote which is placed where... so the contents of the second set of brackets is put first, then a vertical bar, then the first set of brackets, then the third.
By default, REGEXP_REPLACE will replace every occurrence that it finds of the pattern and replace it

Resources