Re: [xsl] elements to attributes

Subject: Re: [xsl] elements to attributes
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 28 Feb 2003 16:23:53 -0500
Hi Harsha,

At 02:23 PM 2/28/2003, you wrote:
and I need my result XML document to have all the attributes in each level
converted to elements at same level
Can there be a generic xsl which looks at all the nodes and does this?

Uh-huh...


There's a template described in the XSL Recommendation (http://www.w3.org/TR/xslt) as an "identity template", because it matches any node and just copies it, then picks its attributes and children to do the same:

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

You need to split this in two: one to handle all nodes but attributes, the other to handle attributes:

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

Except the one for attributes should have instructions to make an element instead:

<xsl:template match="@*">
  <xsl:element name="{name()}">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>

But if you try this out, you really should study up on the processing model (templates and apply-templates, and the default child:: axis) to make sure you understand it.

(Once you understand the technique, you also will know how to extend it, for example to move things around or change names -- or add things.)

The identity template (find it in the list archives under identity template or identity transformation, it's a FAQ too) is also the solution to the other problem you have posted. You may have already gotten a solution, or ask again if you need to and someone will give you a hint.

Cheers,
Wendell


___&&__&_&___&_&__&&&__&_&__&__&&____&&_&___&__&_&&_____&__&__&&_____&_&&_ "Thus I make my own use of the telegraph, without consulting the directors, like the sparrows, which I perceive use it extensively for a perch." -- Thoreau


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



Current Thread