Solved

copy xml create new atributes add text

Posted on 2009-07-06
6
321 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
Comment Utility
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
Comment Utility

<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
Comment Utility
Excellent, Thank you Gertone
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
welcome
0
 
LVL 3

Author Comment

by:QuinnDester
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now