如何使用XPath连接元素?

时间:2021-11-08 16:58:11

I an analysing an XML document with XPath and I have the following data:

我用XPath分析XML文档,我有以下数据:

<rdf:Description rdf:about="https://<server>/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/57108">
    <rdf:type rdf:resource="http://open-services.net/ns/cm#ChangeRequest"/>
    <dcterms:type rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Defect</dcterms:type>
    <dcterms:identifier rdf:datatype="http://www.w3.org/2001/XMLSchema#string">57108</dcterms:identifier>
    <dcterms:title rdf:parseType="Literal">
        The page does not scroll, Save Button not clickable
    </dcterms:title>
</rdf:Description>
<rdf:Description rdf:about="https://<server>/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/56645">
    <rdf:type rdf:resource="http://open-services.net/ns/cm#ChangeRequest"/>
    <dcterms:type rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Defect</dcterms:type>
    <dcterms:identifier rdf:datatype="http://www.w3.org/2001/XMLSchema#string">56645</dcterms:identifier>
    <dcterms:title rdf:parseType="Literal">
        NewMobApp Android: behavior of the swipe is incorrect
    </dcterms:title>
</rdf:Description>

What I am trying to do is format the data so that I get the following output:

我想要做的是格式化数据,以便我得到以下输出:

57108 - The page does not scroll, Save Button not clickable
56645 - NewMobApp Android: behavior of the swipe is incorrect

Is this at all possible? I have tried

这是可能吗?我试过了

String workItemNodes = (String) (xpath.evaluate(workItemMembersXPath, source, XPathConstants.STRING));

where

String workItemMembersXPath ="concat(//dcterms:identifier, //dcterms:title)";   

but it only returns the first result. I am trying to somehow put it into a NodeList, but I am not having much luck. I am still new to XPath.

但它只返回第一个结果。我试图以某种方式把它放入NodeList,但我没有太多运气。我还是XPath的新手。

Am I on the right track, is this even possible, and are there better/easier ways of going about it?

我是在正确的轨道上,这是否可能,并且有更好/更容易的方法吗?

2 个解决方案

#1


0  

First of all: it looks like your working with an RDF format, you might want to consider using jena instead of xpath. Actually I am not too familiar with rdf/jena. RDF can be expressed as XML so that would suppose that you can use XPath.

首先:看起来您使用的是RDF格式,您可能需要考虑使用jena而不是xpath。其实我对rdf / jena不太熟悉。 RDF可以表示为XML,因此可以假设您可以使用XPath。

Using XPath I'd suggest using the /text() to get the text content of a node, this should help you overcome the empty lines in the title element (above and below the actual title).

使用XPath我建议使用/ text()来获取节点的文本内容,这应该可以帮助你克服title元素中的空行(实际标题的上方和下方)。

String workItemMembersXPath ="concat(//dcterms:identifier/text(), //dcterms:title/text())";   

#2


0  

Sorry for not providing a direct answer, but I feel this is important information.

很抱歉没有直接回答,但我觉得这是重要的信息。

I urge you to not handle RDF/XML with XPath. We have tried that at work and it is a horrible, error-prone idea.

我强烈建议您不要使用XPath处理RDF / XML。我们在工作中尝试过它,这是一个可怕的,容易出错的想法。

The main problem that you have is that the XML you will access is not static. And I don't only mean the number of statements, but the representation of these statements in RDF/XML. Whatever you use to serialize your graph in RDF/XML will, I am pretty sure of that, not guarantee you the same output each time. That right there should tell you to abandon the idea of using XPath.

您遇到的主要问题是您将访问的XML不是静态的。我不仅指语句的数量,还指RDF / XML中这些语句的表示。无论您使用哪种方式在RDF / XML中序列化图形,我都非常确定,不能保证每次都输出相同的输出。那就应该告诉你放弃使用XPath的想法。

For example, it's perfectly valid to express the graph like this:

例如,像这样表达图形是完全有效的:

<rdf:Description rdf:about="https://<server>/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/56645">
    <rdf:type rdf:resource="http://open-services.net/ns/cm#ChangeRequest"/>
</rdf:Description>

<rdf:Description rdf:about="https://<server>/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/56645">
    <dcterms:type rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Defect</dcterms:type>
</rdf:Description>

<rdf:Description rdf:about="https://<server>/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/56645">
    <dcterms:identifier rdf:datatype="http://www.w3.org/2001/XMLSchema#string">56645</dcterms:identifier>
</rdf:Description>

<rdf:Description rdf:about="https://<server>/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/56645">
    <dcterms:title rdf:parseType="Literal">
        NewMobApp Android: behavior of the swipe is incorrect
    </dcterms:title>
</rdf:Description>

That's the same subgraph as above, just represented slightly different. While one XPath expression might work with this, if you continue to exploit RDF using XPath you will run into problems with this sooner or later, I can promise you that.

这与上面的子图相同,只是略有不同。虽然一个XPath表达式可能适用于此,但如果您继续使用XPath利用RDF,您迟早会遇到问题,我可以向您保证。

#1


0  

First of all: it looks like your working with an RDF format, you might want to consider using jena instead of xpath. Actually I am not too familiar with rdf/jena. RDF can be expressed as XML so that would suppose that you can use XPath.

首先:看起来您使用的是RDF格式,您可能需要考虑使用jena而不是xpath。其实我对rdf / jena不太熟悉。 RDF可以表示为XML,因此可以假设您可以使用XPath。

Using XPath I'd suggest using the /text() to get the text content of a node, this should help you overcome the empty lines in the title element (above and below the actual title).

使用XPath我建议使用/ text()来获取节点的文本内容,这应该可以帮助你克服title元素中的空行(实际标题的上方和下方)。

String workItemMembersXPath ="concat(//dcterms:identifier/text(), //dcterms:title/text())";   

#2


0  

Sorry for not providing a direct answer, but I feel this is important information.

很抱歉没有直接回答,但我觉得这是重要的信息。

I urge you to not handle RDF/XML with XPath. We have tried that at work and it is a horrible, error-prone idea.

我强烈建议您不要使用XPath处理RDF / XML。我们在工作中尝试过它,这是一个可怕的,容易出错的想法。

The main problem that you have is that the XML you will access is not static. And I don't only mean the number of statements, but the representation of these statements in RDF/XML. Whatever you use to serialize your graph in RDF/XML will, I am pretty sure of that, not guarantee you the same output each time. That right there should tell you to abandon the idea of using XPath.

您遇到的主要问题是您将访问的XML不是静态的。我不仅指语句的数量,还指RDF / XML中这些语句的表示。无论您使用哪种方式在RDF / XML中序列化图形,我都非常确定,不能保证每次都输出相同的输出。那就应该告诉你放弃使用XPath的想法。

For example, it's perfectly valid to express the graph like this:

例如,像这样表达图形是完全有效的:

<rdf:Description rdf:about="https://<server>/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/56645">
    <rdf:type rdf:resource="http://open-services.net/ns/cm#ChangeRequest"/>
</rdf:Description>

<rdf:Description rdf:about="https://<server>/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/56645">
    <dcterms:type rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Defect</dcterms:type>
</rdf:Description>

<rdf:Description rdf:about="https://<server>/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/56645">
    <dcterms:identifier rdf:datatype="http://www.w3.org/2001/XMLSchema#string">56645</dcterms:identifier>
</rdf:Description>

<rdf:Description rdf:about="https://<server>/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/56645">
    <dcterms:title rdf:parseType="Literal">
        NewMobApp Android: behavior of the swipe is incorrect
    </dcterms:title>
</rdf:Description>

That's the same subgraph as above, just represented slightly different. While one XPath expression might work with this, if you continue to exploit RDF using XPath you will run into problems with this sooner or later, I can promise you that.

这与上面的子图相同,只是略有不同。虽然一个XPath表达式可能适用于此,但如果您继续使用XPath利用RDF,您迟早会遇到问题,我可以向您保证。