Re: [xsl] XQuery Updates in XSLT

Subject: Re: [xsl] XQuery Updates in XSLT
From: John Snelson <john.snelson@xxxxxxxxxx>
Date: Wed, 29 Oct 2008 20:15:51 +0000
Robert Koberg wrote:
> 
> On Oct 29, 2008, at 3:48 PM, John Snelson wrote:
> 
>> Robert Koberg wrote:
>>>
>>> On Oct 29, 2008, at 3:21 PM, Scott Trenda wrote:
>>>
>>>> I think John is confusing the meaning of "functional" here. Dimitre
>>>> meant "functional" as in "based on functions/templates, like Haskell or
>>>> Scheme (as opposed to imperative)", and perhaps John interpreted
>>>> "functional" to mean "it works, it's not broken".
>>>
>>> I am sure John knows what functional means. I was commenting about the
>>> use of XQuery as opposed to xsl's identity template with some exception
>>> templates. It sounds like he think XQuery is better at this type of
>>> thing.
>>
>> Yes. It's a few lines of easily readable XQuery Update, versus a few
>> complete XSLT templates and an explanation of how the identity transform
>> works.
> 
> Really? Show me how you use typeswitch (or whatever you would like to
> use) to handle the XSL equivalent of exception templates in a few lines.

Problem: In a copy of the original document, rename the "name" attribute
of "product" elements to "id".

XQuery Update solution:

copy $a := doc(...)
modify for $p in $a//product return rename node $p/@name as "id"
return $a

XSLT 2.0 solution:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="product/@name">
  <xsl:attribute name="id">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

The XQuery Update solution clearly matches the problem definition
better, and is shorter. It would be even shorter if you wanted to
perform the update in-place.

John

-- 
John Snelson, Oracle Corporation            http://snelson.org.uk/john
Berkeley DB XML:            http://oracle.com/database/berkeley-db/xml
XQilla:                                  http://xqilla.sourceforge.net

Current Thread