[xsl] xsl:copy, mode and source code

Subject: [xsl] xsl:copy, mode and source code
From: Dave Jarvis <djarvis@xxxxxxxxxxxxxxxx>
Date: Fri, 27 Aug 2004 10:05:44 -0700
Hello,

I must simplify Java/C source code into a high-level XML representation (which is transformed into source code). I've hit a snag and would really appreciate some help.

Consider the following XML, within the "axe" namespace:

  <axe:message type="response">
    <pingResult><axe:value-of select="pingOutput" /></pingResult>
  </axe:message>

The "axe:message" content can contain XML code or text:

  <axe:message type="response">
    <nesting level="1">
      <pingResult><axe:value-of select="pingOutput" /></pingResult>
    </nesting>
  </axe:message>

The relevant XSL templates include:

  <xsl:template match="axe:message[@type='response']">
    AGENT_m.append( <xsl:apply-templates mode="response" /> );
  </xsl:template>

  <xsl:template match="@*|node()" mode="response">
    X<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
  </xsl:template>

  <xsl:template match="*" mode="response">
    Y<xsl:apply-templates />
  </xsl:template>

  <!-- Used by "axe:var", but may be applicable here ... -->
  <xsl:template match="*|text()">
    "<xsl:copy-of select="." />"
    <xsl:if test="position() != last()">+</xsl:if>
  </xsl:template>

The result from debugging the template:

    AGENT_m.append(
    X
    X
    Y
    AGENT_pingOutput );

The desired result:

    AGENT_m.append(
    "<pingResult>" +
    AGENT_pingOutput +
    "</pingResult>" );

I could try an ugly hack:

  <xsl:template match="@*|node()">
    "&lt;<xsl:value-of select="name()" />&gt;"+
    <xsl:apply-templates /> +
    "&lt;/<xsl:value-of select="name()" />&gt;"
  </xsl:template>

I've also tried xsl:copy-of (for a deep copy), and xsl:for-each over all elements, but no matter what I do, I just can't seem to make <pingResult> appear (much less in quotes with concatentation). I've read numerous FAQs, a few mailing lists, and searched Google. After two days of wrestling, I'm stumped; everything I've read leads me to believe that <pingResult> should appear -- but it doesn't!

Is there an elegant solution?

Many thanks for any ideas, or smacks along the upside of my head.

Sincerely,
Dave Jarvis

P.S.
I'm using libxslt from http://xmlsoft.org/XSLT/.

Current Thread