Solved

copy xml create new atributes add text

Posted on 2009-07-06
6
323 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i generate these XML with SimpleXML() 5 75
Problem to picture file 3 74
Download a website to hdd 2 68
XML response optional elements 12 41
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…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to count occurrences of each item in an array.

810 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