can't get rid of xmlns attribute

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #1

    can't get rid of xmlns attribute

    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):
    Code:
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:plan="urn:kbl.plan:current-and-bygone-playlists" // default NS of the XML file
        exclude-result-prefixes="xsl plan">
    
    [...]
    
    <xsl:template match="/">
      <xsl:element name="div">  // either I have xmlns="..." here ...
        <xsl:attribute name="class">
          <xsl:text>mitte inhalt</xsl:text>
        </xsl:attribute>
    [...]
        <xsl:for-each select="$basis/child::plan:thema">
          <xsl:call-template name="themen" />
        </xsl:for-each>
      </xsl:element>
    </xsl:template>
    
    <xsl:template name="themen">
      <xsl:call-template name="balken" />
      <xsl:for-each select="plan:termin">
        <xsl:call-template name="termine" />
      </xsl:for-each>
      <xsl:if test="plan:xhtml">
        <xsl:copy-of select="plan:xhtml/*" /> // ... or here
      </xsl:if>
    </xsl:template>
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Have you tried using a seperate copy template?
    [code=xml]
    <xsl:template name="copy">
    <xsl:element name="local-name()">
    <xsl:copy-of select="@*"/>
    <xsl:for-each select="*">
    <xsl:call-template name="copy"/>
    </xsl:for-each>
    </xsl:element>
    </xsl:template>

    Change
    <xsl:copy-of select="plan:xh tml/*" />
    with
    <xsl:for-each select="plan:xh tml/*">
    <xsl:call-template name="copy"/>
    </xsl:for-each>
    [/code]

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      Originally posted by jkmyoung
      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

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        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:
        Code:
        <xsl:template name="copy">
        // if I use this line I get the error
          [B]<xsl:element name="local-name()">[/B]
            <xsl:copy-of select="@*"/>
            <xsl:for-each select="*">
              <xsl:call-template name="copy"/>
            </xsl:for-each>
         [B] </xsl:element>[/B]
        </xsl:template>
        I have no idea, why it does so.....

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          finally solved the problem, looked like the name attribute doesn't accept every input. nevertheless, thank you jkmyoung!
          [CODE=xml]<xsl:template name="copy">
          <xsl:element name="{local-name()}">
          <xsl:copy-of select="@*"/>
          <xsl:value-of select="text()"/>
          <xsl:for-each select="plan:*" >
          <xsl:call-template name="copy"/>
          </xsl:for-each>
          </xsl:element>
          </xsl:template>[/CODE]

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            Sorry, forgot that attribute was a string, not an xpath. But you figured out the braces so it's ok.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Originally posted by jkmyoung
              But you figured out the braces so it's ok.
              it was rather a lucky guess....

              Comment

              Working...