如何使用 XPATH 获取 XML 元素的相对深度 [英] How to get relative depth of XML element using XPATH

查看:22
本文介绍了如何使用 XPATH 获取 XML 元素的相对深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从给定 XML 文件中的特定元素中找到给定 XML 元素的相对深度,我尝试使用 XPATH,但我对 XML 解析不是很熟悉,也没有得到想要的结果.我还需要在计数时忽略数据元素.

I am trying to find the relative depth of given XML element from specific element in the given XML file, I tried to use XPATH but I'm not very familiar with XML parsing and I'm not getting the desired result. I need as well to ignore the data elements while counting.

下面是我编写的代码和示例 XML 文件.例如.TS837_2000A_Loop元素中NM109_BillingProviderIdentifier的深度为4.

Below is the code that I have written and the sample XML file. E.g. the depth of NM109_BillingProviderIdentifier from TS837_2000A_Loop element is 4.

父节点为:TS837_2000A_Loop <NM1_SubLoop_2 由于 NM109_BillingProviderIdentifierNM1_BillingProviderName 的子代,因此 NM1_BillingProviderNameTS837_2000A_Loop 的相对深度为 4(包括 TS837_2000A_Loop).

The parent nodes are: TS837_2000A_Loop < NM1_SubLoop_2 < TS837_2010AA_Loop < NM1_BillingProviderName as NM109_BillingProviderIdentifier is a child of NM1_BillingProviderName and thus the relative depth of NM1_BillingProviderName from TS837_2000A_Loop is 4 (including TS837_2000A_Loop).

package com.xmlexamples;
import java.io.File;
import java.io.FileInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;


public class XmlParser {

public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new FileInputStream(new File("D://sample.xml")));

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        String expression;      
        expression = "count(NM109_BillingProviderIdentifier/preceding-sibling::TS837_2000A_Loop)+1";                
        Double d = (Double) xpath.compile(expression).evaluate(doc, XPathConstants.NUMBER);     
        System.out.println("position from  TS837_2000A_Loop " + d);

    }
}

<?xml version='1.0' encoding='UTF-8'?>
<X12_00501_837_P xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <TS837_2000A_Loop>
        <NM1_SubLoop_2>
            <TS837_2010AA_Loop>
                <NM1_BillingProviderName>
                    <NM103_BillingProviderLastorOrganizationalName>VNA of Cape Cod</NM103_BillingProviderLastorOrganizationalName>
                    <NM109_BillingProviderIdentifier>1487651915</NM109_BillingProviderIdentifier>
                </NM1_BillingProviderName>
                <N3_BillingProviderAddress>
                  <N301_BillingProviderAddressLine>8669 NORTHWEST 36TH ST </N301_BillingProviderAddressLine>
                </N3_BillingProviderAddress>
            </TS837_2010AA_Loop>
        </NM1_SubLoop_2>
    </TS837_2000A_Loop>
</X12_00501_837_P>     

推荐答案

获取任何节点深度的关键方法是通过计算其祖先(包括父节点、父节点的父节点等):

The pivotal method for getting the depth of any node is by counting its ancestors (which include the parent, the parent of the parent etc):

count(NM109_BillingProviderIdentifier/ancestor-or-self::*)

这会给你到根的计数.要获取 相对 计数,即从除根以外的任何对象中获取,假设名称不重叠,您可以这样做:

This will give you the count up to the root. To get the relative count, i.e. from anything other than the root, assuming the names do not overlap, you can do this:

count(NM109_BillingProviderIdentifier/ancestor-or-self::*)
- count(NM109_BillingProviderIdentifier/ancestor::TS837_2000A_Loop/ancestor::*)

根据当前元素还是基础元素应包含在计数中,使用ancestor-or-selfancestor 轴.

Depending on whether the current, or the base element should be included in the count, use the ancestor-or-self or ancestor axis.

PS:您可能应该感谢 Pietro Saccardi 如此善意地制作您的帖子和您的巨大(4kB on one)行..) 示例 XML 可读.

PS: you should probably thank Pietro Saccardi for so kindly making your post and your huge (4kB on one line..) sample XML readable.

这篇关于如何使用 XPATH 获取 XML 元素的相对深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆