xsl-list
[Top] [All Lists]

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

2012-08-16 04:35:33
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



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