将非胡萝卜xml转换为胡萝卜xml

时间:2013-07-19 14:26:14

标签: xslt

我是胡萝卜新手,对xsl知之甚少,所以请耐心等待。我的输入文件是来自数据库的xml搜索结果。它不是胡萝卜格式所以根据文档我必须使用xsl文件将其转换为胡萝卜格式。我跟着胡萝卜提供的样本xsl(对于bbc新闻),但它找不到我指定的标题(atl),片段(ab)和链接(url)(得到返回msg“你的查询没有返回任何文件”) 。这是文件,如果有人可以提供帮助我真的很感激。万分感谢。

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>

1 个答案:

答案 0 :(得分:1)

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

作为@G。肯霍尔曼在评论中指出,你的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>