func (n *Node) xpath(stopAtDocument, stopAtID bool) string {
	n.RLock()
	defer n.RUnlock()

	p := ""
	pos := ""
	id := n.AttributeValue("id")
	switch {
	case n.Parent == nil:
		return n.LocalName

	case stopAtDocument && n.NodeType == NodeTypeDocument:
		return ""

	case stopAtID && id != "":
		p = "/"
		pos = `[@id='` + id + `']`

	case n.Parent != nil:
		var i int
		var found bool

		n.Parent.RLock()
		for j := 0; j < len(n.Parent.Children); j++ {
			if n.Parent.Children[j].LocalName == n.LocalName {
				i++
			}
			if n.Parent.Children[j].NodeID == n.NodeID {
				found = true
				break
			}
		}
		n.Parent.RUnlock()

		if found {
			pos = "[" + strconv.Itoa(i) + "]"
		}

		p = n.Parent.xpath(stopAtDocument, stopAtID)
	}

	return p + "/" + n.LocalName + pos
}
func (n *Node) xpath(stopAtDocument, stopAtID bool) string {
	n.RLock()
	defer n.RUnlock()

	p := ""
	pos := ""
	id := n.AttributeValue("id")
	switch {
	case n.Parent == nil:
		return n.LocalName

	case stopAtDocument && n.NodeType == NodeTypeDocument:
		return ""

	case stopAtID && id != "":
		p = "/"
		pos = `[@id='` + id + `']`

	case n.Parent != nil:
		var i int
		var found bool

		n.Parent.RLock()
		for j := 0; j < len(n.Parent.Children); j++ {
			if n.Parent.Children[j].LocalName == n.LocalName {
				i++
			}
			if n.Parent.Children[j].NodeID == n.NodeID {
				found = true
				break
			}
		}
		n.Parent.RUnlock()

		if found {
			pos = "[" + strconv.Itoa(i) + "]"
		}

		p = n.Parent.xpath(stopAtDocument, stopAtID)
	}

	return p + "/" + n.LocalName + pos
}
func ( n * Node ) xpath ( stopAtDocument , stopAtID bool ) string { n . RLock ( ) defer n . RUnlock ( ) p := " " pos := " " id := n . AttributeValue ( " " ) switch { case n . Parent == nil : return n . LocalName case stopAtDocument && n . NodeType == NodeTypeDocument : return " " case stopAtID && id != " " : p = " " pos = `[@id='` + id + `']` case n . Parent != nil : var i int var found bool n . Parent . RLock ( ) for j := 0 ; j < len ( n . Parent . Children ) ; j ++ { if n . Parent . Children [ j ] . LocalName == n . LocalName { i ++ } if n . Parent . Children [ j ] . NodeID == n . NodeID { found = true break } } n . Parent . RUnlock ( ) if found { pos = " " + strconv . Itoa ( i ) + " " } p = n . Parent . xpath ( stopAtDocument , stopAtID ) } return p + " " + n . LocalName + pos }
// xpath builds the xpath string.
xpath builds the xpath string .