all groups > dotnet xml > may 2007 >
You're in the

dotnet xml

group:

is this stylesheet valid?


is this stylesheet valid? Andy Fish
5/31/2007 3:38:45 PM
dotnet xml:
hi,

i'm porting some xsl code from .net 1.1 to 2.0 and I have come across a
transform which works in .net 1.1 and works in mxsml but does not work in
..net 2.0. the stylesheet is this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="foo">
<xsl:param name="param1"/>
<xsl:value-of select="$param1/*"/>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

and the input file is simply

<foo />

so $param1 has no value (empty string? empty node set? I'm not sure)

with .net 2.0 I get an error message Unhandled Exception:
System.Xml.XPath.XPathException: Expression must evaluate to a node-set. I'm
guessing this is because the default parameter value is an empty string.

in the real stylesheet, the parameter (if it is passed in) will be a node
set. so to make sure I don't evaluate an illegal expression, I need to be
able to tell whether the parameter value is an empty string (i.e. default)
or a node set. how can I achieve this?

Andy

Re: is this stylesheet valid? Bjoern Hoehrmann
5/31/2007 5:46:05 PM
* Andy Fish wrote in microsoft.public.dotnet.xml:
[quoted text, click to view]

You could simply check whether $param1 evaluates to a true value, e.g.

<xsl:if text='$param1'>
<xsl:value-of select="$param1/*"/>
</xsl:if>

should do.
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
Re: is this stylesheet valid? Martin Honnen
5/31/2007 5:52:26 PM
[quoted text, click to view]

If I understand <http://www.w3.org/TR/xslt#variable-values> correctly
then the default value is the empty string.

[quoted text, click to view]

To avoid the exception it should suffice to set
<xsl:param name="param1" select="/.."/>
as that way the value is an empty node set and then
<xsl:value-of select="$param1/*"/>
does not give an exception.

--

Martin Honnen --- MVP XML
Re: is this stylesheet valid? David Carlisle
6/1/2007 10:01:11 PM
[quoted text, click to view]


rather than have it default to an empty string, and then having to test
for that to avoid using the variable in path expressions, it's usually
simpler just to make it default to an empty node set, add
<xsl:param name="param1" select="/.."/>

David


--
Re: is this stylesheet valid? Andy Fish
6/5/2007 12:00:00 AM

[quoted text, click to view]

Thanks david, that's pretty much what I ended up doing

unfortunately there were quite a lot of templates with this optional
parameter, although only one of them actually used the parameter. they are
calling each other like this:

<xsl:template match="...">
<xsl:param name="param1"/>
....
<xsl:apply-templates>
<xsl: with-param name="param1" select="$param1"> </xsl:template>
</xsl:apply-templates/>
</xsl:template >

now, if any one of them gets called without a parameter, it passes in the
blank parameter into the ones it calls (rather than not passing in a
parameter - hence the default value does not apply), so this meant I had to
put the default parameter into every one of the templates, not just the one
that needed it.

so as well as some way of telling whether a variable contains a node set,
string or RTF, I would also like to have an option on <xsl:call-template>
and <xsl:apply template> that said:

if this parameter was passed in to me, pass the parameter into the next
template,. otherwise don't pass in the parameter into the next template

Andy

Re: is this stylesheet valid? Pavel Lepin
6/5/2007 12:00:00 AM

Andy Fish <ajfish@blueyonder.co.uk> wrote in
<Vqb9i.31422$Ro3.15173@text.news.blueyonder.co.uk>:
[quoted text, click to view]

[...]

[quoted text, click to view]

Read XSLT2 spec, 10.1.2 and rejoice. (Although I must say I
don't see how precisely that would help you, since from the
description of your predicament it would seem you'd have to
specify the tunneling everywhere anyway.)

--
AddThis Social Bookmark Button