[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 16 Aug 2012 11:28:43 -0400


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:apply-templates select="@*" />
     <xsl:attribute name="image-size" select="normalize-space(..//image-size[1])"/>

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]:


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


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

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


Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
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

Current Thread