本文整理汇总了Golang中aqwari/net/xml/xmltree.Element.SearchFunc方法的典型用法代码示例。如果您正苦于以下问题:Golang Element.SearchFunc方法的具体用法?Golang Element.SearchFunc怎么用?Golang Element.SearchFunc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aqwari/net/xml/xmltree.Element
的用法示例。
在下文中一共展示了Element.SearchFunc方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parse
func (s *Schema) parse(root *xmltree.Element, extra map[string]*xmltree.Element) (err error) {
defer catchParseError(&err)
// First pass: name all anonymous types with placeholder names.
var (
typeCounter int
updateAttr string
accum bool
)
for _, el := range root.SearchFunc(hasAnonymousType) {
if el.Name.Space != schemaNS {
continue
}
switch el.Name.Local {
case "element", "attribute":
updateAttr = "type"
accum = false
case "list":
updateAttr = "itemType"
accum = false
case "restriction":
updateAttr = "base"
accum = false
case "union":
updateAttr = "memberTypes"
accum = true
default:
return fmt.Errorf("Did not expect <%s> to have an anonymous type",
el.Prefix(el.Name))
}
for _, t := range el.SearchFunc(isType) {
typeCounter++
name := anonTypeName(typeCounter, s.TargetNS)
qname := el.Prefix(name)
t.SetAttr("", "name", name.Local)
t.SetAttr("", "_isAnonymous", "true")
if accum {
qname = el.Attr("", updateAttr) + " " + qname
}
el.SetAttr("", updateAttr, qname)
if !accum {
break
}
}
}
// Second pass: de-reference all group/element/attribute references.
for _, el := range root.SearchFunc(hasAttr("", "ref")) {
var found bool
sameType := and(isElem(el.Name.Space, el.Name.Local), hasAttr("", "name"))
if el.Name.Space != schemaNS {
continue
}
ref := el.ResolveDefault(el.Attr("", "ref"), s.TargetNS)
for ns, doc := range extra {
for _, real := range doc.SearchFunc(sameType) {
name := real.ResolveDefault(real.Attr("", "name"), ns)
if name == ref {
extraAttr := el.StartElement.Attr
el.Content = real.Content
el.StartElement = real.StartElement
el.Children = real.Children
el.Scope = *real.JoinScope(&el.Scope)
// In XML Schema, it is valid to
// reference another element and
// at the same time add attributes
// to it. We handle this by merging
// the attributes of elements and
// their references.
for _, attr := range extraAttr {
if attr.Name.Local == "ref" {
continue
}
el.SetAttr(attr.Name.Space, attr.Name.Local, attr.Value)
}
found = true
break
}
}
}
if !found {
return fmt.Errorf("could not dereference %s %s %s", el.Name.Local,
el.Resolve(el.Attr("", "ref")).Space, el.Resolve(el.Attr("", "ref")).Local)
}
}
// Final pass: parse all type declarations.
for _, el := range root.Search(schemaNS, "complexType") {
t := s.parseComplexType(el)
s.Types[t.Name] = t
}
for _, el := range root.Search(schemaNS, "simpleType") {
t := s.parseSimpleType(el)
s.Types[t.Name] = t
}
return err
}