org.jsoup.nodes.Element.cssSelector()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(6.2k)|赞(0)|评价(0)|浏览(204)

本文整理了Java中org.jsoup.nodes.Element.cssSelector()方法的一些代码示例,展示了Element.cssSelector()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Element.cssSelector()方法的具体详情如下:
包路径:org.jsoup.nodes.Element
类名称:Element
方法名:cssSelector

Element.cssSelector介绍

[英]Get a CSS selector that will uniquely select this element.

If the element has an ID, returns #id; otherwise returns the parent (if any) CSS selector, followed by '>', followed by a unique selector for the element (tag.class.class:nth-child(n)).
[中]获取将唯一选择此元素的CSS选择器。
如果元素具有ID,则返回#ID;否则,返回父级(如果有)CSS选择器,后跟'>',后跟元素的唯一选择器(tag.class.class:nth child(n))。

代码示例

代码示例来源:origin: org.jsoup/jsoup

/**
 * Get a CSS selector that will uniquely select this element.
 * <p>
 * If the element has an ID, returns #id;
 * otherwise returns the parent (if any) CSS selector, followed by {@literal '>'},
 * followed by a unique selector for the element (tag.class.class:nth-child(n)).
 * </p>
 *
 * @return the CSS Path that can be used to retrieve the element in a selector.
 */
public String cssSelector() {
  if (id().length() > 0)
    return "#" + id();
  // Translate HTML namespace ns:tag to CSS namespace syntax ns|tag
  String tagName = tagName().replace(':', '|');
  StringBuilder selector = new StringBuilder(tagName);
  String classes = StringUtil.join(classNames(), ".");
  if (classes.length() > 0)
    selector.append('.').append(classes);
  if (parent() == null || parent() instanceof Document) // don't add Document to selector, as will always have a html node
    return selector.toString();
  selector.insert(0, " > ");
  if (parent().select(selector.toString()).size() > 1)
    selector.append(String.format(
      ":nth-child(%d)", elementSiblingIndex() + 1));
  return parent().cssSelector() + selector.toString();
}

代码示例来源:origin: de.unistuttgart.ims/de.unistuttgart.ims.drama.io.core

@Deprecated
public static <T extends Annotation> T selectRange2Annotation(JCas jcas, Element rootElement,
    Map<String, XMLElement> annoMap, String beginCssSelector, String endCssSelector, Class<T> annoClass) {
  Elements elms = rootElement.select(beginCssSelector);
  int begin = jcas.size();
  for (Element elm : elms) {
    XMLElement hAnno = annoMap.get(elm.cssSelector());
    if (hAnno.getBegin() < begin)
      begin = hAnno.getBegin();
  }
  elms = rootElement.select(endCssSelector);
  int end = 0;
  for (Element elm : elms) {
    XMLElement hAnno = annoMap.get(elm.cssSelector());
    if (hAnno.getEnd() > end)
      end = hAnno.getEnd();
  }
  return AnnotationFactory.createAnnotation(jcas, begin, end, annoClass);
}

代码示例来源:origin: de.unistuttgart.ims.uima.io/generic-xml-reader

protected <T extends TOP> void applyRule(JCas jcas, Element rootElement, Map<String, XMLElement> annoMap,
    Rule<T> mapping) {
  Elements elms = rootElement.select(mapping.getSelector());
  for (Element elm : elms) {
    XMLElement hAnno = annoMap.get(elm.cssSelector());
    if (elm.hasText() || elm.childNodeSize() > 0) {
      T annotation = getFeatureStructure(jcas, hAnno, elm, mapping);
      if (mapping.getCallback() != null && annotation != null)
        mapping.getCallback().accept(annotation, elm);
    }
  }
}

代码示例来源:origin: de.unistuttgart.ims/uimautil

protected <T extends TOP> void applyRule(JCas jcas, Element rootElement, Map<String, XMLElement> annoMap,
    Rule<T> mapping) {
  Elements elms = rootElement.select(mapping.getSelector());
  for (Element elm : elms) {
    XMLElement hAnno = annoMap.get(elm.cssSelector());
    if (elm.hasText() || elm.childNodeSize() > 0) {
      T annotation = getFeatureStructure(jcas, hAnno, elm, mapping);
      if (mapping.getCallback() != null && annotation != null)
        mapping.getCallback().accept(annotation, elm);
    }
  }
}

代码示例来源:origin: de.unistuttgart.ims/uimautil

public void tail(Node node, int depth) {
  if (node.getClass().equals(Element.class)) {
    Element elm = (Element) node;
    XMLElement anno = builder.add(beginMap.get(node), XMLElement.class);
    anno.setTag(elm.tagName());
    anno.setId(elm.id());
    anno.setSelector(elm.cssSelector());
    anno.setAttributes(elm.attributes().html());
    if (elm.className().isEmpty())
      anno.setCls(elm.attr("type"));
    else
      anno.setCls(elm.className());
    annotationMap.put(elm.cssSelector(), anno);
    if (!this.preserveWhitespace)
      if (elm.isBlock() || ArrayUtils.contains(blockElements, elm.tagName()))
        builder.add("\n");
  }
}

代码示例来源:origin: com.norconex.collectors/norconex-importer

return element.cssSelector();

代码示例来源:origin: de.unistuttgart.ims/de.unistuttgart.ims.drama.io.core

@Deprecated
public static <T extends Annotation> Collection<T> select2Annotation(JCas jcas, Element rootElement,
    Map<String, XMLElement> annoMap, String cssSelector, Class<T> annoClass, Annotation coveringAnnotation,
    Select2AnnotationCallback<T> callback) {
  HashSet<T> set = new HashSet<T>();
  Elements elms = rootElement.select(cssSelector);
  for (Element elm : elms) {
    XMLElement hAnno = annoMap.get(elm.cssSelector());
    if (elm.hasText() || elm.childNodeSize() > 0) {
      if (coveringAnnotation == null || (coveringAnnotation.getBegin() <= hAnno.getBegin()
          && coveringAnnotation.getEnd() >= hAnno.getEnd())) {
        T annotation = AnnotationFactory.createAnnotation(jcas, hAnno.getBegin(), hAnno.getEnd(),
            annoClass);
        if (callback != null)
          callback.call(annotation, elm);
        set.add(annotation);
      }
    }
  }
  return set;
}

代码示例来源:origin: de.unistuttgart.ims.uima.io/generic-xml-reader

@Override
public void tail(Node node, int depth) {
  if (node instanceof Element) {
    Element elm = (Element) node;
    XMLElement anno = builder.add(beginMap.get(node), XMLElement.class);
    anno.setTag(elm.tagName());
    anno.setId(elm.id());
    anno.setSelector(elm.cssSelector());
    anno.setAttributes(elm.attributes().html());
    if (elm.className().isEmpty())
      anno.setCls(elm.attr("type"));
    else
      anno.setCls(elm.className());
    annotationMap.put(elm.cssSelector(), anno);
    if (!this.preserveWhitespace)
      if (elm.isBlock() || ArrayUtils.contains(blockElements, elm.tagName()))
        builder.add("\n");
  } else if (node instanceof XmlDeclaration) {
    XmlDeclaration xmlDecl = (XmlDeclaration) node;
    XmlDeclarationAnnotation anno = builder.add(beginMap.get(node), XmlDeclarationAnnotation.class);
    anno.setOuterHtml(xmlDecl.outerHtml());
  }
}

代码示例来源:origin: com.norconex.collectors/norconex-importer

childMeta.load(doc.getMetadata());
String childContent = elm.outerHtml();
String childEmbedRef = elm.cssSelector();
String childRef = doc.getReference() + "!" + childEmbedRef;
CachedInputStream content = null;

代码示例来源:origin: de.unistuttgart.ims/de.unistuttgart.ims.drama.io.core

XMLElement hAnno = annoMap.get(elm.cssSelector());
Utterance utterance = JCasUtil.selectPreceding(Utterance.class, hAnno, 1).get(0);
utterance.setEnd(hAnno.getEnd());

相关文章

Element类方法