xpath to select based on one xml element but retrieve another element
This is a truncated example of one record in some auto-generated XML that I have to work with.
<marcrecord>
<marcdatafield tag="245" ind1="0" ind2="0">
<marcsubfield code="a">Argumentation and advocacy :</marcsubfield>
<marcsubfield code="b">the journal of the American Forensic Association.</marcsubfield>
</marcdatafield>
<marcdatafield tag="246" ind1="1" ind2="7">
<marcsubfield code="a">Journal of the American Forensic Association</marcsubfield>
<marcsubfield code="f">summer 1988-spring 1989</marcsubfield>
</marcdatafield>
<marcdatafield tag="246" ind1="3" ind2="">
<marcsubfield code="a">Argumentation & advocacy</marcsubfield>
</marcdatafield>
<marcdatafield tag="260" ind1="" ind2="">
<marcsubfield code="a">[River Falls, WI] :</marcsubfield>
<marcsubfield code="b">The Association,</marcsubfield>
<marcsubfield code="c">[1988-</marcsubfield>
</marcdatafield>
<marcdatafield tag="300" ind1="" ind2="">
<marcsubfield code="a">v. ;</marcsubfield>
<marcsubfield code="c">26 cm.</marcsubfield>
</marcdatafield>
<marcdatafield tag="650" ind1="" ind2="0">
<marcsubfield code="a">Forensics (Public speaking)</marcsubfield>
<marcsubfield code="v">Periodicals.</marcsubfield>
</marcdatafield>
<marcdatafield tag="710" ind1="2" ind2="">
<marcsubfield code="a">American Forensic Association.</marcsubfield>
</marcdatafield>
<marcdatafield tag="780" ind1="0" ind2="0">
<marcsubfield code="a">American Forensic Association.</marcsubfield>
<marcsubfield code="t">Journal of the American Forensic Association</marcsubfield>
<marcsubfield code="x">0002-8533</marcsubfield>
<marcsubfield code="w">(DLC)sf 80000452</marcsubfield>
<marcsubfield code="w">(OCoLC)1479880</marcsubfield>
</marcdatafield>
<marcrecord>
I need to show the data in the marcdatafield tagged as 780 (specifically the one subfield coded t). The specific 780 subfield to retrieve is based on finding a match in the previous subfield of the datafield tagged as 245 (this is the title)
The user's title search string is in the variable called title and the XPath below seems to do a decent job of finding the specific record based on开发者_JAVA技巧 a title match.
XPathExpression expr = xpath.compile("//marcdatafield[marcsubfield='" +title+ "']/*/text()");
But I'm stumped as to how to get it to actually retrieve the associated 780 field instead of just the title field. (FWIW, this is using native Java XPath classes and I'll be using it in JSP)
Thanks for any help,
Ceci
I think you are looking for this:
//marcdatafield/marcsubfield[@code='t'][//marcdatafield[@tag='246']/marcsubfield[@code='a']/text()]
I'm assuming that you really meant the title under tag 246 subfield tagged a because that title matches and the one under tag 245 subfield b desn't.
This would make your code:
XPathExpression expr = xpath.compile("//marcdatafield/marcsubfield[@code='t'][//marcdatafield[marcsubfield='" +title+ "']/*/text()]");
try this example :
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader(
"tds.xml")), XPathConstants.NODESET);
for (int i = 0; i < shows.getLength(); i++) {
Element show = (Element) shows.item(i);
String guestName = xPath.evaluate("guest/name", show);
String guestCredit = xPath.evaluate("guest/credit", show);
System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date") + " - "
+ guestName + " (" + guestCredit + ")");
}
精彩评论