xsl copy xml but move 1 field value to another

Here should go questions about transforming XML with XSLT and FOP.
eakinpeter
Posts: 1
Joined: Mon Jun 25, 2007 3:38 pm

xsl copy xml but move 1 field value to another

Post by eakinpeter »

Hi guys

I am new to xsl and have what could be quite a simple problem. I am
trying to take an xml file and copy the entire file, i then want to
take a value out of 1 area in the file and put it in another area so
below is an example of what i'm trying to do:
Go from this xml

<startrequest>
<userdetails>
<username>
test username
</username>
<userdetails>
<furtherdetails>
.....................
.....................
</furtherdetails>

<requestdetails>
<username>
</username
</requestdetails>
</startrequest>

to this xml, notice i have moved the username from the userdetails tag
to the requestdetails tag, I am not aware of the structure of the xml
at runtime, all i know is that the requestdetails tag will be present
and the userdetails tag will be present but everything else can
change..

<startrequest>
<userdetails>
<username>

</username>
<userdetails>
<furtherdetails>
.....................
.....................
</furtherdetails>

<requestdetails>
<username>
test username
</username
</requestdetails>
</startrequest>

Can anyone help me with this, is someone able to provide me with the
xsl needed?

Thanks
Peter
jkmyoung
Posts: 89
Joined: Mon Mar 06, 2006 10:13 pm

Post by jkmyoung »

You can create 2 templates for the specified username nodes like so:

<xsl:template match="userdetails/username">
<xsl:copy/> <!-- remove inside text -->
</xsl:template>

<xsl:template match="requestdetails/username">
<xsl:copy>
<xsl:value-of select="//userdetails/username"/> <!-- grab the new value -->
</xsl:copy>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
Post Reply