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

Re: [xsl] [XPath 3.0] Can anonymous functions return markup?


Subject: Re: [xsl] [XPath 3.0] Can anonymous functions return markup?
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Sun, 16 Dec 2012 15:38:38 -0800

No, there are no standard XPath ways to create a new node.

But, given a function in the context of the XPath evaluator, this is possible:

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:my="my:my" exclude-result-prefixes="my xs">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
   <xsl:sequence select=
   "let $print := function () as element()
                    {
                       my:element('Hello', (), 'World')
                    }
       return ($print())
   "/>
 </xsl:template>

 <xsl:function name="my:element" as="element()">
  <xsl:param name="pName" as="xs:string"/>
  <xsl:param name="pAttributes" as="attribute()*"/>
  <xsl:param name="pText" as="xs:string?"/>

  <xsl:element name="{$pName}">
   <xsl:sequence select="$pAttributes"/>
   <xsl:sequence select="$pText"/>
  </xsl:element>
 </xsl:function>
</xsl:stylesheet>

produces:

<Hello>World</Hello>

Similarly, the function my:element() can be written in XQuery.


Cheers,

Dimitre

On Sun, Dec 16, 2012 at 2:07 PM, Costello, Roger L. <costello@xxxxxxxxx> wrote:
> Hi Folks,
>
> I want to create an anonymous XPath function that returns markup. For example:
>
>          let $print := function ()
>                               {
>                                   <hello>World</hello>
>                               }
>          return ($print())
>
> I tried embedding that XPath within an xsl:sequence element:
>
>       <xsl:sequence select="
>          let $print := function ()
>                               {
>                                   <hello>World</hello>
>                               }
>          return ($print())
>          " />
>
> but that produced this error message:
>
>     The value of attribute "select" associated with an element
>     type "xsl:sequence" must not contain the '<' character.
>
> So I escaped the '<' characters:
>
>       <xsl:sequence select="
>          let $print := function ()
>                               {
>                                   &lt;hello>World&lt;/hello>
>                               }
>          return ($print())
>          " />
>
> and that produced this error message:
>
>     Unexpected token "<" in path expression
>
> Is it possible to create an anonymous function that returns markup?
>
> If yes, how?
>
> /Roger


Current Thread
Keywords