vbNewbie
vbNewbie

Reputation: 3345

xslt transformation returning blank values

I am just getting into xslt transformation and after going through a tutorial I tried doing some code in visual studio 2008. I tried the following code after linking the xml document I needed as input:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
  <xsl:copy>
    <xsl:element name="entry">
     <xsl:attribute name="published" >
      <xsl:value-of select="entry/published"/>
    </xsl:attribute>
    <xsl:attribute name="title" >
      <xsl:value-of select="title"/>
    </xsl:attribute>
    <xsl:attribute name="summary" >
      <xsl:value-of select="summary"/>
    </xsl:attribute>
     </xsl:element>
   </xsl:copy>
 </xsl:template>

here is a sample of the xml file:

<entry xmlns:gnip="http://www.p.com/schemas/2010" xmlns="http://www.w3.org/2005/Atom">
<id>tag:search,2005:38587000730689536</id>
<published>2011-02-18T13:13:52Z</published>
<updated>2011-02-18T13:13:52Z</updated>
<title>nachopego (J Ignacio Pe&#xF1;a G.) posted a note</title>
<summary type="html">Truculencia: en Nayarit no nada mas
     el engrudo se hace  bolas</summary>
<category term="StatusPosted" label="Status Posted"/>
 <category term="NotePosted" label="Note Posted"/>
 <link rel="alternate" 
       type="text/html"  href="http://nachopego/statuses/38587000730689536"/>

Upvotes: 2

Views: 2449

Answers (2)

Michael Kay
Michael Kay

Reputation: 163458

By the way, this code

     <xsl:element name="entry">
        <xsl:attribute name="published" >
            <xsl:value-of select="x:entry/x:published"/>
        </xsl:attribute>
        <xsl:attribute name="title" >
            <xsl:value-of select="x:title"/>
        </xsl:attribute>
        <xsl:attribute name="summary" >
            <xsl:value-of select="x:summary"/>
        </xsl:attribute>
    </xsl:element>

can be written much more legibly as

<entry published="{x:entry/x:published}" 
       title="{x:title}" 
       summary="{x:summary}"/>

Upvotes: 1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

This is the Most FAQ in the xpath and xslt tags: XPath expressions against an XML document with a default namespace.

Just search for "xpath default namespace" and you will find many good answers.

Solution:

  1. Add a namespace declaration in the XSLT stylesheet for the default namespace of the XML document. Use the prefix (say) "x:" in this declaration.

  2. In any XPath expression that references an element by name, prefix every name with the "x:" prefix.

Your code becomes:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://www.w3.org/2005/Atom" >
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <xsl:copy>
            <xsl:element name="entry">
                <xsl:attribute name="published" >
                    <xsl:value-of select="x:entry/x:published"/>
                </xsl:attribute>
                <xsl:attribute name="title" >
                    <xsl:value-of select="x:title"/>
                </xsl:attribute>
                <xsl:attribute name="summary" >
                    <xsl:value-of select="x:summary"/>
                </xsl:attribute>
            </xsl:element>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

and now it produces some output.

Explanation:

Xpath always treats unprefixed names as belonging to "no namespace". Thus if an expression contains the name someName, XPath tries to find an element with the name someName that belongs to "no namespace" and fails, because all the elements in the document belong to its non-empty default namespace.

The solution (as the one above) is to refer to the names using a prefixed name, where the prefix is bound exactly to the default namespace of the XML document.

Upvotes: 3

Related Questions