0

我必须为从数据库导出的 xml 输出制作一个 xsl。xml 中的名称标签有一个前缀 bib 后跟一个冒号(如 bib:),它是在 xml 中定义的。但是我仍然收到一个 xsl 编译器错误,说 bib: is not declared。所以我在 xsl 中添加了声明。这次错误消失了,但结果为零,我检查了正确的路径。我还尝试在声明后排除 xsl 中的“bib:”前缀,但得到相同的零结果。我是 xsl 的新手,所以我不知道这里有什么问题。这些是我的文件。非常感谢。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<embasexmllist>
<cards items="1">
  <bib:card items="0" xmlns:bib="http://elsevier.co.uk/namespaces/2001/bibliotek">-         
    <bib:cardfields>
      <bib:Fulltext>
        <bib:DOI>10.1371/journal.pone.0068303</bib:DOI>
      </bib:Fulltext>
      <bib:Title>Mesothelin Virus-Like Particle Immunization Controls Pancreatic Cancer Growth through CD8+ T Cell Induction and Reduction in the Frequency of CD4+foxp3+ICOS- Regulatory T Cells
      </bib:Title>
    </bib:cardfields>
  </bib:card>
</cards>
</embasexmllist>

XSL

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


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


  <xsl:template match="/">
    <searchresult>
      <xsl:apply-templates 
        select="/embasexmllist/cards/bib:card/bib:cardfields" />
    </searchresult>
  </xsl:template>

  <xsl:template match="bib:cardfields">
    <document>
      <title><xsl:value-of select="bib:Title" /></title>
      <snippet>
        <xsl:value-of select="bib:Title" />
      </snippet>
      <url>
        <xsl:variable name="doi" select="bib:Fulltext/bib:DOI"/>
        <xsl:value-of 
          select="concat('http://dx.doi.org/', $doi)" />
      </url>
    </document>
  </xsl:template>
</xsl:stylesheet>
4

1 回答 1

1

前缀定义 XML 元素的名称空间

为了使您的样式表正常工作,名称空间声明需要与输入 XML 中的内容相匹配。代替

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:bib="http://www.bib.com/xml">

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:bib="http://elsevier.co.uk/namespaces/2001/bibliotek">

这会产生以下输出 XML:

<?xml version="1.0" encoding="utf-8"?>
<searchresult xmlns:bib="http://elsevier.co.uk/namespaces/2001/bibliotek">
  <document>
    <title>
          Mesothelin Virus-Like Particle Immunization Controls Pancreatic Cancer Growth through CD8+ T Cell Induction and Reduction in the Frequency of CD4+foxp3+ICOS- Regulatory T Cells
        </title>
    <snippet>
      Mesothelin Virus-Like Particle Immunization Controls Pancreatic Cancer Growth through CD8+ T Cell Induction and Reduction in the Frequency of CD4+foxp3+ICOS- Regulatory T Cells
        </snippet>
    <url>http://dx.doi.org/10.1371/journal.pone.0068303</url>
  </document>
</searchresult>
于 2013-08-13T20:53:36.833 回答