How to distinguish between attribute and element nodes returned from a Saxon XPathSelector

萝らか妹 提交于 2019-12-06 01:35:57

I discovered the answer just as I finished writing the question, so I'll share it for others.

After casting the XdmItem to an XdmNode, you can call XdmNode.getNodeKind(), which returns a value from the XdmNodeKind enumeration specifying which type of node it is:

        XdmValue matchList = xPathSelector.evaluate();
        XdmItem firstItem = matchList.itemAt(0);
        if (firstItem instanceof XdmNode) {
           XdmNode xdmNode = (XdmNode) firstItem;
           XdmNodeKind nodeKind = xdmNode.getNodeKind();
           if (nodeKind == XdmNodeKind.ELEMENT) {
              return xdmNode.toString();
           }
        }
        return firstItem.getStringValue();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!