Re: [xsl] Copying and renaming an element/attribute

Subject: Re: [xsl] Copying and renaming an element/attribute
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sat, 8 Jul 2006 22:48:41 +0530
Hi Mark,
 Please try this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="xml" indent="yes" />

  <xsl:template match="topic">
    <topicref href="{@id}">
      <xsl:apply-templates />
    </topicref>
  </xsl:template>

<xsl:template match="text()" />

</xsl:stylesheet>

Regards,
Mukul

On 7/8/06, Mark Peters <flickrmeister@xxxxxxxxx> wrote:
Hi Folks,

I'm trying to copy a single element ("topic") and attribute ("id") to
a new XML file, discarding all other elements and attributes. I'd also
like to rename the topic element as topicref, and rename the id
attribute as href.


Input XML:


<topic id="unique_id">
       <title>Title</title>
       <body>
               <p>Some text.</p>
       </body>
       <topic id="unique_id">
               <title>Title</title>
               <body>
                       <p>Some text.</p>
               </body>
       </topic>
       <topic id="unique_id">
               <title>Title</title>
               <body>
                       <p>Some text.</p>
               </body>
               <topic id="unique_id">
                       <title>Title</title>
                       <body>
                               <p>Some text.</p>
                       </body>
               </topic>
       </topic>
</topic>



Output XML:

<topicref href="unique_id">
       <topicref href="unique_id"/>
       <topicref href="unique_id">
               <topicref href="unique_id"/>
       </topicref>
</topicref>


I've tried various value-of statements, which result in a simple list of topicref elements. The elements aren't nested. I'm trying to keep the original nesting.

I've also tried xsl:copy (see below), but the output file only
displays the first topic element. From what I've been reading, I think
"topic" as an XPath statement should find all instances of that
element -- although I've tried a few different XPath patterns, without
success.


Stylesheet:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
       <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
       <xsl:template match="node()|@*">
               <xsl:copy>
                       <xsl:apply-templates select="@*"/>
                       <xsl:apply-templates/>
               </xsl:copy>
       </xsl:template>
       <xsl:template match="topic">
               <topicref>
                                       <xsl:attribute name="href">
                       <xsl:for-each select="@id">
                               <xsl:value-of select="."/>
                       </xsl:for-each>
                                        </xsl:attribute>
               </topicref>
       </xsl:template>
</xsl:stylesheet>



Could someone tell me what I'm missing?

Thanks in advance,
Mark

--

Mark Peters
Senior Technical Writer
Saba Software

Current Thread