xsl-list
[Top] [All Lists]

Node renaming problem

2002-10-09 14:26:49
Hello,

It's been over a year since I last worked with XSL and am having a difficult time remembering the simplest of things. In a current project, I want to add a prefix to a particular node tree such that
<math>
  <apply>
    <power/>
    <ci>x</ci>
    <cn>2</cn>
  </apply>
</math>

becomes

<m:math>
  <m:apply>
    <m:power/>
    <m:ci>x</m:ci>
    <m:cn>2</m:cn>
  </m:apply>
</m:math>

(The subnodes must carry the prefix because that is how the third-party browser plugin is configured to parse them.)

I wrote the following named template, but I think I did it the hard way. Plus my test for text-only nodes is incorrect.

  <xsl:template name="str:prepend-ns">
    <xsl:param name="tags" select="."/>
    <xsl:param name="ns" select="'alt'"/>
    <xsl:for-each select="$tags">
      <xsl:choose>
        <!-- WRONG -->
        <xsl:when test=".=text()">
          <xsl:copy-of select=."/>
        </xsl:when>
        <xsl:otherwise>
<xsl:variable name="tagName" select="concat($ns, ':', local-name(.))"/>
          <xsl:element name="{$tagName}">
            <xsl:call-template name="str:prepend-ns">
              <xsl:with-param name="tags" select="child::node()"/>
              <xsl:with-param name="ns" select="$ns"/>
            </xsl:call-template>
          </xsl:element>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>

This is the result using MSXSL4.0:
<m:math xmlns="">
  <m:apply xmlns="">
    <m:power xmlns="" />
    <ci>x</ci>
    <cn>2</cn>
  </m:apply>
</m:math>

1) How should I properly check to see if the current node is a text()?
2) How do I avoid the unnecessary xmlns="" attributes?
3) Am I overcomplicating everything?


Greg Faron
Integre Technical Publishing Co.



XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>