Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在XQuery、XPath中包含多条处理指令会导致for循环_Xpath_Xquery_Jdom_Jdom 2 - Fatal编程技术网

在XQuery、XPath中包含多条处理指令会导致for循环

在XQuery、XPath中包含多条处理指令会导致for循环,xpath,xquery,jdom,jdom-2,Xpath,Xquery,Jdom,Jdom 2,我需要读取NAME=“CONTENTTYPE”的所有处理指令,并且我想要读取@VALUE并连接所有值,然后在XQuery/XPath中返回 我的XML: <REG > <MARKER MRKEID="SLREG:7.1" MRKTYPE="LD DU" MRKDATE="20130909" MRKTIME="10402688"/> <?METADATA NAME="CONTENTTYPE" VALUE="STATUTE"?> <?

我需要读取NAME=“CONTENTTYPE”的所有处理指令,并且我想要读取@VALUE并连接所有值,然后在XQuery/XPath中返回

我的XML:

<REG >
    <MARKER MRKEID="SLREG:7.1" MRKTYPE="LD DU" MRKDATE="20130909" MRKTIME="10402688"/>
    <?METADATA NAME="CONTENTTYPE" VALUE="STATUTE"?>
    <?METADATA NAME="CONTENTTYPE" VALUE="LEGISLATIVEDOCUMENT"?>
    <?METADATA NAME="CONTENTTYPE" VALUE="PRIMARYSOURCE"?>
    <?METADATA NAME="SLTAXTYPE" VALUE="PRIMARYSOURCE"?>
</REG>
感谢您在编写XQuery/XPath以获得上述输出方面提供的帮助

提前谢谢

问候,,
Hari

//处理指令('METADATA')[匹配(,'NAME=“CONTENTTYPE”VALUE=“[^”]*”)]/replace(在(,'VALUE=“”),'”,'',''之后的子字符串)
。这就是XPath 2.0。

使用JDOM标记帮助我找到了这一点

答案很长……XPath没有解析向ProcessingInstructions中添加“属性”的“标准”方式的本机能力。如果您想将值串联为单个XPath表达式的一部分,我认为您运气不好……实际上,Martin的答案看起来很有希望,但它将返回大量字符串值s、 不处理引用。JDOM 2.x需要XPath.compile(…)上的Filters.string(),您将得到一个
列表
结果到path.evaluate(doc)…我认为在XPath之外执行更简单。特别是考虑到JDOM 2.x使用Saxon库对XPath2.0的支持有限

至于以编程方式实现,JDOM 2.x提供了相当多的帮助。以您的示例XML为例,我采用了两种方法,第一种方法在XPath结果集上使用自定义过滤器。第二种方法有效地实现了相同的功能,但限制了PI在循环中的进一步使用

public static void main(String[] args) throws Exception {
    SAXBuilder saxb = new SAXBuilder();
    Document doc = saxb.build(new File("data.xml"));

    // This custom filter will return PI's that have the NAME="CONTENTTYPE" 'pseudo' attribute...
    @SuppressWarnings("serial")
    Filter<ProcessingInstruction> contenttypefilter = new AbstractFilter<ProcessingInstruction>() {

        @Override
        public ProcessingInstruction filter(Object obj) {
            // because we know the XPath expression selects Processing Instructions
            // we can safely cast here:
            ProcessingInstruction pi = (ProcessingInstruction)obj;
            if ("CONTENTTYPE".equals(pi.getPseudoAttributeValue("NAME"))) {
                return pi;
            }
            return null;
        }

    };

    XPathExpression<ProcessingInstruction> xp = XPathFactory.instance().compile(
            // search for all METADATA PI's.
            "//processing-instruction('METADATA')",
            // The XPath will return ProcessingInstruction content, which we
            // refine with our custom filter.
            contenttypefilter);

    StringBuilder sb = new StringBuilder();
    for (ProcessingInstruction pi : xp.evaluate(doc)) {
        sb.append(pi.getPseudoAttributeValue("VALUE")).append("\n");
    }
    System.out.println(sb);
}

这些处理指令的名称是元数据,例如“是您需要用自己的代码解析的非结构化数据
@VALUE
不起作用,它选择了该名称的属性,但只有元素节点有属性,处理指令没有。嗨,马丁,谢谢你的回复,但它只返回第一个值,即:规约。请让我知道如何获取这三个值。它应该返回一个序列,其中包含您发布的输入示例的三个字符串值。如果只得到一个值,则XPath处理器或API会隐藏部分结果。如何计算XPath?嗨,Martin,请告诉我,我们可以使用for循环获得相同的输出吗。@user2919291,您使用哪个XPathAPI?嗨,Martin,我们使用的是JDOM。
public static void main(String[] args) throws Exception {
    SAXBuilder saxb = new SAXBuilder();
    Document doc = saxb.build(new File("data.xml"));

    // This custom filter will return PI's that have the NAME="CONTENTTYPE" 'pseudo' attribute...
    @SuppressWarnings("serial")
    Filter<ProcessingInstruction> contenttypefilter = new AbstractFilter<ProcessingInstruction>() {

        @Override
        public ProcessingInstruction filter(Object obj) {
            // because we know the XPath expression selects Processing Instructions
            // we can safely cast here:
            ProcessingInstruction pi = (ProcessingInstruction)obj;
            if ("CONTENTTYPE".equals(pi.getPseudoAttributeValue("NAME"))) {
                return pi;
            }
            return null;
        }

    };

    XPathExpression<ProcessingInstruction> xp = XPathFactory.instance().compile(
            // search for all METADATA PI's.
            "//processing-instruction('METADATA')",
            // The XPath will return ProcessingInstruction content, which we
            // refine with our custom filter.
            contenttypefilter);

    StringBuilder sb = new StringBuilder();
    for (ProcessingInstruction pi : xp.evaluate(doc)) {
        sb.append(pi.getPseudoAttributeValue("VALUE")).append("\n");
    }
    System.out.println(sb);
}
public static void main(String[] args) throws Exception {
    SAXBuilder saxb = new SAXBuilder();
    Document doc = saxb.build(new File("data.xml"));

    XPathExpression<ProcessingInstruction> xp = XPathFactory.instance().compile(
            // search for all METADATA PI's.
            "//processing-instruction('METADATA')",
            // Use the pre-defined filter to set the generic type
            Filters.processinginstruction());

    StringBuilder sb = new StringBuilder();
    for (ProcessingInstruction pi : xp.evaluate(doc)) {
        if (!"CONTENTTYPE".equals(pi.getPseudoAttributeValue("NAME"))) {
            continue;
        }
        sb.append(pi.getPseudoAttributeValue("VALUE")).append("\n");
    }
    System.out.println(sb);
}