Database |> Language |> 

Attributes

When selecting stuff you may not be just interested in the text contained in an element but the value of an attribute provided to said element. In CSS you will find the @ symbol used for defining rules but never selectors hence it’s used in LSD as a mnemonic for “attribute”.

Contents

  1. Getting the attribute value of an element
  2. Getting the attribute value of a variable

Getting the attribute value of an element

To get the attribute value of an element, simply append to its identifier the @ symbol followed by the name of the attribute you’re interested in.

SELECT
  <css_selector>@<attribute>
FROM
  <url>;

For example, when getting links from the front page of Hacker News, you’d be interested in both what the post submitters wrote as titles as well as the URLs to the thing they’re sharing.

SELECT
  a AS post
, a@href AS post_link
FROM
  https://news.ycombinator.com
GROUP BY
  span.titleline;

Or, written with variables and pipe operator syntax to isolate the attribute assignment more:

hn <| https://news.ycombinator.com |
container <| span.titleline |
post <| a |
post_link <| a@href | -- The [href] attribute

FROM hn
|> GROUP BY container
|> SELECT post, post_link;

Getting the attribute value of a variable

The @ symbol can be flexibly used with variables as well as CSS selectors. Rewriting the example shown above to obtain the href attribute from post:

hn <| https://news.ycombinator.com |
container <| span.titleline |
post <| a |

FROM hn
|> GROUP BY container
|> SELECT post, post@href AS post_link;