Connecting Tech Pros Worldwide Forums | Help | Site Map

can't get rid of xmlns attribute

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#1: Dec 27 '08
Hi,

I have some problems removing the xmlns attributes from a transformed xml file. I can't completely remove them (so it would validate).

the problematic elements are <div> or the elements pasted by <xsl:copy-of>

if I have no default NS in the XSL file I get the namespace attribute attached to the copied elements (the one from the "plan" prefix because that's default NS of the XML, all prefixed NS are removed).

if I have a default NS in the XSL file, the copied elements are free of NS, but the NS is now attached to the <div> element (because it's the top level element in the result XML).

so, how do I need to change my copy construct to get all elements without NS?

alternatively I have to add a preg_replace() function to my script, if this is not possible with XSL......

the xsl file (the important part):
Expand|Select|Wrap|Line Numbers
  1. <xsl:stylesheet version="1.0"
  2.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3.     xmlns:plan="urn:kbl.plan:current-and-bygone-playlists" // default NS of the XML file
  4.     exclude-result-prefixes="xsl plan">
  5.  
  6. [...]
  7.  
  8. <xsl:template match="/">
  9.   <xsl:element name="div">  // either I have xmlns="..." here ...
  10.     <xsl:attribute name="class">
  11.       <xsl:text>mitte inhalt</xsl:text>
  12.     </xsl:attribute>
  13. [...]
  14.     <xsl:for-each select="$basis/child::plan:thema">
  15.       <xsl:call-template name="themen" />
  16.     </xsl:for-each>
  17.   </xsl:element>
  18. </xsl:template>
  19.  
  20. <xsl:template name="themen">
  21.   <xsl:call-template name="balken" />
  22.   <xsl:for-each select="plan:termin">
  23.     <xsl:call-template name="termine" />
  24.   </xsl:for-each>
  25.   <xsl:if test="plan:xhtml">
  26.     <xsl:copy-of select="plan:xhtml/*" /> // ... or here
  27.   </xsl:if>
  28. </xsl:template>
  29.  

Moderator
 
Join Date: Mar 2006
Posts: 1,103
#2: Jan 5 '09

re: can't get rid of xmlns attribute


Have you tried using a seperate copy template?
Expand|Select|Wrap|Line Numbers
  1. <xsl:template name="copy">
  2.   <xsl:element name="local-name()">
  3.     <xsl:copy-of select="@*"/>
  4.     <xsl:for-each select="*">
  5.       <xsl:call-template name="copy"/>
  6.     </xsl:for-each>
  7.   </xsl:element>
  8. </xsl:template>
  9.  
  10. Change 
  11. <xsl:copy-of select="plan:xhtml/*" />
  12. with
  13. <xsl:for-each select="plan:xhtml/*">
  14.   <xsl:call-template name="copy"/>
  15. </xsl:for-each>
  16.  
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#3: Jan 5 '09

re: can't get rid of xmlns attribute


Quote:

Originally Posted by jkmyoung View Post

Have you tried using a seperate copy template?

admittedly you are much better with such copy problems than I am. I'll try it.

regards
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#4: Jan 8 '09

re: can't get rid of xmlns attribute


tried the template but I got a problem. the XSLT returns with an error (i.e. the XSLTProcessor::transformToXml() method returns false. I could narrow down the problem to the following:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template name="copy">
  2. // if I use this line I get the error
  3.   <xsl:element name="local-name()">
  4.     <xsl:copy-of select="@*"/>
  5.     <xsl:for-each select="*">
  6.       <xsl:call-template name="copy"/>
  7.     </xsl:for-each>
  8.   </xsl:element>
  9. </xsl:template>
I have no idea, why it does so.....
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#5: Jan 8 '09

re: can't get rid of xmlns attribute


finally solved the problem, looked like the name attribute doesn't accept every input. nevertheless, thank you jkmyoung!
Expand|Select|Wrap|Line Numbers
  1. <xsl:template name="copy">
  2.   <xsl:element name="{local-name()}">
  3.     <xsl:copy-of select="@*"/>
  4.     <xsl:value-of select="text()"/>
  5.     <xsl:for-each select="plan:*">
  6.       <xsl:call-template name="copy"/>
  7.     </xsl:for-each>
  8.   </xsl:element>
  9. </xsl:template>
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#6: Jan 8 '09

re: can't get rid of xmlns attribute


Sorry, forgot that attribute was a string, not an xpath. But you figured out the braces so it's ok.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#7: Jan 8 '09

re: can't get rid of xmlns attribute


Quote:

Originally Posted by jkmyoung View Post

But you figured out the braces so it's ok.

it was rather a lucky guess....
Reply