Saxon 8(Java版)问题

发布于 2024-07-19 17:42:00 字数 2009 浏览 5 评论 0原文

我现在要指出的是,我是使用 saxon 的新手,并且我已经尝试遵循包中的文档和示例,但我只是没有运气解决这个问题。

基本上,我正在尝试使用 saxon v8 在 java 中进行一些 xml 处理。 为了让某些东西正常工作,我获取了包中包含的示例文件之一并根据我的需要进行了修改。 只要我不使用命名空间,它就可以工作,这就是我的问题。 如何解决命名空间问题? 我不太关心使用它,但它存在于我的 xml 中,所以我要么必须使用它,要么忽略它。 任何一种解决方案都可以。

无论如何,这是我的起始代码。 它不执行任何操作,只是进行 xpath 查询并尝试针对硬编码的 xml 文档使用它。

public static void main(String[] args) {
    String query = args[0];

    File XMLStream=null;
    String xmlFileName="doc.xml";
    OutputStream destStream=System.out;
    XQueryExpression exp=null;
    Configuration C=new Configuration();
    C.setSchemaValidation(false);
    C.setValidation(false);

    StaticQueryContext SQC=new StaticQueryContext(C);
    DynamicQueryContext DQC=new DynamicQueryContext(C);      
    QueryProcessor processor = new QueryProcessor(SQC);
    Properties props=new Properties();
    try{   
        exp=processor.compileQuery(query);
        XMLStream=new File(xmlFileName);
        InputSource XMLSource=new InputSource(XMLStream.toURI().toString());
        SAXSource SAXs=new SAXSource(XMLSource); 
        DocumentInfo DI=SQC.buildDocument(SAXs);
        DQC.setContextNode(DI);

        SequenceIterator iter = exp.iterator(DQC);
        while(true){
            Item i = iter.next();
            if(i != null){
                System.out.println(i.getStringValue());
            }
            else break;
        }
    }
    catch (Exception e){
        System.err.println(e.getMessage());
    }
}   

这里有一个示例 XML 文件...

<?xml version="1.0"?>
<ns1:animal xmlns:ns1="http://my.catservice.org/">
    <cat>
        <catId>8889</catId>
        <fedStatus>true</fedStatus>
    </cat>
</ns1:animal>

如果我使用包含命名空间的查询运行此文件,则会收到错误。 例如: /ns1:animal/cat/ 给出错误:“前缀 ns1 尚未声明”。

如果我从查询中删除 ns1: ,它不会给我任何结果。 如果我修改 xml 以删除“animal”前面的“ns1:”,我可以成功运行查询 /animal/cat/

任何帮助将不胜感激。 谢谢。

I'll point out now, that I'm new to using saxon, and I've tried following the docs and examples in the package, but I'm just not having luck with this problem.

Basically, I'm trying to do some xml processing in java using saxon v8. In order to get something working, I took one of the sample files included in the package and modified to my needs. It works so long as I'm not using namespaces, and that is my question. How can I get around the namespace problem? I don't really care to use it, but it exists in my xml, so I either have to use it or ignore it. Either solution is fine.

Anyway, here is my starter code. It doesn't do anything but take an xpath query try to use it against the hard coded xml doc.

public static void main(String[] args) {
    String query = args[0];

    File XMLStream=null;
    String xmlFileName="doc.xml";
    OutputStream destStream=System.out;
    XQueryExpression exp=null;
    Configuration C=new Configuration();
    C.setSchemaValidation(false);
    C.setValidation(false);

    StaticQueryContext SQC=new StaticQueryContext(C);
    DynamicQueryContext DQC=new DynamicQueryContext(C);      
    QueryProcessor processor = new QueryProcessor(SQC);
    Properties props=new Properties();
    try{   
        exp=processor.compileQuery(query);
        XMLStream=new File(xmlFileName);
        InputSource XMLSource=new InputSource(XMLStream.toURI().toString());
        SAXSource SAXs=new SAXSource(XMLSource); 
        DocumentInfo DI=SQC.buildDocument(SAXs);
        DQC.setContextNode(DI);

        SequenceIterator iter = exp.iterator(DQC);
        while(true){
            Item i = iter.next();
            if(i != null){
                System.out.println(i.getStringValue());
            }
            else break;
        }
    }
    catch (Exception e){
        System.err.println(e.getMessage());
    }
}   

An example XML file is here...

<?xml version="1.0"?>
<ns1:animal xmlns:ns1="http://my.catservice.org/">
    <cat>
        <catId>8889</catId>
        <fedStatus>true</fedStatus>
    </cat>
</ns1:animal>

If I run this with a query including the namespace, I get an error. For example:
/ns1:animal/cat/ gives the error: "Prefix ns1 has not been declared".

If I remove the ns1: from the query, it gives me nothing. If I doctor the xml to remove the "ns1:" prepended to "animal" I can run the query /animal/cat/ with success.

Any help would be greatly appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你另情深 2024-07-26 17:42:00

错误消息正确指出您的 xpath 表达式未指示名称空间前缀“ns1”的含义(绑定到)。 仅仅因为要操作的文档碰巧使用了“ns1”的绑定,并不意味着它就是应该使用的:这是因为在 XML 中,重要的是命名空间 URI,而前缀只是实际事物的便捷快捷方式。

那么:如何定义绑定? 有两种通用方法; 要么提供可以解析前缀的上下文,要么在 XPath 表达式中嵌入实际的 URI。

关于第一种方法,来自Saxon作者的这封电子邮件提到了JAXP方法XPath.setNamespaceContext(),类似地,< href="http://jaxen.codehaus.org/faq.html" rel="nofollow noreferrer">Jaxen XPath 处理器常见问题解答 有一些示例代码可以提供帮助
这不是很方便,因为您必须实现 NamespaceContext,但是一旦您实现了,您就可以设置了。

因此,符号方法...让我们看看: 使用 XPath 和 XPointer 的十大技巧 显示了以下示例:

要匹配使用名称空间声明的元素,例如:

xmlns:book="http://my.example.org/namespaces/book",

您可以使用 XPath 名称,例如:

{ http://my.example.org/namespaces/book}部分

希望能够被理解撒克逊人(或贾克森人)。

最后,如果您在使用上述解决方案之一时遇到任何问题,我建议您尽可能升级到 Saxon9。

Error message correctly points out that your xpath expression does not indicate what namespace prefix "ns1" means (binds to). Just because document to operate on happens to use binding for "ns1" does not mean it is what should be used: this because in XML, it's the namespace URI that matters, and prefixes are just convenient shortcuts to the real thing.

So: how do you define the binding? There are 2 generic ways; either provide a context that can resolve the prefix, or embed actual URI within XPath expression.

Regarding the first approach, this email from Saxon author mentions JAXP method XPath.setNamespaceContext(), similarly, Jaxen XPath processor FAQ has some sample code that could help
That's not very convenient, as you have to implement NamespaceContext, but once you have an implementation you'll be set.

So the notation approach... let's see: Top Ten Tips to Using XPath and XPointer shows this example:

to match element declared with namespace like:

xmlns:book="http://my.example.org/namespaces/book"

you use XPath name like:

{http://my.example.org/namespaces/book}section

which hopefully is understood by Saxon (or Jaxen).

Finally, I would recommend upgrading to Saxon9 if possible, if you have any trouble using one of above solutions.

猫九 2024-07-26 17:42:00

如果您想要开箱即用的东西,您可以查看 在 java 中嵌入 xquery。 有一个 github 项目,它使用 Saxon 来计算一些示例 XQuery 表达式。

问候

If you want to have something working out of the box, you can check out embedding-xquery-in-java. There's github project, which uses Saxon to evaluate some sample XQuery expressions.

Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文