當前位置: 首頁>>代碼示例>>Golang>>正文


Golang xpath.Compile函數代碼示例

本文整理匯總了Golang中github.com/moovweb/gokogiri/xpath.Compile函數的典型用法代碼示例。如果您正苦於以下問題:Golang Compile函數的具體用法?Golang Compile怎麽用?Golang Compile使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Compile函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: titleSeparation

func titleSeparation(newDatas []xml.Node) ([]string, error) {
	var ret []string

	a := xpath.Compile("./a/span")
	for _, newData := range newDatas {
		titles, err := newData.Search(a)
		if err != nil {
			return nil, err
		}
		for _, title := range titles {
			newsAndTime := title.Content()
			timePath := xpath.Compile("./span")
			time, err := title.Search(timePath)
			if err != nil {
				return nil, err
			}
			if len(time) != 0 {
				cutstr := time[0].Content()
				ret = append(ret, strings.Trim(newsAndTime, cutstr))
			}
		}
	}

	return ret, nil
}
開發者ID:ieee0824,項目名稱:news-getter,代碼行數:25,代碼來源:yomiuri.go

示例2: main

func main() {

	data, err := ioutil.ReadFile("xpath.xml")
	if err != nil {
		log.Fatal(err)
	}

	doc, err := xml.Parse(data, nil, nil, 0, xml.DefaultEncodingBytes)
	if err != nil {
		log.Fatal(err)
	}
	defer doc.Free()

	xp := doc.DocXPathCtx()
	xp.RegisterNamespace("folia", "http://ilk.uvt.nl/folia")

	fmt.Println("\nAll sentences with all words:\n")

	xps := xpath.Compile("//folia:s")
	xpw := xpath.Compile("folia:w/folia:t")

	ss, err := doc.Root().Search(xps)
	if err != nil {
		log.Fatal(err)
	}
	for _, s := range ss {
		fmt.Println(s.Attr("id"))
		ww, err := s.Search(xpw)
		if err != nil {
			log.Fatal(err)
		}
		for _, w := range ww {
			fmt.Println("\t" + w.Parent().Attr("id") + "  \t" + w.Content())
		}
	}

	fmt.Println("\nSearch for specific sentence:\n")
	n, err := doc.Root().Search(`//folia:s[@xml:id="WR-P-E-E-0000000020.head.4.s.2"]`)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(n)

	fmt.Println("\nSearch for sentence with specific word:\n")
	n, err = doc.Root().Search(`//folia:w[@xml:id="WR-P-E-E-0000000020.head.4.s.2.w.2"]`)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(n[0].Parent())

}
開發者ID:rdterner,項目名稱:HowToGo,代碼行數:51,代碼來源:xpath.go

示例3: main

func main() {
	xpathString := ""
	if len(os.Args) < 2 {
		fmt.Fprintln(os.Stderr, "Missing second argument, XPATH!")
		os.Exit(2)
	} else {
		xpathString = os.Args[1]
	}

	page, _ := ioutil.ReadAll(os.Stdin)
	doc, err := gokogiri.ParseHtml(page)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Problem parsing document.")
	}
	defer doc.Free()

	xps := xpath.Compile(xpathString)
	defer xps.Free()

	search, err := doc.Search(xps)
	if err == nil {
		for _, s := range search {
			fmt.Println(s.Content())
		}
	} else {
		fmt.Fprintln(os.Stderr, "Sorry. Got error.")
	}
}
開發者ID:otobrglez,項目名稱:xq,代碼行數:28,代碼來源:main.go

示例4: GetBlock

// GetBlock GETs and parses practitioner's appointments on date
// along with any information needed to book available appointments.
func (s *Session) GetBlock(date time.Time, practitioner Practitioner) (Block, error) {

	root, err := s.loadBlock(date)
	if err != nil {
		return nil, err
	}

	var result Block

	for status, path := range paths {

		divs, err := root.Search(xpath.Compile(fmt.Sprintf(path, practitioner)))
		if err != nil {
			return nil, err
		}

		for _, div := range divs {

			timestamp, blockIndex, err := parseAppDiv(div)
			if err != nil {
				return nil, err
			}

			result = append(result, Appointment{
				session:      s,
				Timestamp:    time.Unix(timestamp, 0),
				Practitioner: practitioner,
				Status:       status,
				blockIndex:   blockIndex,
			})
		}
	}
	return result, nil
}
開發者ID:allonhadaya,項目名稱:boa,代碼行數:36,代碼來源:block.go

示例5: processOffJoins

func (record *Record) processOffJoins(doc *html.HtmlDocument) {
	xPath := xpath.NewXPath(doc.DocPtr())
	// join the offence with the defendants and verdict
	joinPtrs := xPath.Evaluate(doc.Root().NodePtr(),
		xpath.Compile("//join[@result='criminalCharge']"))

	for _, nodePtr := range joinPtrs {
		node := xml.NewNode(nodePtr, doc)
		targets := strings.Split(node.Attr("targets"), " ")
		var personId, offId, verdictId string
		for _, targetId := range targets {
			if strings.Contains(targetId, "defend") {
				personId = targetId
			}
			if strings.Contains(targetId, "off") {
				offId = targetId
			}
			if strings.Contains(targetId, "verdict") {
				verdictId = targetId
			}
		}
		offence := record.findOffence(offId)
		if offence == nil {
			panic("couldn't find offence " + offId)
		}
		person := record.findPerson(personId)
		if person != nil {
			offence.Defendants = append(offence.Defendants, person)
		}
		verdict := record.findVerdict(verdictId)
		if verdict != nil {
			offence.Verdict = verdict
		}
	}
}
開發者ID:codingkoi,項目名稱:oldbailey,代碼行數:35,代碼來源:record.go

示例6: NewRecord

func NewRecord(content []byte) (record *Record) {
	doc, err := gokogiri.ParseHtml([]byte(content))
	if err != nil {
		panic(err)
	}

	displayText := cleanUpContent(doc.String())
	record = &Record{RawText: content, DisplayText: displayText}
	dateStr := getInterp(doc.Root().NodePtr(), "date", doc)
	date, err := time.Parse("20060102", dateStr)
	if err != nil {
		record.Date = nil
	} else {
		record.Date = &date
	}

	xPath := xpath.NewXPath(doc.DocPtr())
	nodePtrs := xPath.Evaluate(doc.Root().NodePtr(),
		xpath.Compile("//div1"))

	node := xml.NewNode(nodePtrs[0], doc)
	record.Id = node.Attr("id")
	record.Type = node.Attr("type")

	record.processPersons(doc)
	record.processOffences(doc)
	record.processVerdicts(doc)
	record.processOffJoins(doc)
	return
}
開發者ID:codingkoi,項目名稱:oldbailey,代碼行數:30,代碼來源:record.go

示例7: SearchWithVariables

// As the Search function, but passing a VariableScope that can be used to reolve variable
// names or registered function references in the XPath being evaluated.
func (xmlNode *XmlNode) SearchWithVariables(data interface{}, v xpath.VariableScope) (result []Node, err error) {
	switch data := data.(type) {
	default:
		err = ERR_UNDEFINED_SEARCH_PARAM
	case string:
		if xpathExpr := xpath.Compile(data); xpathExpr != nil {
			defer xpathExpr.Free()
			result, err = xmlNode.SearchWithVariables(xpathExpr, v)
		} else {
			err = errors.New("cannot compile xpath: " + data)
		}
	case []byte:
		result, err = xmlNode.SearchWithVariables(string(data), v)
	case *xpath.Expression:
		xpathCtx := xmlNode.Document.DocXPathCtx()
		xpathCtx.SetResolver(v)
		nodePtrs, err := xpathCtx.EvaluateAsNodeset(unsafe.Pointer(xmlNode.Ptr), data)
		if nodePtrs == nil || err != nil {
			return nil, err
		}
		for _, nodePtr := range nodePtrs {
			result = append(result, NewNode(nodePtr, xmlNode.Document))
		}
	}
	return
}
開發者ID:thebrave,項目名稱:gokogiri,代碼行數:28,代碼來源:node.go

示例8: FiveThousandBest

func FiveThousandBest() (titles []string, err error) {
	res, err := http.Get("http://5000best.com/movies/1")
	if err != nil {
		return
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return
	}

	doc, err := gokogiri.ParseHtml(body)
	if err != nil {
		return
	}

	exp := xpath.Compile("//a[@class='n']")

	nodes := doc.XPathCtx.Evaluate(doc.NodePtr(), exp)

	for _, np := range nodes {
		node := xml.NewNode(np, doc)
		title := node.InnerHtml()
		title = title[:len(title)-8]
		titles = append(titles, title)
	}

	return
}
開發者ID:samnardoni,項目名稱:movies,代碼行數:29,代碼來源:movies.go

示例9: getNewsInfo

func getNewsInfo(doc *html.HtmlDocument) ([]xml.Node, error) {
	xp := "//body/div/div/div/div/div/div/div/div/div/div/div/div/div/div/ul/li"
	xps := xpath.Compile(xp)
	newDatas, err := doc.Root().Search(xps)
	if err != nil {
		return nil, err
	}
	return newDatas, nil
}
開發者ID:ieee0824,項目名稱:news-getter,代碼行數:9,代碼來源:yomiuri.go

示例10: getInterp

// get the value out of an <interp> tag
func getInterp(basePtr unsafe.Pointer, interpType string, doc *html.HtmlDocument) (value string) {
	xPath := xpath.NewXPath(doc.DocPtr())
	nodePtrs := xPath.Evaluate(basePtr, xpath.Compile(".//interp[@type='"+
		interpType+"']"))
	if len(nodePtrs) == 1 {
		node := xml.NewNode(nodePtrs[0], doc)
		value = node.Attr("value")
	}
	return
}
開發者ID:codingkoi,項目名稱:oldbailey,代碼行數:11,代碼來源:record.go

示例11: urlSeparation

func urlSeparation(newDatas []xml.Node) ([]string, error) {
	var ret []string

	a := xpath.Compile("./a/@href")

	for _, newData := range newDatas {
		urls, err := newData.Search(a)
		if err != nil {
			return nil, err
		}
		ret = append(ret, urls[0].Content())
	}
	return ret, nil
}
開發者ID:ieee0824,項目名稱:news-getter,代碼行數:14,代碼來源:yomiuri.go

示例12: main

func main() {
	doc, _ := gokogiri.ParseXml([]byte(a))
	defer doc.Free()
	xp := doc.DocXPathCtx()
	xp.RegisterNamespace("ns", "http://example.com/this")
	x := xpath.Compile("/ns:NodeA/ns:NodeB")
	groups, err := doc.Search(x)
	if err != nil {
		fmt.Println(err)
	}
	for i, group := range groups {
		fmt.Println(i, group.Content())
	}
}
開發者ID:suntong,項目名稱:lang,代碼行數:14,代碼來源:lx2_parse-namespace.go

示例13: parseHtml

func parseHtml(page []byte) (*[]DubizzleResult, error) {
	// parse the web page
	doc, err := gokogiri.ParseHtml(page)
	if err != nil {
		return nil, err
	}
	println("parsed the doc: \n")

	// perform operations on the parsed page
	xp := xpath.Compile("//*[@id='results-list']/div")
	result_list, err := doc.Root().Search(xp)
	if err != nil {
		return nil, err
	}

	ads := []DubizzleResult{}
	for _, rslt := range result_list {
		xptitle := xpath.Compile(".//h3[@id='title']/span[@class='title']/a")
		xpprice := xpath.Compile(".//div[@class='price']")
		title_info, title_err := rslt.Search(xptitle)
		price_info, price_err := rslt.Search(xpprice)
		if title_err == nil || price_err == nil {
			if len(title_info) > 0 && len(price_info) > 0 {
				title := title_info[0].InnerHtml()
				price := price_info[0].InnerHtml()
				price = strings.TrimSpace(price)
				price = strings.Trim(price, "<br>")
				price = strings.TrimSpace(price)
				url := title_info[0].Attribute("href").String()
				ads = append(ads, DubizzleResult{Title: title, Price: price, Url: url})
			}
		}
	}
	doc.Free()
	return &ads, err
}
開發者ID:Qwinix,項目名稱:go-dubizzle,代碼行數:36,代碼來源:dubizzle.go

示例14: processVerdicts

func (record *Record) processVerdicts(doc *html.HtmlDocument) {
	xPath := xpath.NewXPath(doc.DocPtr())
	verdictPtrs := xPath.Evaluate(doc.Root().NodePtr(),
		xpath.Compile("//rs[@type='verdictDescription']"))
	verdicts := make([]Verdict, len(verdictPtrs))

	for i, nodePtr := range verdictPtrs {
		node := xml.NewNode(nodePtr, doc)
		verdict := Verdict{}
		verdict.Id = node.Attr("id")
		verdict.Desc = cleanUpContent(node.Content())
		verdict.SetType(getInterp(nodePtr, "verdictCategory", doc))
		verdicts[i] = verdict
	}
	record.Verdicts = verdicts
}
開發者ID:codingkoi,項目名稱:oldbailey,代碼行數:16,代碼來源:record.go

示例15: photoSeparation

func photoSeparation(newDatas []xml.Node) ([]bool, error) {
	var ret []bool
	a := xpath.Compile("./a/span[@class='icon-photo']")
	for _, newData := range newDatas {
		icons, err := newData.Search(a)
		if err != nil {
			return nil, err
		}
		if len(icons) == 0 {
			ret = append(ret, false)
		} else {
			ret = append(ret, true)
		}
	}
	return ret, nil
}
開發者ID:ieee0824,項目名稱:news-getter,代碼行數:16,代碼來源:yomiuri.go


注:本文中的github.com/moovweb/gokogiri/xpath.Compile函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。