sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Tim Menninger's Avatar

Passing XML as XsltArgumentList, can't select children


Question posted by: Tim Menninger (Guest) on November 12th, 2005 03:03 AM
I have seen the postings about passing XML into an XSL transformation. The
problem is that I cannot use any XPath navigation in the XSL that won't
generate an error. If I use <xsl:copy-of select="$var"/> then the XML is
output into the html page but this <xsl:copy-of select="$var/NS:Child"/>,
and all other XPath navigation, fails with the error that 'The expression
passed to this method should result in a NodeSet'. The real goal is to use
xsl:for-each to navigate over all $var's children but can't get it to work.

I have seen the samples and they all show I should be able to do this. The
only difference is that the samples never use namespaces and I have a
namspace on my xml.

Tim


5 Answers Posted
Oleg Tkachenko [MVP]'s Avatar
Oleg Tkachenko [MVP] November 12th, 2005 03:03 AM
Guest - n/a Posts
#2: Re: Passing XML as XsltArgumentList, can't select children

Tim Menninger wrote:
[color=blue]
> I have seen the postings about passing XML into an XSL transformation. The
> problem is that I cannot use any XPath navigation in the XSL that won't
> generate an error. If I use <xsl:copy-of select="$var"/> then the XML is
> output into the html page but this <xsl:copy-of select="$var/NS:Child"/>,
> and all other XPath navigation, fails with the error that 'The expression
> passed to this method should result in a NodeSet'. The real goal is to use
> xsl:for-each to navigate over all $var's children but can't get it to work.[/color]

You must be passing XPathNavigator to XSLT?
You have to pass XPathNodeIterator to be able to work with it as with
nodeset in XSLT.
XPathNavigator is RTF in XSLT, XPathNodeIterator - nodeset.
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Tim Menninger's Avatar
Guest - n/a Posts
#3: Re: Passing XML as XsltArgumentList, can't select children

Oleg,

Thanks, I did try this but since my XML has a namespace I could not figure
out how to set the XsltContext for the query. Below is the code I am using.

//props is XmlNode
XPathNavigator nav = props.CreateNavigator();
XsltArgumentList args = new XsltArgumentList();
args.AddParam("properties", "", nav.Select("/FE:Properties"));

The .Select() method throws an exception 'Namespace Manager or XsltContext
needed. This query has a prefix,
variable, or user-defined function'. The XmlNode's XmlDocument has the
correct context set, so props.SelectSingleNode("/FE:Properties") does work.

I have tried to use the second param in AddParam but I do not think this
does what I need.

Tim


"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:%23JGjQ45$DHA.1288@TK2MSFTNGP10.phx.gbl...[color=blue]
> Tim Menninger wrote:
>[color=green]
> > I have seen the postings about passing XML into an XSL transformation.[/color][/color]
The[color=blue][color=green]
> > problem is that I cannot use any XPath navigation in the XSL that won't
> > generate an error. If I use <xsl:copy-of select="$var"/> then the XML is
> > output into the html page but this <xsl:copy-of[/color][/color]
select="$var/NS:Child"/>,[color=blue][color=green]
> > and all other XPath navigation, fails with the error that 'The[/color][/color]
expression[color=blue][color=green]
> > passed to this method should result in a NodeSet'. The real goal is to[/color][/color]
use[color=blue][color=green]
> > xsl:for-each to navigate over all $var's children but can't get it to[/color][/color]
work.[color=blue]
>
> You must be passing XPathNavigator to XSLT?
> You have to pass XPathNodeIterator to be able to work with it as with
> nodeset in XSLT.
> XPathNavigator is RTF in XSLT, XPathNodeIterator - nodeset.
> --
> Oleg Tkachenko [XML MVP, XmlInsider]
> http://blog.tkachenko.com[/color]


Tim Menninger's Avatar
Guest - n/a Posts
#4: Re: Passing XML as XsltArgumentList, can't select children

Oleg thanks for the quick response,

I got it working but not really the way I wanted. If you could answer the
namespace question that would be great. This is what I changed

XPathNavigator nav = props.CreateNavigator();
XsltArgumentList args = new XsltArgumentList();
args.AddParam("properties", "", nav.Select("./*")); //changed this to no
namespace XPath

Then used <xsl:for-each select="$properties"> to output rows

Thanks,

Tim

"Tim Menninger" <tmenninger@forensicsexplorers.com> wrote in message
news:%2397Q9R6$DHA.2180@TK2MSFTNGP09.phx.gbl...[color=blue]
> Oleg,
>
> Thanks, I did try this but since my XML has a namespace I could not figure
> out how to set the XsltContext for the query. Below is the code I am[/color]
using.[color=blue]
>
> //props is XmlNode
> XPathNavigator nav = props.CreateNavigator();
> XsltArgumentList args = new XsltArgumentList();
> args.AddParam("properties", "", nav.Select("/FE:Properties"));
>
> The .Select() method throws an exception 'Namespace Manager or XsltContext
> needed. This query has a prefix,
> variable, or user-defined function'. The XmlNode's XmlDocument has the
> correct context set, so props.SelectSingleNode("/FE:Properties") does[/color]
work.[color=blue]
>
> I have tried to use the second param in AddParam but I do not think this
> does what I need.
>
> Tim
>
>
> "Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
> news:%23JGjQ45$DHA.1288@TK2MSFTNGP10.phx.gbl...[color=green]
> > Tim Menninger wrote:
> >[color=darkred]
> > > I have seen the postings about passing XML into an XSL transformation.[/color][/color]
> The[color=green][color=darkred]
> > > problem is that I cannot use any XPath navigation in the XSL that[/color][/color][/color]
won't[color=blue][color=green][color=darkred]
> > > generate an error. If I use <xsl:copy-of select="$var"/> then the XML[/color][/color][/color]
is[color=blue][color=green][color=darkred]
> > > output into the html page but this <xsl:copy-of[/color][/color]
> select="$var/NS:Child"/>,[color=green][color=darkred]
> > > and all other XPath navigation, fails with the error that 'The[/color][/color]
> expression[color=green][color=darkred]
> > > passed to this method should result in a NodeSet'. The real goal is to[/color][/color]
> use[color=green][color=darkred]
> > > xsl:for-each to navigate over all $var's children but can't get it to[/color][/color]
> work.[color=green]
> >
> > You must be passing XPathNavigator to XSLT?
> > You have to pass XPathNodeIterator to be able to work with it as with
> > nodeset in XSLT.
> > XPathNavigator is RTF in XSLT, XPathNodeIterator - nodeset.
> > --
> > Oleg Tkachenko [XML MVP, XmlInsider]
> > http://blog.tkachenko.com[/color]
>
>[/color]


Oleg Tkachenko [MVP]'s Avatar
Oleg Tkachenko [MVP] November 12th, 2005 03:03 AM
Guest - n/a Posts
#5: Re: Passing XML as XsltArgumentList, can't select children

Tim Menninger wrote:
[color=blue]
> args.AddParam("properties", "", nav.Select("/FE:Properties"));
>
> The .Select() method throws an exception 'Namespace Manager or XsltContext
> needed. This query has a prefix,
> variable, or user-defined function'. The XmlNode's XmlDocument has the
> correct context set, so props.SelectSingleNode("/FE:Properties") does work.[/color]

You have to provide XmlNamespaceManager to Select method to define FE
prefix:

XPathExpression expr = nav.Compile("/FE:Properties");
XmlNamespaceManager mngr = new XmlNamespaceManager(nav.NameTable);
mngr.AddNamespace("FE","FE namespace URI goes here");
expr.SetContext(mngr);
args.AddParam("properties", "", nav.Select(expr));

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Tim Menninger's Avatar
Guest - n/a Posts
#6: Re: Passing XML as XsltArgumentList, can't select children

Oleg,

Thanks, got it working. I had tried this but I was using the prop's
XmlDocument NameTable not the nav's NameTable this must have caused a
problem. I actually have a wrapper class around XmlDocument that manages my
namespaces so I don't have to create a namespace manager for each dom. I was
using my GetNsMgr() accessor to return the XmlDocuments namespace manage and
pass that to SetContext but that didn't work. When you recreate a namespace
manager it works.

Thanks,

Tim


"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:%23vJtBl6$DHA.3536@tk2msftngp13.phx.gbl...[color=blue]
> Tim Menninger wrote:
>[color=green]
> > args.AddParam("properties", "", nav.Select("/FE:Properties"));
> >
> > The .Select() method throws an exception 'Namespace Manager or[/color][/color]
XsltContext[color=blue][color=green]
> > needed. This query has a prefix,
> > variable, or user-defined function'. The XmlNode's XmlDocument has the
> > correct context set, so props.SelectSingleNode("/FE:Properties") does[/color][/color]
work.[color=blue]
>
> You have to provide XmlNamespaceManager to Select method to define FE
> prefix:
>
> XPathExpression expr = nav.Compile("/FE:Properties");
> XmlNamespaceManager mngr = new XmlNamespaceManager(nav.NameTable);
> mngr.AddNamespace("FE","FE namespace URI goes here");
> expr.SetContext(mngr);
> args.AddParam("properties", "", nav.Select(expr));
>
> --
> Oleg Tkachenko [XML MVP, XmlInsider]
> http://blog.tkachenko.com[/color]


 
Not the answer you were looking for? Post your question . . .
196,864 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,864 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors