[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: moving an element and then converting it to an at

Subject: Re: 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
Re:  moving an element and then converting it to an  at
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@xxxxxxxxxxxxxxxxxxxx Google+ profile: https://plus.google.com/116832879756988317389/about Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.