[xsl] Copying and renaming an element/attribute

Subject: [xsl] Copying and renaming an element/attribute
From: "Mark Peters" <flickrmeister@xxxxxxxxx>
Date: Sat, 8 Jul 2006 12:32:11 -0400
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