xsl-list
[Top] [All Lists]

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

2012-08-16 10:28:58
Hi,

A slight qualification (really for the archive):

On 8/16/2012 5:35 AM, it was written:
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.

There is an edge case in which this will give an error (in XSLT 2.0) unless you control your data to prevent more than one 'image-size' element appearing somewhere inside the parent of 'media': if more than one is the first 'image-size' child of their respective parents.

This is due to the problematic semantics of "//node" (which expands to "/descendant-or-self::node()/child::node") when used with a positional predicate such as [1]:

..//image-size[1]

expands to

parent::node()/descendant-or-self::node()/child::image-size[position() = 1]

Under 1.0, normalize-space() will take the first element in a node-set argument in any case (no error), so you don't even need the [1].

But it might be good to learn

normalize-space(../descendant::image-size[1])

in case you're ever running under XSLT 2.0 ...

... at least as long as you agree that a little paranoia never hurt ...

Cheers,
Wendell

======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

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