Michael Rys

Musings on XML, XQuery and more...

<July 2008>
SuMoTuWeThFrSa
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789


Navigation

Papers

SQL Server XML Whitepapers

Weblogging Links

MS Bloggers

Recommended Books

Other Blogs

Recommended Links

Presentations (Upcoming)

Presentations (Recent)

Subscriptions

News


Upcoming Presentations


TechEd 2007, Orlando, June 4 to June 8, 2007


Books I co-authored



www.flickr.com
This is a Flickr badge showing public photos from Michael Rys. Make your own badge here.
eXTReMe Tracker

Post Categories

Article Categories



MSDN WebCast Demo: CLR UDF: Transforming XML with XSLT

-- CLR UDF: Transforming XML with XSLT
-- Execute every statement in the sample in order
-- (c) 2004, 2005 Microsoft Corp.

-----------------------------
--- Initialization for demo
-----------------------------

use master
use msdn2005
go

-- First step: Compile TransformXML.cs to generate TransformXml.dll

IF EXISTS (SELECT * FROM sys.objects
WHERE name='ApplyXslTransform' AND type='FS')
 
DROP FUNCTION ApplyXslTransform
GO

IF EXISTS (SELECT * FROM sys.assemblies WHERE name='TransformXml')
 
DROP ASSEMBLY TransformXml
go

IF EXISTS (SELECT * FROM sys.tables WHERE name='docs')
 
DROP TABLE docs
GO

-- Create Example Data
CREATE TABLE docs (id INT PRIMARY KEY, xbook XML)
GO

INSERT INTO docs VALUES (1,
N
'<book ISBN=''0-7356-1588-2''>
  <chapter num=''1''>
    <title>Background</title>
  </chapter>
  <chapter num=''5''>
    <title>Epilogue</title>
  </chapter>
</book>'
)
GO

INSERT INTO docs
SELECT 2, xbook.x
FROM OPENROWSET 
         
(BULK 'Insert your path here\xmlfile.xml'
        
, SINGLE_BLOB) AS xbook(x)

-- Create assembly for XSL transformation
create assembly TransformXml
from 'Insert your path here\TransformXml.dll'
with permission_set = UNSAFE
GO

----------------------------------------------------
-- Create functions on assembly
----------------------------------------------------

--- XSL transformation function definition
CREATE FUNCTION ApplyXslTransform(
  
@xData XML,
  
@xslPath nvarchar(1000))
RETURNS XML
WITH
RETURNS NULL ON NULL INPUT
EXTERNAL NAME TransformXml.TransformXml.ApplyXslTransform
GO

-- Table contents
SELECT *
FROM docs
GO

----------------------------------------------
----- Apply XSL transformation to query result
----------------------------------------------
DECLARE @xData XML
DECLARE
@xslPath nvarchar(1000)
SELECT @xData = xbook.query('
 
<alltopics>{
     /book[@ISBN = "0-1896-1899-3"]//chapter[@num >= 2]
 }</alltopics>'
)
FROM docs
WHERE id=2

-- Set the path of the XSL transformation
SET @xslPath = 'Insert your path here\myxsl.xsl'
SELECT dbo.ApplyXslTransform (@xData, @xslPath)
GO

----------------------------------------
-- Apply XSL transform to any XML column
----------------------------------------

-- Apply XSL transformation to full document
SELECT id, dbo.ApplyXslTransform(xbook, 'Insert your path here\myxsl.xsl') AS NEWCOL
FROM docs
GO

-- Apply XSL transformation to first section only
SELECT id, dbo.ApplyXslTransform(xbook.query('//chapter[1]'), 'Insert your path here\myxsl.xsl')
FROM docs
GO

posted on Tuesday, April 12, 2005 1:08 PM by mrys


# Material for my MSDN WebCast &amp;quot;Managing XML Data on the Database with SQL Server 2005 and Visual Studio 2005&amp;quot; @ Tuesday, April 12, 2005 4:43 PM

You can get the WebCast recording and copies of the slides&nbsp;from the MSDN website (registration required)....

mrys

# re: MSDN WebCast Demo: CLR UDF: Transforming XML with XSLT @ Thursday, December 15, 2005 8:41 AM

I've got following error during the exection this transofrmation fucntion, is any idea how to fix this?

A .NET Framework error occurred during execution of user defined routine or aggregate 'SqlXslTransform':
System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
System.Security.SecurityException:
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.Xsl.Xslt.XsltLoader.CreateReader(Uri uri, XmlResolver xmlResolver)
at System.Xml.Xsl.Xslt.XsltLoader.Load(Compiler compiler, Object stylesheet, XmlResolver xmlResolver)
at System.Xml.Xsl.Xslt.Compiler.Compile(Object stylesheet, XmlResolver xmlResolver, QilExpression& qil)
at System.Xml.Xsl.XslCompiledTransform.CompileToQil(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
at System.Xml.Xsl.XslCompiledTransform.Load(String stylesheetUri)
at TransformXml.ApplyXslTransform(SqlXml XmlData, String XslPath)

Evgenios

# re: MSDN WebCast Demo: CLR UDF: Transforming XML with XSLT @ Wednesday, July 05, 2006 9:24 AM

What is the correct way to enable support of xslt's document() function inside SQL Server 2005?
This error makes me crazy:
An error occurred while loading document ''. See InnerException for a complete description of the error. ---> System.Xml.XmlException: Resolving of external URIs was prohibited.

progger




Powered by Dot Net Junkies, by Telligent Systems