欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

GraphQuery - Powerful html/xml query language

程序员文章站 2022-03-13 15:53:05
GraphQuery " " " " " " " " " " " " GraphQuery is a query language and execution engine tied to any backend service. It is . Project Address: "GraphQue ......

graphquery GraphQuery - Powerful html/xml query language GraphQuery - Powerful html/xml query language GraphQuery - Powerful html/xml query language GraphQuery - Powerful html/xml query language GraphQuery - Powerful html/xml query language GraphQuery - Powerful html/xml query language

GraphQuery - Powerful html/xml query language

graphquery is a query language and execution engine tied to any backend service. it is back-end language independent.

project address: graphquery

related projects:

overview

graphquery is an easy to use query language, it has built-in xpath/css/regex/jsonpath selectors and enough built-in text processing functions.
the most amazing thing is that you can use the minimalist graphquery syntax to get any data structure you want.

language-independent

use graphquery to let you unify text parsing logic on any backend language.
you won't need to find implementations of xpath/css/regex/jsonpath selectors between different languages ​​and get familiar with their syntax or explore their compatibility.

multiple selector syntax support

you can use graphquery to parse any text and use your skilled selector. graphquery currently supports the following selectors:

  1. jsonpath for parsing json strings
  2. xpath and css for parsing xml/html
  3. regular expressions for parsing any text.

you can use these selectors in any combination in graphquery. the rich built-in selector provides great flexibility for your parsing.

complete function

graphquery has some built-in text processing functions like trim, template, replace. if you think these functions don't meet your needs, you can register new custom functions in the pipeline.

clear data structure & concise grammar

with graphquery, you won't need to look for parsing libraries when parsing text, nor do you need to write complex nesting and traversal. simple and clear graphquery syntax gives you a clear picture of the data structure.

GraphQuery - Powerful html/xml query language

as you can see from the comparison chart above, the syntax of graphquery is so simple that even if you are in touch with it for the first time, you can still understand its meaning and get started quickly.

at the same time, graphquery is also very easy to integrate into your backend data system (any backend language), let's continue to look down.

getting started

graphquery consists of query language and pipelines. to guide you through each of these components, we've written an example designed to illustrate the various pieces of graphquery. this example is not comprehensive, but it is designed to quickly introduce the core concepts of graphquery. the premise of the example is that we want to use graphquery to query for information about library books.

1. first example

<library>
<!-- great book. -->
<book id="b0836217462" available="true">
    <isbn>0836217462</isbn>
    <title lang="en">being a dog is a full-time job</title>
    <quote>i'd dog paddle the deepest ocean.</quote>
    <author id="cms">
        <?echo "go rocks"?>
        <name>charles m schulz</name>
        <born>1922-11-26</born>
        <dead>2000-02-12</dead>
    </author>
    <character id="pp">
        <name>peppermint patty</name>
        <born>1966-08-22</born>
        <qualification>bold, brash and tomboyish</qualification>
    </character>
    <character id="snoopy">
        <name>snoopy</name>
        <born>1950-10-04</born>
        <qualification>extroverted beagle</qualification>
    </character>
</book>
</library>

faced with such a text structure, we naturally think of extracting the following data structure from the text :

{
    bookid
    title
    isbn
    quote
    language
    author{
        name
        born
        dead
    }
    character [{
        name
        born
        qualification
    }]
}

this is perfect, when you know the data structure you want to extract, you have actually succeeded 80%, the above is the data structure we want, we call it ddl (data definition language) for the time being. let's see how graphquery does it:

{
    bookid `css("book");attr("id")`
    title `css("title")`
    isbn `xpath("//isbn")`
    quote `css("quote")`
    language `css("title");attr("lang")`
    author `css("author")` {
        name `css("name")`
        born `css("born")`
        dead `css("dead")`
    }
    character `xpath("//character")` [{
        name `css("name")`
        born `css("born")`
        qualification `xpath("qualification")`
    }]
}

as you can see, the syntax of graphquery adds some strings wrapped in ` to the ddl. these strings wrapped by ` are called pipeline. we will introduce pipeline later.
let's first take a look at what data graphquery engine returns to us.

{
    "bookid": "b0836217462",
    "title": "being a dog is a full-time job",
    "isbn": "0836217462",
    "quote": "i'd dog paddle the deepest ocean.",
    "language": "en",
    "author": {
        "born": "1922-11-26",
        "dead": "2000-02-12",
        "name": "charles m schulz"
    },
    "character": [
        {
            "born": "1966-08-22",
            "name": "peppermint patty",
            "qualification": "bold, brash and tomboyish"
        },
        {
            "born": "1950-10-04",
            "name": "snoopy",
            "qualification": "extroverted beagle"
        }
    ],
}

wow, it's wonderful. just like what we want.
we call the above example example1, now let's have a brief look at what pipeline is.

2. pipeline

a pipeline is a collection of functions that use the parent element text as an entry parameter to execute the functions in the collection in sequence.
for example, the language field in our previous example is defined as follows:

language `css("title");attr("lang")`

the language is the field name, css("title"); attr("lang") is the pipeline. in this pipeline, graphquery first uses the css selector to find the title node from the document, and the title node will be obtained. pass the obtained node into the attr() function and get its lang attribute. the whole process is as follows:

GraphQuery - Powerful html/xml query language

in example1, we not only use the css and attr functions, but also xpath(). it is easy to associate, xpath() is to select elements with the xpath selector.
the following is a list of the pipeline functions built into the current version of graphquery:

pipeline prototype example introduce
css css(cssselector) css("title") use css selector to select elements
json json(jsonselector) json("title") use json path to select elements
xpath xpath(xpathselector) xpath("//title") use xpath selector to select elements
regex regex(regexselector) regex("") use regex selector to select elements
trim trim() trim() clear spaces and line breaks before and after the string
template template(templatestr) template("[{$}]") add characters before and after variables
attr attr(attributename) attr("lang") extract the property of the current node
eq eq(index) eq("0") take the nth element in the current node collection
string string() string() extract the current node native string
text text() text() extract the text of the current node
link link(keyname) link("title") returns the current text of the specified key
replace replace(a, b) replace("a", "b") replace all a in the current node to b

more detailed introduction to pipeline and function, please go to docs.

install

graphquery is currently only native to golang, but for other languages, it can be invoked as a service.

1. golang:

go get github.com/storyicon/graphquery

create a new go file :

package main

import (
    "encoding/json"
    "log"

    "github.com/storyicon/graphquery"
)

func main() {
    document := `
        <html>
            <body>
                <a href="01.html">page 1</a>
                <a href="02.html">page 2</a>
                <a href="03.html">page 3</a>
            </body>
        </html>
    `
    expr := "{ anchor `css(\"a\")` [ content `text()` ] }"
    response := graphquery.parsefromstring(document, expr)
    bytes, _ := json.marshal(response.data)
    log.println(string(bytes))
}

run the go file, the output is as follows :

{"anchor":["page 1","page 2","page 3"]}

2. other language

we use the http protocol to provide a cross-language solution for developers to query graphquery using any back-end language you want to use to access the specified port after starting the service.

graphquery-http : cross language solution for graphquery

you can also use rpc for communication, but currently you may need to do this yourself, because the rpc project on graphquery is still under development.
at the same time, we welcome the contributors to write native support code for other languages ​​in graphquery.