Solved

copy xml create new atributes add text

Posted on 2009-07-06
6
324 Views
Last Modified: 2013-11-18
I need to copy an xml and all its attributes but add in an extra attribute containing additional text plus 2 existing attributes

like this

Existing XML
<CategorySearch format="Tree" id="601" >
    <Category catalogs="false" id="601" name="Home Page" refinements="false">
        <Category catalogs="false" id="100334923" name="Baby &amp; Nursery" refinements="false">
            <Category catalogs="false" id="100450223" name="Baby Travel &amp; Carriers" refinements="false">
                <Category catalogs="false" id="100466023" name="Baby Bouncers, Swings &amp; Walkers" refinements="true"/>
                <Category catalogs="false" id="100450423" name="Baby Car Seats" refinements="true"/>

I want to add in 1 extra attributes for each containing text and 2 of the existing values

Url="?catid=<xsl:value-of  select="@id">&amp;title=<xsl:value-of select="@name">"

i have the following code that does the copy, but i cant seem to be able to add in extra attributes using it

Thank You


<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent = "yes"/>
	<xsl:template match="/">
		<xsl:apply-templates select="*"/>
	</xsl:template>
	
	<xsl:template match="*">
		<xsl:copy>
			<xsl:apply-templates select="@* | * | text()"/>
		</xsl:copy>
    </xsl:template>
	
	<xsl:template match="text() | @*">
		<xsl:copy/>
    </xsl:template>
 
</xsl:stylesheet>

Open in new window

0
Comment
Question by:QuinnDester
  • 3
  • 3
6 Comments
 
LVL 3

Author Comment

by:QuinnDester
ID: 24788321
Sorry forgot to mention i also need to change the name of @name to @title for use in treeview using web.sitemap

Thanks
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 24788464

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent = "yes"/>
    <xsl:template match="/">
        <xsl:apply-templates select="*"/>
    </xsl:template>
    
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*[not(name() = 'name')]"/>
            <xsl:attribute name="title"><xsl:value-of select="@name"/></xsl:attribute>
            <xsl:attribute name="url">
                <xsl:text>?catid=</xsl:text>
                <xsl:value-of select="@id"/>
                <xsl:text>&amp;amp;title=</xsl:text>
                <xsl:value-of select="@name"/>
            </xsl:attribute>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="text()">
        <xsl:copy/>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 
LVL 3

Author Closing Comment

by:QuinnDester
ID: 31600312
Excellent, Thank you Gertone
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24789180
welcome
0
 
LVL 3

Author Comment

by:QuinnDester
ID: 24790485
Gertone: sorry about this, the URL's created in this have got spaces in, as they are built up from the category name, this doesnt seem to stop them from working, but it does stop the treeview from remembering its state.

is there a way of replacing the spaces with underscores or dashes or +  in just the url attribute using xsl, or any other way you know about
Thank you
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24791676
instead of this
 <xsl:value-of select="@name"/>
you can do this
 <xsl:value-of select="translate(normalize-space(@name), ' ', '_')"/>
and all spaces will be '_'
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XML filtering Windows Event Viewer 10 63
Convert XML to excel12book 5 33
tutorial for ebay api 3 55
Show tab when enter div and change underline color 3 64
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question