Home > Software engineering >  xsl stylesheet won't apply
xsl stylesheet won't apply

Time:01-29

I'm pretty new to XML and XSL/T so i don't really know much about it yet. So i tried to make a tutorial from http://edutechwiki.unige.ch/en/XSLT_Tutorial_-_Basics and at one point i tried making a little xsl stylesheet but it just don't get displayed right. Does someone see where i'm making a mistake?

XLS:

<xsl:template match="student">
    <html> 
        <head> 
            <title> <xsl:value-of select="firstname"/></title>
        </head>
        <body bgcolor="#ffffff">
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>

<xsl:template match="lastname">
    <h1 align="center"> <xsl:value-of select="lastname"/> <xsl:apply-templates/> </h1>
</xsl:template>

XML:

<student>
    <firstname>Tim</firstname>
    <lastname>Spilka</lastname>
    <nickname>Timi</nickname>
</student>

It gets displayed like that in the Browser:

<html>
    <head>
        <META content="text/html; charset=UTF-16" http-equiv="Content-Type">
            <title>Tim</title>
        </META>
    </head>
</html>

The body just doesn't show up

CodePudding user response:

<xsl:template match="lastname">
    <h1 align="center"> 
      <xsl:value-of select="lastname"/> 
      <xsl:apply-templates/> 
    </h1>
</xsl:template>

Within a template rule that matches lastname, the lastname element is the context node (or context item). When you do select="lastname", it means select="child::lastname", that is, select a child of the context node called lastname. Your lastname element doesn't have any children called lastname, so it selects nothing.

You want <xsl:value-of select="."/>

  •  Tags:  
  • Related