Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 3rd, 2012, 06:19 AM
Registered User
 
Join Date: Feb 2012
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default Add name to element with XSLT

Hi,

My requirement is that i have to transform this:
Code:
<catalog>
<document name="a">
<product_id>666746837</product_id>
<title>B</title>
<price>15.00</price>
<creator_name>C</creator_name>
<category_L1>D</category_L1>
<availability>102</availability>
</document>
< more documents.....>
</catalog>
into this:
Code:
<add>
<doc>
<field name="product_id">666746837</field>
<field name="title">B</field>
<field name="price">15.00</field>
<field name="creator_name">C</field>
<field name="category_L1">D</field>
<field name="availability">102</field>
</doc>
< more doc....>
</add>
my xslt:
Code:
<xsl:template match="/">
		<xsl:element name="add">
			<xsl:apply-templates select="catalog/document" />
		</xsl:element>
	</xsl:template>

	<xsl:template match="catalog/document">
		<xsl:element name="doc">
			<xsl:apply-templates select="product_id" />
		</xsl:element>
	</xsl:template>
	
	<xsl:template match="product_id">
		<xsl:element name="field" >
			<xsl:value-of select="." />
		</xsl:element>
	</xsl:template>
but my output so far:
Code:
<add><doc><field>666746837</field></doc>
How do i add the 'name' attribute to the field element? The name of the element should be the name of the source xml element;

Hope someone can help.

Maarten
 
Old February 3rd, 2012, 06:30 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

A couple of things I would point out that you are doing the hard way.

1. There is no need for all those <xsl:element> instructions. You can enter xml elements into your output by simply typing them as literals.

2. The hard way to do attributes is to use the <xsl:attribute> instruction.

3. The east way it to use attribute value templates, e.g. field="{xpath}".

So your entire XSLT would look something like this:

Code:
<xsl:template match="catalog">
<add><xsl:apply-templates/></add>
</xsl:template>

<xsl:template match="document">
<doc><xsl:apply-templates/></doc>
</xsl:template>

<xsl:template match="document/*">
<field name="{name()}"><xsl:value-of select="."/></field>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
mroosendaal (February 3rd, 2012)





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT output to HTML - xml element text should look exactly as laid out in xml element DjGogga XSLT 4 July 21st, 2011 05:39 PM
Add commos after the element Nagaraj XSLT 18 April 23rd, 2009 07:51 AM
How to add an attribute to each element bartnowa XSLT 3 November 8th, 2007 12:12 PM
How to add attribute to unbounded element 2BOrNot2B XML 4 January 16th, 2007 03:42 PM
How to Add a new element to Dataset after build Namita Crystal Reports 3 October 4th, 2004 01:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.