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:46:59 -0800
Now, this is a pure XPath way :)   :

<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()
                    {
                       parse-xml('&lt;hello>World&lt;/hello>')/*
                    }
       return ($print())
   "/>
 </xsl:template>
</xsl:stylesheet>


On Sun, Dec 16, 2012 at 3:38 PM, Dimitre Novatchev <dnovatchev@xxxxxxxxx> wrote:
> 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
>>



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

Current Thread