|
Subject: Re: [xsl] moving an element and then converting it to an attribute From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Wed, 15 Aug 2012 15:33:05 -0400 |
Hi, I'm pretty new to XSLT and I'm having some trouble with a project.
I have the following XML:
<media.block id="fun1"> <caption> <para> <txt> <image-size> spread </image-size> </txt> </para> </caption> <media filename="1234567"/> </media.block>
What I want is to turn the image-size node into an attribute of the media element, so it looks like this:
<media.block id="fun1"> <caption> <para> <txt> <image-size> spread </image-size> </txt> </para> </caption> <media filename="0801_75750-rm" image-size="spread"/> </media.block>
But I keep getting stuck. I was thinking I might need to move the image-size node somewhere before I could turn it into an attribute of the media element?
Can anyone help me with this? I'm just using version 1, not 2.
~/t/ftemp $ cat jonina.xml
<?xml version="1.0" encoding="UTF-8"?>
<media.block id="fun1">
<caption>
<para>
<txt>
<image-size>
spread
</image-size>
</txt>
</para>
</caption>
<media filename="1234567"/>
</media.block>
~/t/ftemp $
~/t/ftemp $
~/t/ftemp $ xslt jonina.xml jonina.xsl
<?xml version="1.0" encoding="utf-8"?><media.block id="fun1">
<caption>
<para>
<txt>
<image-size>
spread
</image-size>
</txt>
</para>
</caption>
<media filename="1234567" image-size="spread"/>
</media.block>~/t/ftemp $
~/t/ftemp $
~/t/ftemp $
~/t/ftemp $ cat jonina.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"><xsl:template match="media">
<!--preserve the element-->
<xsl:copy>
<!--preserve the existing attributes-->
<xsl:copy-of select="@*"/>
<!--add the desired attributes-->
<xsl:attribute name="image-size">
<xsl:value-of select="normalize-space(../caption/para/txt/image-size)"/>
</xsl:attribute>
<!--handle any children that may exist; they probably don't for this-->
<xsl:apply-templates/>
</xsl:copy>
</xsl:template><xsl:template match="@*|node()"><!--identity for all other nodes-->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
-- Public XSLT, XSL-FO, UBL and code list classes in Europe -- Oct 2012 Contact us for world-wide XML consulting and instructor-led training Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Google+ profile: https://plus.google.com/116832879756988317389/about Legal business disclaimers: http://www.CraneSoftwrights.com/legal
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| [xsl] moving an element and then co, Jonina Dames | Thread | Re: [xsl] moving an element and the, Wendell Piez |
| [xsl] moving an element and then co, Jonina Dames | Date | Re: [xsl] moving an element and the, Wendell Piez |
| Month |