SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Sweden
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I get rid of the annoying last line (XML XSL in the same file)

    I tried to put both the XSLT and the XML data in the same file, but I get an annoying last line like this:
    Code:
    Last: Hill
    First: Phil
    Salary: 100000
    Hire Date: 04/23/1999
    
    Last: Herbert
    First: Johnny
    Salary: 95000
    Hire Date: 09/01/1998
    
    Last: Hill
    First: Graham
    Salary: 89000
    Hire Date: 08/20/2000
    
    Last: First: Salary: Hire Date:
    This is the code which produces it:
    Code:
    <?xml version="1.0"?>
    <?xml-stylesheet type='text/xsl' href='test.xml'?>
    <xsl:stylesheet 
    	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    	version="1.0"
    	xmlns:z="MyXMLStuff" 
    	exclude-result-prefixes="z">
    
    	<xsl:output method="html"/>
    
    <z:employees>
    
      <z:employee hireDate="04/23/1999">
        <last>Hill</last>
        <first>Phil</first>
        <salary>100000</salary>
      </z:employee>
    
      <z:employee hireDate="09/01/1998">
        <last>Herbert</last>
        <first>Johnny</first>
        <salary>95000</salary>
      </z:employee>
    
      <z:employee hireDate="08/20/2000">
        <last>Hill</last>
        <first>Graham</first>
        <salary>89000</salary>
      </z:employee>
    
    </z:employees>
    
      <xsl:template match="z:employees">
        <xsl:apply-templates>
          <xsl:sort select="salary" data-type="number" order="descending"/>
        </xsl:apply-templates>
      </xsl:template>
    
      <xsl:template match="z:employee">
      Last:      <xsl:apply-templates select="last"/> <br/>
      First:     <xsl:apply-templates select="first"/> <br/>
      Salary:    <xsl:apply-templates select="salary"/> <br/>
      Hire Date: <xsl:apply-templates select="@hireDate"/> <br/><br/>
        <xsl:text>
      </xsl:text>
      </xsl:template>
    
    
    </xsl:stylesheet>
    How do I get rid of it?
    Last edited by lagu2653; Sep 20, 2008 at 00:51.

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's because you cannot embed XSL and XML into the same file.
    What you see, are the text nodes contained into your file.
    You have to split it in 2, 1 XML that refrences the stylesheet, and the XSL style sheet:

    Edit...
    Ok, I cannot parse the xml/xsl code, because the forum detects a link (I wonder where...) and I don't have enough posts.

    But the only speciality was to add a definition to the namespace on the root xml element <employees> like this: <z:employees xmlns:z="MyXMLStuff">
    This apply the namespace z to the element. Simply define the namespace in the xsl stylesheet using the same URI, and everything will work.
    Matching between namespaces between xml and xsl is done on the URI of the namespace.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •