1

I have this HTML source:

<div id="naver">
    <ul id="naverlist">
        <li class="overbrand" class="navhome"><a href="http://www.laptop-power-adapters.co.nz">HOME</a></li>
        <li><a href="http://www.laptop-power-adapters.co.nz/acer-laptop-power-adapters.htm">Acer</a></li>
        <li><a href="http://www.laptop-power-adapters.co.nz/asus-laptop-power-adapters.htm">ASUS</a></li>
        <li class="navspecial"><a href="http://www.laptop-power-adapters.co.nz/contact.htm" target="_blank">Contact Us</a></li>
    </ul>
</div>

I want to create function for select inner text of <li> elements with <a> and return URL and brand laptop.

Example:

brandname   url
acer        laptop-power-adapters.co.nz/acer-laptop-power-adapters.htm 
1
  • Welcome to Stack Overflow! Please go through the tour, the help center and the how to ask a good question sections to see how this site works and to help you improve your current and future questions, which can help you get better answers. Commented Sep 4, 2016 at 10:01

1 Answer 1

1

This could be done if you send this HTML (as XML) into function and this function will return table:

CREATE FUNCTION dbo.htmltotable (   
    @html xml
)
RETURNS TABLE 
AS
RETURN 
(
    SELECT  t.v.value('.','nvarchar(max)') as Brand,
            t.v.value('@href','nvarchar(max)') as [URL]
    FROM @html.nodes('/div/ul/li/a') as t(v)
)
GO

But here comes a problem:

You have class="overbrand" class="navhome" in first <li>. It will throw error duplicate attribute on a moment when you convert your text to XML. So you need to do something with your HTML first.

F.e. if this:

<li class="overbrand" class="navhome"><a href="http://www.laptop-power-adapters.co.nz">HOME</a></li>

is a standard for all pages you can use REPLACE:

DECLARE @x nvarchar(max),
        @replacement nvarchar(max) = ' class="overbrand" class="navhome"'

SELECT @x = N'
<div id="naver">
    <ul id="naverlist">
        <li class="overbrand" class="navhome"><a href="http://www.laptop-power-adapters.co.nz">HOME</a></li>
        <li><a href="http://www.laptop-power-adapters.co.nz/acer-laptop-power-adapters.htm">Acer</a></li>
        <li><a href="http://www.laptop-power-adapters.co.nz/asus-laptop-power-adapters.htm">ASUS</a></li>
        <li class="navspecial"><a href="http://www.laptop-power-adapters.co.nz/contact.htm" target="_blank">Contact Us</a></li>
    </ul>
</div>'

SELECT @x = REPLACE(@x,@replacement,'')

SELECT *
FROM dbo.htmltotable(@x)

Output:

Brand       URL
HOME        http://www.laptop-power-adapters.co.nz
Acer        http://www.laptop-power-adapters.co.nz/acer-laptop-power-adapters.htm
ASUS        http://www.laptop-power-adapters.co.nz/asus-laptop-power-adapters.htm
Contact Us  http://www.laptop-power-adapters.co.nz/contact.htm
Sign up to request clarification or add additional context in comments.

38 Comments

very good.thanks for answer,please help me .1.maybe get source code with sql?.2.parse url of acer and return url and model of acer. please visit site. 3-Home and contact us dont get . thanks and very thanks
1 and 2 It is completely another question and it is much better done with another tools like c# or php. 3. To get rid of them you can add WHERE [URL] NOT IN ('HOME','Contact Us'). Or even in function to filter them.
why t ? why v? why '.'? why ' @href'?thanks for answer
In 'nodes' we pass the full pat to nodes we need. You can use any other letters instead of t(v)... g(s) or h(j) or whatever, it is an alias so we can refer to that part ast t.v. Value brings the value of node or attribute we need. Dot brings innertext, '@somename' brings the value of attribute. F.e. for brand name is used dot, because it is an inner text of a tag and for link url is used '@href' attribute, because the value is inside a tag in href attribute.
Thanks for Answer. So for the contents of the text from dot and for the address of the page@href we use. is The difference between S and v?(T(v) T(s)) وWhen I wanted to change V TO S, error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.