xsl无法渲染结果

时间:2013-08-13 20:43:58

标签: xslt namespaces

我必须为从数据库导出的xml输出创建一个xsl。 xml中的名称标签有一个前缀bib后跟一个冒号(如bib :),它是在xml中定义的。但我仍然有一个xsl编译器错误说bib:未声明。所以我在xsl中添加了声明。这次错误消失了,但结果是零,我检查了正确的路径。我也试图排除" bib:"声明后的xsl中的前缀,但得到相同的零结果。我是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>

1 个答案:

答案 0 :(得分:1)

前缀为XML元素定义namespace

要使样式表起作用,名称空间声明需要与输入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>