0

我是胡萝卜新手,对xsl知之甚少,所以请多多包涵。我的输入文件是来自数据库的搜索结果的 xml。它不是胡萝卜格式,因此根据文档,我必须使用 xsl 文件将其转换为胡萝卜格式。我遵循了胡萝卜提供的示例 xsl(用于 bbc 新闻),但它没有找到我指定的标题(atl)、片段(ab)和链接(url)(得到返回消息“您的查询没有返回任何文档”) . 这是文件,如果有人可以提供帮助,我将不胜感激。太感谢了。

xml(我的 3 个示例搜索记录)

<?xml version="1.0" encoding="UTF-8"?>

<records>
  <rec resultID="1">
    <header shortDbName="aph" longDbName="Academic Search Premier" uiTerm="43811908">
      <controlInfo>
        <bkinfo />
        <jinfo>
          <jtl>BMC Public Health</jtl>
          <issn>14712458</issn>
        </jinfo>
        <pubinfo>
          <dt year="2007" month="01" day="01">2007</dt>
          <vid>7</vid>
        </pubinfo>
        <artinfo>
          <ppf>31</ppf>
          <ppct>12</ppct>
          <formats>
            <fmt type="PDF" size="322KB" />
          </formats>
          <tig>
            <atl>Cocaine- and opiate-related fatal overdose in New York City, 1990-2000.</atl>
          </tig>
          <aug />
          <sug />
          <pubtype>Academic Journal</pubtype>
          <doctype>Article</doctype>
        </artinfo>
        <language code="eng">English</language>
      </controlInfo>
      <displayInfo>
        <pLink>
          <url>http://survey.hshsl.umaryland.edu/?url=http://search.ebscohost.com/login.aspx?direct=true&amp;db=aph&amp;AN=43811908&amp;site=eds-live</url>
        </pLink>
      </displayInfo>
    </header>
  </rec>
  <rec resultID="2">
    <header shortDbName="aph" longDbName="Academic Search Premier" uiTerm="43830400">
      <controlInfo>
        <bkinfo />
        <jinfo>
          <jtl>BMC Public Health</jtl>
          <issn>14712458</issn>
        </jinfo>
        <pubinfo>
          <dt year="2007" month="01" day="01">2007</dt>
          <vid>7</vid>
        </pubinfo>
        <artinfo>
          <ppf>31</ppf>
          <ppct>12</ppct>
          <formats>
            <fmt type="PDF" size="312KB" />
          </formats>
          <tig>
            <atl>Cocaine- and opiate-related fatal overdose in New York City, 1990--2000.</atl>
          </tig>
          <aug />
          <sug />
          <pubtype>Academic Journal</pubtype>
          <doctype>Article</doctype>
        </artinfo>
        <language code="eng">English</language>
      </controlInfo>
      <displayInfo>
        <pLink>
          <url>http://survey.hshsl.umaryland.edu/?url=http://search.ebscohost.com/login.aspx?direct=true&amp;db=aph&amp;AN=43830400&amp;site=eds-live</url>
        </pLink>
      </displayInfo>
    </header>
  </rec>
  <rec resultID="3">
    <header shortDbName="cmedm" longDbName="MEDLINE" uiTerm="17334938">
      <controlInfo>
        <bkinfo />
        <jinfo>
          <jtl>Journal Of Urban Health: Bulletin Of The New York Academy Of Medicine</jtl>
          <jtl>J Urban Health</jtl>
          <issn type="Print">1099-3460</issn>
        </jinfo>
        <pubinfo>
          <dt year="2007" month="03" day="01">2007 Mar</dt>
          <vid>84</vid>
          <iid>2</iid>
          <place>United States</place>
        </pubinfo>
        <artinfo>
          <ui type="pmid">17334938</ui>
          <pages>292-306</pages>
          <formats />
          <tig>
            <atl>Observed patterns of illicit opiate overdose deaths in Chicago, 1999-2003.</atl>
          </tig>
          <aug>
            <au>Scott G</au>
            <au>Thomas SD</au>
          <aug>
          <sug>
            <subj type="geographic">Chicago</subj>
            <subj type="major">Drug Overdose mortality</subj>
            <subj type="major">Narcotics poisoning</subj>
            <subj type="major">Opioid-Related Disorders mortality</subj>
            <subj type="minor">Age Factors</subj>
            <subj type="minor">Drug Overdose ethnology</subj>
            <subj type="minor">Opioid-Related Disorders epidemiology</subj>
            </sug>
          <ab>This article explores trends and correlates of Chicago's opiate-related overdose (OD) deaths. We manually examined data from every death certificate filed between 1999 and 2003 to identify all Chicago residents' accidental deaths involving acute intoxication with illicit opiates, OD, or opiate poisoning.</ab>
          <pubtype>Journal Article</pubtype>
        </artinfo>
        <language code="eng">English</language>
      </controlInfo>
      <displayInfo>
        <pLink>
          <url>http://survey.hshsl.umaryland.edu/?url=http://search.ebscohost.com/login.aspx?direct=true&amp;db=cmedm&amp;AN=17334938&amp;site=eds-live</url>
        </pLink>
      </displayInfo>
    </header>
  </rec>
</records>

xsl(以下胡萝卜样本):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


  <xsl:output indent="yes" omit-xml-declaration="no"
       media-type="application/xml" encoding="UTF-8" />

  <xsl:template match="/">
    <searchresult>
      <xsl:apply-templates select="/reords/rec/header" />
    </searchresult>
  </xsl:template>

  <xsl:template match="header">
    <document>
      <title><xsl:value-of select="atl" /></title>
      <snippet>
        <xsl:value-of select="ab" />
      </snippet>
      <url><xsl:value-of select="url" /></url>
    </document>
  </xsl:template>
</xsl:stylesheet>
4

1 回答 1

1

有一些事情发生了,但你真的很接近。

作为@G。Ken Holman 在评论中指出,您的apply-templates. 它应该读作

<xsl:apply-templates select="/records/rec/header" />

然后,您在标题模板中引用的三个元素在您引用它们的位置不存在。下面是每个的正确 XPath。

<xsl:value-of select="controlInfo/artinfo/tig/atl" />
<xsl:value-of select="displayInfo/pLink/url" />
<xsl:value-of select="controlInfo/artinfo/ab" />

或者,如果您不想列出整个路径,您可以使用快捷方式。

<xsl:value-of select="//atl" />
<xsl:value-of select="//url" />
<xsl:value-of select="//ab" />

但这并不总是推荐用于大型文档或元素名称可以重复的文档。

更新路径后,我能够得到以下输出:

<searchresult>
  <document>
    <title>Cocaine- and opiate-related fatal overdose in New York City, 1990-2000.</title>
    <snippet/>
    <url>http://survey.hshsl.umaryland.edu/?url=http://search.ebscohost.com/login.aspx?direct=true&amp;db=aph&amp;AN=43811908&amp;site=eds-live</url>
  </document>
  <document>
    <title>Cocaine- and opiate-related fatal overdose in New York City, 1990--2000.</title>
    <snippet/>
    <url>http://survey.hshsl.umaryland.edu/?url=http://search.ebscohost.com/login.aspx?direct=true&amp;db=aph&amp;AN=43830400&amp;site=eds-live</url>
  </document>
  <document>
    <title>Observed patterns of illicit opiate overdose deaths in Chicago, 1999-2003.</title>
    <snippet>This article explores trends and correlates of Chicago's opiate-related overdose (OD) deaths. We manually examined data from every death certificate filed between 1999 and 2003 to identify all Chicago residents' accidental deaths involving acute intoxication with illicit opiates, OD, or opiate poisoning.</snippet>
    <url>http://survey.hshsl.umaryland.edu/?url=http://search.ebscohost.com/login.aspx?direct=true&amp;db=cmedm&amp;AN=17334938&amp;site=eds-live</url>
  </document>
</searchresult>
于 2013-07-19T15:43:23.417 回答