472,094 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,094 software developers and data experts.

Passing Parameter to Transform of XML to XML

The following transformation puzzles me when trying to transform XML to XML.

I get an exception "THE EXPRESSION PASSED TO THIS METHOD SHOULD RESULT IN A
NODESET" at the last line "xmlDoc.Load(xr)" when I run the code.

I previously tried it all without a parameter and hard-coded the value into
the 'apply-templates' statement and the tranform produced

the result that I expected but when I inset the parameter I get the
exception.

I would appreciate it if someone could look over the code and identify which
part of the code causes the fault

I use VB.NET 2003 and .NET 1.1

cheers,
Craig
------------------ regs.xml ------------
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="regs.xslt"?>
<AllCtls>
<Ctl>
<cl>2</cl>
<ol>AAAAAAAAA</ol>
</Ctl>
<Ctl>
<cl>6</cl>
<ol>BBBBBBBBB</ol>
</Ctl>
<Ctl>
<cl>3</cl>
<ol>CCCCCCCCC</ol>
</Ctl>
<Ctl>
<cl>6</cl>
<ol>DDDDDDDDD</ol>
</Ctl>
</AllCtls>
------------ regs.xslt -------------------
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:param name='myFilter' />

<xsl:template match="/">
<AllCtls>
<xsl:apply-templates select='$myFilter' />
</AllCtls>
</xsl:template>

<xsl:template match="Ctl">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>


----------- VB.Net code ----------------
Dim xmlDoc As New XmlDocument
xmlDoc.Load("regs.xml")

Dim xslt As XslTransform = New XslTransform
xslt.Load("regs.xslt")

Dim xslArg As XsltArgumentList = New XsltArgumentList

Dim str As String = "/AllCtls/Ctl[cl='6']"
xslArg.AddParam("myFilter", "", str)

Dim res As XmlResolver
Dim nav As XPathNavigator = xmlDoc.CreateNavigator()

Dim xr As XmlReader = xslt.Transform(nav, xslArg, res)

Dim xmlDoc As New XmlDocument
xmlDoc.Load(xr)
------------------------------------------------------------
Nov 12 '05 #1
2 3916
Craig Petrie wrote:
Dim xmlDoc As New XmlDocument
xmlDoc.Load("regs.xml")

Dim xslt As XslTransform = New XslTransform
xslt.Load("regs.xslt")

Dim xslArg As XsltArgumentList = New XsltArgumentList

Dim str As String = "/AllCtls/Ctl[cl='6']"
xslArg.AddParam("myFilter", "", str)


Hehe, nice one. Can you pass fragment of C# source code to a method
call? I don't think so.
What you are passing here is plain string. There is no such thing as
"string-which-is-actually-XPath-expression" type in XSLT.
Instead select nodes you want to pass and just pass them. Something like

XPathNavigator nav = xmlDoc.CreateNavigator();
xslArg.AddParam("myFilter", "", nav.Select("/AllCtls/Ctl[cl='6']"));

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #2
Many thanks Oleg - much appreciated

I am new to xsl and its good to get advice from experts such as yourself.

I tried your solution and it worked very well - many thanks.

cheers,
Craig
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:OV****************@TK2MSFTNGP09.phx.gbl...
Craig Petrie wrote:
Dim xmlDoc As New XmlDocument
xmlDoc.Load("regs.xml")

Dim xslt As XslTransform = New XslTransform
xslt.Load("regs.xslt")

Dim xslArg As XsltArgumentList = New XsltArgumentList

Dim str As String = "/AllCtls/Ctl[cl='6']"
xslArg.AddParam("myFilter", "", str)


Hehe, nice one. Can you pass fragment of C# source code to a method
call? I don't think so.
What you are passing here is plain string. There is no such thing as
"string-which-is-actually-XPath-expression" type in XSLT.
Instead select nodes you want to pass and just pass them. Something like

XPathNavigator nav = xmlDoc.CreateNavigator();
xslArg.AddParam("myFilter", "", nav.Select("/AllCtls/Ctl[cl='6']"));

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mitch Cunningham | last post: by
1 post views Thread by Daryl | last post: by
1 post views Thread by Steven T. Hatton | last post: by
1 post views Thread by Gary Whittle | last post: by
7 posts views Thread by Harolds | last post: by
3 posts views Thread by sp | last post: by
6 posts views Thread by David Schwartz | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.