user2599757
user2599757

Reputation: 5

xsl not rendering results

I have to make an xsl for an xml output exported from a database. The name tags in xml have a prefix bib followed by a colon (as in bib:), and it was defined in xml. But I still got an xsl compiler error saying bib: is not declared. So I added the declaration in xsl. this time the error went away, but the result comes out zero, and I checked the path that is correct. I also tried to exclude "bib:" prefix in xsl after declaration but got the same zero result. I am new to xsl so I don't know what is wrong here. These are my files. Thanks very much.

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>

Upvotes: 0

Views: 76

Answers (1)

Ben L
Ben L

Reputation: 1312

The prefix defines a namespace for the XML elements.

For your stylesheet to work, the namespace declaration needs to match that in your input XML. Replace

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

with

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

This results in the following output 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>

Upvotes: 1

Related Questions