Solved

copy xml create new atributes add text

Posted on 2009-07-06
6
325 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cordova Camera plugin fails 2 167
Excel file not created as expected 7 94
ebay devID, appID, certID, userToken 2 85
TSQL Query Into Specific XML Format 3 33
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 …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 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