Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copying all elements using xsl and renaming specific nodes

Status
Not open for further replies.

sianmace

Programmer
Jun 8, 2004
14
GB
I have a document from Amazon wich is massive and has the same names for elements under different branches of the tree.

ie
<item>
<smallimage>
<height units=""></height>
</smallimage>
<largeimage>
<height units=""></height>
</largeImage>

.net cannot deal with this in a dataset as it needs unique names. so i need to rename the height elements to something like smallheight and largeheight.

as this is only a snippet of the document, i need to copy all of the docuemnt and just rename these specific nodes.

is there any way to say copy everything, but whith these, do this.
??
Please help i have been searching and searchng
 
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
	<xsl:template match="@*|node()">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="/item/smallimage/height">
		<smallheight units="{units}">
			<xsl:value-of select="."/>
		</smallheight>
	</xsl:template>
	<xsl:template match="/item/largeimage/height">
		<largeheight units="{units}">
			<xsl:value-of select="."/>
		</largeheight>
	</xsl:template>
</xsl:stylesheet>
 
I know you can do xsl:copy but this is not what you want...

What you really want to do is create a couple of templates that output the xml with the format you want, using the xml you already have and use apply-templates to structure it.

Something like this (which will not work on its own without development!! I can't just hand you the answer ;):
Code:
<xsl:template select="/">
    <item>
        <xsl:apply-templates select="smallimage"/>
        <xsl:apply-templates select="largeimage"/>
    </items>
</xsl:tempalte>
...

<xsl:template match="smallimage">
<smallimage>
    <small-height>
       <xsl:attribute name="units"><xsl:value-of select="@units"/></xsl:attribute>
    </small-height>
</smallimage>
</xsl:template>

etc..

so basically you are recreating the xml with the stylesheet not copying and replacing nodes.


Hope this helps

Matt

 
If item is not the root node, you'll need to put
Code:
<xsl:template match="//item/smallimage/height">
and
Code:
<xsl:template match="//item/smallimage/height">
 
yes or what jonty said might actually be better because you do not loose attributes that might potentially be in the items or height tags.. :)

 
If you truly need to copy the whole document to dataset, then xsl:copy is what you want. But if you only using some of the nodes, constructing your own cut-down xml would be better.

Jon
 
no, i need all of the rest of the document exactly the same. and just those nodes renamed.
so what do you collectively think is the best way to go about this
 
Right i tried the first option and it worked a treat, if i took a xmlns attribute out.
heres what i have

<ItemSearchResponse xmlns:xsd=" xmlns:xsi="<Items xmlns="<Item>
<ASIN>0747581088</ASIN>
<DetailPageURL><SalesRank>1</SalesRank>
<SmallImage>
<URL><Height Units="mm">75</Height>
<Width>53</Width>
</SmallImage>
<MediumImage>
<URL><Height Units="mm">160</Height>
<Width>113</Width>
</MediumImage>

If i take out the xmlns from items it works, but if its in id doesnt, can you please help, im so near
 
See my reply to this thread:


"Basically, the XSLT engine needs a fully qualified name to reference a node. This means to reference any node in XSLT that uses a namespace, you need to have a refence to that namespace in the XSLT and that namespace must use a prefix whether it refers to the default namespace or not.
 
That was the answer i was looking for.

but i have one more question about it that i dont really understand.

because i have defined it in the stylesheet as
xmlns:amazon="http....."
i now get on those elements that i changed
<LargeHeight xmlns:amazon="http...."

but in the xml its just xmlns="http...." and i would like to keep it that way on the other side.

i understand that the amazon is there because i have said that in the schema, but can i do it without that?

 
Never used this myself, but I'm guessing at something along the lines of:

Code:
<xsl:namespace-alias stylesheet-prefix="amazon" result-prefix="#default"/>

Let me know if that works.

Jon
 
that was great.
How do you know what to search for to find the answers on all these xsl questions.
Its all so foreign to me.
Thank you so much for the help.
At last i can get on with my work
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top