SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,037
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Looking for XSLT loop that doesn't produce xmlns="" attribute.

    How's that for a title?

    This non-critical problem has been driving me a bit nuts ever since I started using XML/XSLT for the creation of dynamic sites.

    I am trying to produce valid XHTML Transitional sites dynamically, but whenever I run a loop in my XSLT, it generates xmlns="" within certain elements like list items resulting in <li id="some value" xmlns="">some value</li>.

    I would like to see: <li id="some value">some value</li>

    Has anyone else had this experience and/or figured out how to be rid of it.

    The following is some sample XML/XSL that you can use if you feel so inclined.

    Cheers and thanks,
    Andrew

    XML
    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
           <?xml-stylesheet type="text/xsl" href="test.xsl"?>
           <SITEMAP>
        	   <!-- START PRIMARY NAVIGATION -->
        	   <CATEGORY TITLE="Primary" ID="primaryNavigation">
        		   <PAGE ID="1" TITLE="Homepage" HYPERTEXT="Home" />
        		   <PAGE ID="2" TITLE="About Us" HYPERTEXT="About" />
        		   <PAGE ID="3" TITLE="Contact" HYPERTEXT="Contact" />
        		   <PAGE ID="4" TITLE="Articles" HYPERTEXT="Articles" />
        		   <PAGE ID="5" TITLE="Tools" HYPERTEXT="Tools" />
        		   <PAGE ID="6" TITLE="Links" HYPERTEXT="Links" />
        	   </CATEGORY>
        	   <!-- END PRIMARY NAVIGATION -->
           </SITEMAP>
    XSL
    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
           
           <!-- Stylesheet Specifications Begin-->
           <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
           
     <xsl:output omit-xml-declaration="yes" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
           
           <xsl:param name="param3" /> <!--baseURL-->
           <xsl:param name="param4" /> <!--pageURL-->
           
           <xsl:template match="/">
           <html xmlns="http://www.w3.org/1999/xhtml">
           <head>
           <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
           <title>Test Page</title>
           </head>
           <body>
           <!-- Primary Navigation Items -->
        	 <div id="navcontainer">
        	   <ul id="navlist">
        		 <xsl:call-template name="primary"/>
        	   </ul>
        	 </div>
           <!-- End Primary Navigation -->
           </body>
           </html>
           </xsl:template>
           
           <xsl:template name="primary">
           <xsl:for-each select="SITEMAP/CATEGORY/PAGE"> 
        	 <xsl:element name="li">
        	   <xsl:attribute name="id"><xsl:value-of select="@HYPERTEXT" /></xsl:attribute>
        		 <xsl:element name="a">
     			 <xsl:attribute name="href">#</xsl:attribute>
        			 <xsl:attribute name="title">
        			   <xsl:value-of select="@TITLE"/>
        			 </xsl:attribute>
        			 <xsl:value-of select="@HYPERTEXT"/>
        		 </xsl:element>
        	   </xsl:element>
           </xsl:for-each>   
           </xsl:template>
           
           </xsl:stylesheet>
           <!-- Stylesheet Specifications End-->
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Can you show an example of what your XSL file outputs?

    Also, have you tried using the exclude-result-prefixes attribute on your stylesheet? Example:
    HTML Code:
     <xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="html">
    <!--this exclude value excludes the html namespace (xmlns:html) from showing up in the result document.-->
    ...

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,037
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Hey Vinnie,

    Thanks for the reply. The output is as follows:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
     <title>Test Page</title>
     </head>
     <body>
     <div id="navcontainer">
     <ul id="navlist">
     <li id="Home" xmlns="">
     <a href="#" title="Homepage">Home</a>
     </li>
     <li id="About" xmlns="">
     <a href="#" title="About Us">About</a>
     </li>
     <li id="Contact" xmlns="">
     <a href="#" title="Contact">Contact</a>
     </li>
     <li id="Articles" xmlns="">
     <a href="#" title="Articles">Articles</a>
     </li>
     <li id="Tools" xmlns="">
     <a href="#" title="Tools">Tools</a>
     </li>
     <li id="Links" xmlns="">
     <a href="#" title="Links">Links</a>
     </li>
     </ul>
     </div>
     </body>
     </html>
    As you can see each <li> item has an xmlns="" attribute. I changed my stylesheet tag as indicated in your post to:
    <xsl:stylesheet version="1.0"
    xmlnssl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="html">

    but it resulted in an error:
    ERROR: Description: Reference to undeclared namespace prefix: 'html'.

    Any other tips would be great.

    Thanks,
    Andrew
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I think the problem is that you're using xsl:element rather than the literal element. Change this:
    HTML Code:
    <xsl:element name="li">
        	   <xsl:attribute name="id"><xsl:value-of select="@HYPERTEXT" /></xsl:attribute>
        		 <xsl:element name="a">
     			 <xsl:attribute name="href">#</xsl:attribute>
        			 <xsl:attribute name="title">
        			   <xsl:value-of select="@TITLE"/>
        			 </xsl:attribute>
        			 <xsl:value-of select="@HYPERTEXT"/>
        		 </xsl:element>
        	   </xsl:element>
    To this:
    HTML Code:
    <li>
        	   <xsl:attribute name="id"><xsl:value-of select="@HYPERTEXT" /></xsl:attribute>
        		 <a>
     			 <xsl:attribute name="href">#</xsl:attribute>
        			 <xsl:attribute name="title">
        			   <xsl:value-of select="@TITLE"/>
        			 </xsl:attribute>
        			 <xsl:value-of select="@HYPERTEXT"/>
        		 </a>
        	  </li>
    See if that helps

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,037
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Thanks Vinnie,
    I was hoping that was it but....
    Sadly I'm getting the same results I was getting with the xsl:element. I just checked with both MSXMLDOM (ASP) and Cooktop 2.5 and the results are that same.

    This is a strange one isn't it?

    Thanks for the advice,
    Andrew
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Ireland
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,
    had the same problem recently, really simple the xml namespace is declared in the <xsl: output> tag and the <html> tag. take the xmlns attribute out of the html tag and it should be fine

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,037
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Hi rich_gib,

    That definitely removed the empty xmlns attribute but... In order for the page to validate as xhtml the <html> tag has to contain the namespace.

    It's a good riddle but there must be a way to do this.

    Thanks... That almost gets me where I want to go.

    Cheers,
    Andrew
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Ireland
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats strange, the xslt engine for php5 generated the namespace automatically in the html tag when i removed it

    ie. this
    Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    	<xsl:output omit-xml-declaration="yes"
    				doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    				doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
    		<html>
    is transformed to
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,037
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Ok,

    I did a little bit more messing around with the processors to see if I can replicate what you're seeing in PHP5.

    I'm porting my app to work on .NET, ASP and PHP4 to begin with and them maybe later to others. I don't have PHP5 loaded so I couldn't check but here's what I found.

    As you pointed out (rich_gib), removing xmlns="http://www.w3.org/1999/xhtml" from the <html> tag fixes the issue of having xmlns="" appearing throughout the generated code. However, in most cases I still end up with invalid markup because xmlns="http://www.w3.org/1999/xhtml" is not put back into the <html> tag during XSLT processing.

    I've tried processing with MSXML, MSXML 3.0, MSXML 4.0 and .NET and in all cases xmlns="http://www.w3.org/1999/xhtml" is not added back to the <html> tag.

    On the PHP4 front it's a little more interesting. I found that if I use DOMXML (experimental in PHP4) ie:
    PHP Code:
       $xmldoc domxml_open_file("test.xml");
        
    $xsldoc domxml_xslt_stylesheet_file ("test.xsl");
        
    $result $xsldoc->process($xmldoc);
        print 
    $result->dump_mem(); 
    xmlns="http://www.w3.org/1999/xhtml" is added back to the <html> tag

    However, using PHP_XSLT ie:
    PHP Code:
    $xsltproc xslt_create();  
           
    $html xslt_process($xsltproc'arg:/_xml'$xsl_fileNULL$arguments); 
    Doesn't add it back.

    So... I don't think I can rely on the processor to add it back. Any thoughts about removing it from the stylesheet and then adding it back through XSL during the transformation?

    I haven't given it much thought but it occured to me as I was writing this that it may be possible.

    Thanks,
    Andrew
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development


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
  •