Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When using XSLT xmlns attribute added to output html 1

Status
Not open for further replies.

miraclemaker

Programmer
Oct 16, 2002
127
GB
Hi guys. I'm transforming an XML file using XSLT, I'm defining some namespaces in my XSL file to allow the document to be transformed.

Here's a cut down version of the two files:

XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<allGamesWeb creationDate="2005-06-08" xmlns="allGamesWeb"
    xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] 
    xsi:schemaLocation="allGamesWeb [URL unfurl="true"]http://games-preview.real.com/rnfeedgamesdl/XMLSchemas/allGamesWeb.xsd">[/URL]

    <gameGroupList>
        <gameGroup priority="1" title="Action/Adventure">
            <gameItem gameID="hamsterball" gameType="web" priority="1"/>
            <gameItem gameID="insaniquariumdeluxe" gameType="web" priority="2"/>
            <gameItem gameID="shroomz" gameType="web" priority="3"/>
            <gameItem gameID="atomaders" gameType="web" priority="1"/>
            <gameItem gameID="feedingfrenzy" gameType="web" priority="2"/>
            <gameItem gameID="freakoutgold" gameType="web" priority="3"/>
            <gameItem gameID="atomicpongling" gameType="web" priority="4"/>
            <gameItem gameID="varmintz" gameType="web" priority="5"/>
        </gameGroup>
    </gameGroupList>
</allGamesWeb>

XSL:
Code:
<?xml version='1.0'?>
<xsl:stylesheet 
 xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
 xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL]
 xmlns:game="game"
 xmlns:allGamesWeb="allGamesWeb"
 version="1.0">
	
  <xsl:output method="html"/>

  <xsl:template match="/allGamesWeb:allGamesWeb/allGamesWeb:gameGroupList">
  
    <xsl:for-each select="allGamesWeb:gameGroup">
        <p>
          <xsl:value-of select="allGamesWeb:gameItem/@gameID" />
        </p>        
    </xsl:for-each>
    
  </xsl:template> 
  
</xsl:stylesheet>

Now when I combine the the output is like:
Code:
<p xmlns:allGamesWeb="allGamesWeb" xmlns:game="game" xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">hamsterball</p>[/URL]
<p xmlns:allGamesWeb="allGamesWeb" xmlns:game="game" xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">insaniquariumdeluxe</p>[/URL]
<p xmlns:allGamesWeb="allGamesWeb" xmlns:game="game" xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">shroomz</p>[/URL]
<p xmlns:allGamesWeb="allGamesWeb" xmlns:game="game" xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">atomaders</p>[/URL]
<p xmlns:allGamesWeb="allGamesWeb" xmlns:game="game" xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">feedingfrenzy</p>[/URL]

<p xmlns:allGamesWeb="allGamesWeb" xmlns:game="game" xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">freakoutgold</p>[/URL]
<p xmlns:allGamesWeb="allGamesWeb" xmlns:game="game" xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">atomicpongling</p>[/URL]
<p xmlns:allGamesWeb="allGamesWeb" xmlns:game="game" xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">varmintz</p>[/URL]

As you can see the xmlns attributes of the XSL doc are being added to the html elements of the output for some reason. Can anyone tell mne why this is happening and how to stop it?

Thanks.
 
Use exclude-result-prefixes as an attribute for the xsl:stylesheet node, with a whitespace delimited list of values of namespaces you want to exclude from the output document (#default for default namespace):
Code:
exclude-result-prefixes="#default game allGamesWeb"

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Perfect! Thanks Jon. It seems strange to me that the namespaces were being added to the html tags, which ware in the default namespace. Can you explain to me why this was happening?

Thanks.
 
I think XSL will add the namespaces by default to the top level element of the XML. Your output is not XML, because its not well-formed (XML can only have one top level element). I guess the parser is treating each element as a new XML document, so adds the namespaces to each.

btw, your XML is not valid against the schema.

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top