By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,271 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,271 IT Pros & Developers. It's quick & easy.

Passing Parameter to Transform of XML to XML

P: n/a
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
Share this Question
Share on Google+
2 Replies

P: n/a
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

P: n/a
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.