XSLT following Sibling issue - xslt-1.0

<root>
<A>
...
</A>
<A1>Text</A1>
<A1>Text</A1>
<B1>Text</B1>
<C1>Text</C1>
<A>
..
</A>
<A1>Text</A1>
In the above example I want to select all the siblings of first A which are A1.If I put "following-sibling::*[self::A]" then it will select only the first A1. But I want to select all A1 which are followed by A. There is no rule like B1 tag will come after A1. Any tag can come after A1.
Please help to sort this issue.

If I put "following-sibling::*[self::A]" then it will select only the
first A1.
I think you meant:
following-sibling::*[self::A1]
which can be written simply as:
following-sibling::A1
This will select all A1 nodes that are following siblings of the context node. You may think it only selects the first one because of how you use the select statement. For example, in XSLT 1.0 the instruction:
<xsl:value-of select="following-sibling::A1"/>
will return the value of the first A1 node in the selected set.

Related

Extract characters in string following keyword and ending right before the other keyword

I have a table that looks like:
id
re|cid|13324242|
wa|cid|13435464|
fs|cid|2343532|
I want to extract information that is contained right after "|cid|" and before the following "|" element. That is:
13324242
13435464
2343532
I thought of substr() but there I don't know how to specify start and end element.
You could use REGEXP_REPLACE here (Standard SQL):
SELECT
id,
CASE WHEN id LIKE '%|cid|%'
THEN REGEXP_REPLACE(id, '^.*\|cid\|(\d+)\|.*$', '\1') END AS cid
FROM yourTable;
The idea is to use a regex replacement to extract the cid value from the id column, should it be present (and if not, we would just return NULL).
Here is a demo showing that the regex logic be correct.
If you want the third element (which appears to be the intention given the sample data), I would recommend split():
select (split(id, '|')[ordinal(3)]

Fetch sub-string from a column containing XML formatted String in oracle sql

As part of my query I am having one column which is having field value in two patterns as below
First Pattern:-
"First Name:<a class='text-lg text-info'> David Peter </a><br>
Deadline:<a class='text-lg text-info'>2019-12-07 20:05:01</a><br>
Remarks:<a class='text-lg text-info'>Some remarks with multiple spaces</a><br>"
Second Pattern:-
FirstName: <a class='text-lg text-info'>Alex Vander Veen</a><br>DeadLine: <a class='text-lg text-info'>2019-11-16 16:30:35</a>
I am trying to fetch out First Name/FirstName and Deadline value from these two fields.
Output needs to be as below :-
For getting this details case statement needs to be used on two patterns mentioned but I am really clueless on how to fetch only First Name/FirstName and Deadline from XML formatted string.
I am able to find solution to my above problem.
SELECT SUBSTR(REGEXP_SUBSTR(A.FIELD_CONTENT,'>[^>]+<'),2,LENGTH(REGEXP_SUBSTR(A.FIELD_CONTENT,'>[^>]+<')) - 2 ) AS First_Name,
SUBSTR(SUBSTR(A.FIELD_CONTENT, REGEXP_INSTR(a.FIELD_CONTENT, '>', 1,4)+1),1, (REGEXP_INSTR(SUBSTR(A.FIELD_CONTENT, REGEXP_INSTR(a.FIELD_CONTENT, '>', 1,4)+1),'<',1,1) -1)) AS DEADLINE
FROM table_name1 a
WHERE a.FIELD_CONTENT like '%First%'
I came up with this. If anyone has any suggestion to make this less expensive. Suggestions are welcomed.. :-)

How to not replace the word with in a pattern using regexp_replace?

In the text below, the word number appears twice. I want to not replace the word which appears between a pattern <a hef and a>. Is there a way to avoid the word between this pattern using just the regexp_replace?
The code doesn't work as expected.
with t as (
select 'The Number can be a whole number. <a href https://number.com a>' as text from dual)
select regexp_replace(text,'^[<a href].*number.*[a>]','num') from t
The expected outcome is
The num can be a whole num. <a href https://number.com a>
I don't know of a way to do it in a single call, but you can do it with multiple calls.
First call: convert the "number" occurrences in the href to a different string
Second call: convert the remaining "number" occurrences
Third call: convert the "different string" occurrences back to "number".
E.g.,
with t as (
select 'The Number can be a whole number. <a href https://number.com a>' as text from dual)
select regexp_replace(
regexp_replace(
regexp_replace(text,'(<a href.*)(number)(.*a>)','\1$$$SAVE_NBR$$$\3'),
'number', 'num'),
'\$\$\$SAVE_NBR\$\$\$','number')
from t
I don't know why I used "$" in the "different string"... it just makes it harder. The point is to choose a string that can never occur naturally in your input.
this will work:
with t as (
select 'The Number can be a whole number. <a href https://number.com a>' as text from dual)
select regexp_replace(regexp_replace(text,' Number',' num'),' number',' num') from t

SELECT middle part of a String if it exists. Postgresql

i've got a problem with transferring "real-World" data into my schema.
It's actually a "project" for my Database course and they gave us ab table with dog race results. This Table has a column which contains the name of the Dog (which itself consists of the actuall name and the name of the breeder) and informations about the Birthcountry, actual living Country and the birth year.
Example filed are "Lillycette [AU 2012]" or "Black Bear Lee [AU/AU 2013]" or "Lemon Ralph [IE/UK 1998]".
I've managed it to get out the first word and save it in the right column with split_part like this:
INSERT INTO tblHund (rufname)
SELECT
split_part(name, ' ', 1) AS rufname,
FROM tblimport;
tblimport is a table where I dumped the data from the csv file.
That works just as it should.
Accessing the second part of the Name with this fails because sometimes there isn't a second part and sometimes times there second part consists of two words.
And this is the where iam stuck right now.
I tried it with substring and regular expressions:
INSERT INTO tblZwinger (Name)
SELECT
substring(vatertier from E'[^ ]*\\ ( +)$')AS Name
FROM tblimport
WHERE substring(vatertier from E'[^ ]*\\ ( +)$') != '';
The above code is executed without errors but actually does nothing because the SELECT statement just give empty strings back.
It took me more then 3h to understand a bit of this regular Expressions but I still feel pretty stupid when I look at them.
Is there any other way of doing this. If so just give me a hint.
If not what is wrong with my expression above?
Thanks for your help.
You need to use atom ., which matches any single character inside capturing group:
E'[^ ]*\\ (.+)$'
SELECT
tblimport.*,
ti.parts[1] as f1,
ti.parts[2] as f2, -- It should be the "middle part"
ti.parts[3] as f3
FROM
tblimport,
regexp_matches(tblimport.vatertier, '([^\s]+)\s*(.*)\s+\[(.*)\]') as ti(parts)
WHERE
nullif(ti.parts[2], '') is not null
Something like above.

SQL Get Text Between \

I'm having an issue getting the correct text between characters. I'm currently trying to use SUBSTRING and CHARINDEX. The issue is that the data contains the same identifier. I would like the text that falls between the first '\' and the second '\', removing all the other text. None of the text fields have a fixed number of characters.
Thank you for your help its greatly appreciated.
Example
1. Location\Georgia\Atlanta
2. Country\USA\States\Minnesota
Final Result
1. Georgia
2. USA
My Current Attempt
SUBSTRING(Source,CHARINDEX('\',Source),CHARINDEX('\',Source) - CHARINDEX('\',Source))
I agree with Joel's comment. This does look like a violation of first normal form but to find the second instance of \ you can pass the location of the first one in as the third argument to CHARINDEX
WITH T(Source) AS
(
SELECT 'Location\Georgia\Atlanta' UNION ALL
SELECT 'Country\USA\States\Minnesota'
)
SELECT *,
SUBSTRING(Source, F,CHARINDEX('\',Source, F) - F)
FROM T
CROSS APPLY (SELECT 1 + CHARINDEX('\',Source)) CA(F)
WHERE Source LIKE '%\%\%'
Martin's answer is slick, this method works, but mostly just illustrates how slick his is:
WITH T(Source) AS
(
SELECT 'Location\Georgia\Atlanta' UNION
SELECT 'Country\USA\States\Minnesota'
)
SELECT *, SUBSTRING(Source,CHARINDEX('\',Source)+1,CHARINDEX('\',Source,CHARINDEX('\',Source)+1)-CHARINDEX('\',Source)-1)
FROM T