Given the below XML
</parties>
<party>
<name>Tom</name>
<typeCode>D</typeCode>
<typeDesc>Defendant</typeDesc>
<info></info>
<otherData>
<partySeqNum>2</partySeqNum>
<partyTitle></partyTitle>
<partyAddDate></partyAddDate>
<terminateDate></terminateDate>
<indexPartyName>Tom</indexPartyName>
</otherData>
</party>
<party>
<name>Jim</name>
<typeCode>DE</typeCode>
<typeDesc>Defendant</typeDesc>
<info></info>
<otherData>
<partySeqNum>2</partySeqNum>
<partyTitle></partyTitle>
<partyAddDate></partyAddDate>
<terminateDate></terminateDate>
<indexPartyName>Tom</indexPartyName>
</otherData>
</party>
</parties>
我有一个util,它返回xpath:/parties/party(1)/typeDesc
有没有办法可以让表达式返回/parties/party(1)/typeDesc
之前的兄弟/parties/party(1)/typeCode
。
我很一般,无论我得到什么xpath表达式,我都需要得到它以前的兄弟。
xPath.compile(expression).evaluate(xmlDocumentOrg, XPathConstants.NODE);
答案 0 :(得分:0)
String expression = "/parties/party[1]/typeDesc";
String precedingSiblingExpression = expression + "/preceding-sibling::*[1]";
Node result = (Node) xpath.compile(precedingSiblingExpression).evaluate(source, XPathConstants.NODE);