【问题标题】:Get XPath from XML Document Present in Marklogic Database从 Marklogic 数据库中存在的 XML 文档获取 XPath
【发布时间】:2018-09-01 06:50:01
【问题描述】:

我是 Marklogic 的新手,我的要求是,我在 Marklogic 数据库中有包含多个元素的 XML 文档。

 ** Example :**    

<tXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Header>
        <Source>Source1</Source>
        <Action_Type>Action_Type1</Action_Type>
        <Sequence_Number>Sequence_Number1</Sequence_Number>
    </Header>
</tXML>

当我通过“Sequence_Number”时,它应该返回 /tXML/Header/Sequence_Number 并且可以多次使用“Sequence_Number”元素。

请告诉我是否可以使用 Marklogic XQuery/Java API 或者我需要使用任何第三方 API 来获得此结果。

【问题讨论】:

    标签: marklogic marklogic-8


    【解决方案1】:

    它可能在大型文档上表现不佳,但您可以对元素名称使用字符串匹配来查找元素,然后使用xdmp:path 获取该文档的相应 XPath。比如:

    xquery version "1.0-ml";
    
    let $xml := document {
    <tXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Header>
            <Source>Source1</Source>
            <Action_Type>Action_Type1</Action_Type>
            <Sequence_Number>Sequence_Number1</Sequence_Number>
        </Header>
    </tXML>
    }
    let $elem-name := "Sequence_Number"
    let $elems := $xml//*[local-name() eq $elem-name]
    return $elems ! xdmp:path(.)
    

    HTH!

    【讨论】:

    • 甚至更短(可能更高效)将避免让变量与匹配的元素,并简单地返回 XPath 序列:return $xml//*[local-name() eq $elem-name]/xdmp:path(.)
    【解决方案2】:

    如果您通过String“Sequence_Number”,那么它怎么知道Sequence_Number 兄弟姐妹中的哪一个获得XPath?您可能会谈论第一次出现,也可能是第二次或第三次,它不会知道。仅给定一个 tagName,它无法知道要为哪个事件获取 XPath。

    也就是说,下面的方法将为您提供元素所在位置的一般 XPath。你只需要先获取org.w3c.dom.Element,可以从org.w3c.dom.Document获取。

    public static String getXPathOfElement(org.w3c.dom.Element el) {
        Objects.requireNonNull(el);
        LinkedList<String> list = new LinkedList<>();
        for (Node n = el; n != null; n = n.getParentNode()) {
            if (n.getNodeType() == Node.ELEMENT_NODE)
                list.push(n.getNodeName());
            else
                list.push("");
        }
        return String.join("/", list);
    }
    
    
    public static void main(String[] args)
            throws ParserConfigurationException, SAXException, IOException {
    
        // Get an input stream of your Xml somehow
        String xml =
                "<tXML xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
                + "<Header>"
                + "<Source>Source1</Source>"
                + "<Action_Type>Action_Type1</Action_Type>"
                + "<Sequence_Number>Sequence_Number1</Sequence_Number>"
                + "</Header>"
                + "</tXML>";
    
        java.io.InputStream xmlInputStream = new java.io.ByteArrayInputStream(xml.getBytes());
    
        // Get the Document from the xml InputStream
        javax.xml.parsers.DocumentBuilderFactory docBuilderFact =
                javax.xml.parsers.DocumentBuilderFactory.newInstance();
        javax.xml.parsers.DocumentBuilder docBuilder = docBuilderFact.newDocumentBuilder();
        org.w3c.dom.Document doc = docBuilder.parse(xmlInputStream);
    
        // Get the Element you want the general XPath of
        // In this case just find the first one with a certain tag in the Document
        org.w3c.dom.Element el =
                (org.w3c.dom.Element) doc.getElementsByTagName("Sequence_Number").item(0);
    
        System.out.println(getXPathOfElement(el));
    }
    

    【讨论】:

    • 感谢 xtratic 的回复,但我不能接受这个 (org.w3c.dom.Element) doc.getElementsByTagName("Sequence_Number").item(0);正如我提到的,Sequence_Number 可以在多个父节点下重复多次,所以我需要所有出现 Sequence_Number 的 xpath
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多