xslt - XSL Copy Child Element to Parent Attribute -


i'm starting learn xls modify xml below. in particular, copy value of <description> element , replace name attribute of parent <game>.

source xml:

<?xml version="1.0"?> <menu>    <game name="$100000p" index="" image="">       <description>$100,000 pyramid (1988)</description>       <cloneof></cloneof>       <crc></crc>       <manufacturer>box office, inc.</manufacturer>       <year>1988</year>       <genre>strategy</genre>       <rating></rating>       <enabled>yes</enabled>    </game>    <game name="$takes" index="" image="">       <description>high stakes dick francis (1986)</description>       <cloneof></cloneof>       <crc></crc>       <manufacturer>mindscape, inc.</manufacturer>       <year>1986</year>       <genre>adventure</genre>       <rating></rating>       <enabled>yes</enabled>    </game>    <game name="007licen" index="" image="">       <description>007 -  licence kill (1989)</description>       <cloneof></cloneof>       <crc></crc>       <manufacturer>domark ltd.</manufacturer>       <year>1989</year>       <genre>driving</genre>       <rating></rating>       <enabled>yes</enabled>    </game> ... 

desired output:

<?xml version="1.0"?> <menu>    <game name="$100,000 pyramid (1988)" index="" image="">       <description>$100,000 pyramid (1988)</description>       <cloneof></cloneof>       <crc></crc>       <manufacturer>box office, inc.</manufacturer>       <year>1988</year>       <genre>strategy</genre>       <rating></rating>       <enabled>yes</enabled>    </game>    <game name="high stakes dick francis (1986)" index="" image="">       <description>high stakes dick francis (1986)</description>       <cloneof></cloneof>       <crc></crc>       <manufacturer>mindscape, inc.</manufacturer>       <year>1986</year>       <genre>adventure</genre>       <rating></rating>       <enabled>yes</enabled>    </game>    <game name="007 - licence kill (1989)" index="" image="">       <description>007 -  licence kill (1989)</description>       <cloneof></cloneof>       <crc></crc>       <manufacturer>domark ltd.</manufacturer>       <year>1989</year>       <genre>driving</genre>       <rating></rating>       <enabled>yes</enabled>    </game> 

i had tried following xsl doesn't seemed make changes. scratching heads on right now.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">     <xsl:template match="@*|node()">         <xsl:copy>             <xsl:apply-templates select="@*|node()"/>         </xsl:copy>     </xsl:template>       <xsl:template match="game">         <game name="{description}">             <xsl:apply-templates select="@*|node()"/>         </game>     </xsl:template> </xsl:stylesheet> 

the problem approach when do:

<xsl:apply-templates select="@*|node()"/> 

you copy original @name attribute, overwriting new @name attribute have created.

try instead:

<xsl:template match="game">     <game>         <xsl:copy-of select="@*"/>         <xsl:attribute name="name">             <xsl:value-of select="description"/>         </xsl:attribute>         <xsl:apply-templates select="node()"/>     </game> </xsl:template> 

or, if know attributes game have:

<xsl:template match="game">     <game name="{description}" index="{@index}" image="{@image}">         <xsl:apply-templates select="node()"/>     </game> </xsl:template> 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -