?
Solved

copy xml create new atributes add text

Posted on 2009-07-06
6
Medium Priority
?
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

762 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