xsl-list
[Top] [All Lists]

Re: [xsl] moving an element and then converting it to an attribute

2012-08-15 14:41:20
At 2012-08-15 14:13 -0400, Jonina Dames wrote:
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?

You are thinking procedurally and not declaratively. You need to copy your input to the output and indicate when creating each point of your output the things you need from the input.

Can anyone help me with this? I'm just using version 1, not 2.

I hope the example below helps.

Check out some of the diagrams in chapter 1 of the free XSLT book I have made available with its complete content through the links found at:

  http://www.CraneSoftwrights.com/training/#ptux

. . . . . . . . . . Ken

~/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>

</xsl:stylesheet>~/t/ftemp $


--
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(_at_)CraneSoftwrights(_dot_)com
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--