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

Subject: Re: [xsl] moving an element and then converting it to an attribute
From: Michael Müller-Hillebrand <mmh@xxxxxxxxx>
Date: Thu, 16 Aug 2012 11:35:08 +0200
Hi Jonina,

in a template for <media> just pull in the value of <image-size> like this:

<xsl:template match="media">
  <xsl:copy>
    <xsl:apply-templates select="@*" />
    <xsl:attribute name="image-size"
select="normalize-space(..//image-size[1])"/>
  </xsl:copy>
</xsl:template>

This assumes that <media> is an empty element.

As <image-size> is not a sibling of <media> I go to the parent element (..)
and search all descendants (//) for <image-size>. In case there are multiple
<image-size> present I just use the first instance as otherwise the
normalize-space() function would create a run-time error.

- Michael


Am 15.08.2012 um 20:13 schrieb Jonina Dames:

> 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.
>
> Thanks,
> Joni
>

--
DOCUFY GmbH
Michael M|ller-Hillebrand | Senior Consultant
www.docufy.de

Current Thread