xpath基于一个xml元素进行选择,但检索另一个元素

时间:2011-04-19 23:16:53

标签: xml xpath

这是我必须使用的一些自动生成的XML中的一条记录的截断示例。

<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 &amp; 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>

我需要在标记为780的marcdatafield中显示数据(特别是一个子字段编码为t)。要检索的特定780子字段基于在标记为245的数据字段的前一个子字段中找到匹配项(这是标题)

用户的标题搜索字符串位于名为title的变量中,下面的XPath似乎可以很好地根据标题匹配查找特定记录。

XPathExpression expr = xpath.compile(“// marcdatafield [marcsubfield ='”+ title +“'] / * / text()”);

但我很难过如何让它实际检索关联的780字段而不仅仅是标题字段。 (FWIW,这是使用本机Java XPath类,我将在JSP中使用它)

感谢您的帮助,

塞西

2 个答案:

答案 0 :(得分:1)

我认为你正在寻找这个:

//marcdatafield/marcsubfield[@code='t'][//marcdatafield[@tag='246']/marcsubfield[@code='a']/text()]

我假设您真的意味着标签246子字段下的标题标记为a,因为该标题匹配且标记245子字段b下的标题不是。

这将使您的代码成为:

XPathExpression expr = xpath.compile("//marcdatafield/marcsubfield[@code='t'][//marcdatafield[marcsubfield='" +title+ "']/*/text()]");

答案 1 :(得分:0)

试试这个例子:

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 + ")");
    }