Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Expression must evaluate to a node-set.

189 views
Skip to first unread message

BradR

unread,
Apr 26, 2007, 8:24:08 PM4/26/07
to
The following .xslt works on this .xml in XMLSpy, but when I try to
use them in my ASP.NET page I get an error: Expression must evaluate
to a node-set. Any help would be greatly appreciated.

<!-- asp.net -->

<div id="services">
<asp:xml runat="server" ID="getServices"
DocumentSource="XML/packages.xml"
TransformSource="XML/
bgc_select_services.xslt" />
</div>

<!-- xml -->

<packages>
<package>
<id>1</id>
<name>No Package</name>
<default>true</default>
<sort_order>1</sort_order>
<kroll_key>NONE</kroll_key>
<services>
<service>
<id>1</id>
<count>0</count>
</service>
<service>
<id>2</id>
<count>0</count>
</service>
<service>
<id>3</id>
<count>0</count>
</service>
<service>
<id>4</id>
<count>0</count>
</service>
<service>
<id>5</id>
<count>0</count>
</service>
<service>
<id>6</id>
<count>0</count>
</service>
<service>
<id>7</id>
<count>0</count>
</service>
<service>
<id>8</id>
<count>0</count>
</service>
</services>
</package>
<package>
<id>4</id>
<name>Candidate Report - Edu, Emp, Criminal</name>
<default>false</default>
<sort_order>4</sort_order>
<kroll_key>CANDIDATE</kroll_key>
<services>
<service>
<id>1</id>
<count>0</count>
</service>
<service>
<id>2</id>
<count>2</count>
</service>
<service>
<id>3</id>
<count>0</count>
</service>
<service>
<id>4</id>
<count>3</count>
</service>
<service>
<id>5</id>
<count>9</count>
</service>
<service>
<id>6</id>
<count>0</count>
</service>
<service>
<id>7</id>
<count>0</count>
</service>
<service>
<id>8</id>
<count>1</count>
</service>
</services>
</package>
</packages>


<!-- xslt -->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
<xsl:param name="package_id">4</xsl:param>
<xsl:output method="html" indent="yes" version="4.0"/>
<xsl:variable name="services">
<services>
<service>
<id>1</id>
<name>County Civil Record Search</name>
<kroll_key>CIVIL</kroll_key>
<sort_order>1</sort_order>
</service>
<service>
<id>2</id>
<name>County Criminal Record Search</name>
<kroll_key>CRIMINAL_RECORD</kroll_key>
<sort_order>2</sort_order>
</service>
<service>
<id>3</id>
<name>Statewide Criminal Record Search</name>
<kroll_key>CRIMINAL_STATE</kroll_key>
<sort_order>3</sort_order>
</service>
<service>
<id>4</id>
<name>Education Verification</name>
<kroll_key>EDUCATION</kroll_key>
<sort_order>4</sort_order>
</service>
<service>
<id>5</id>
<name>Employement Verification</name>
<kroll_key>JOB_HISTORY</kroll_key>
<sort_order>5</sort_order>
</service>
<service>
<id>6</id>
<name>Negative Press Check</name>
<kroll_key>NEGATIVE_PRESS</kroll_key>
<sort_order>6</sort_order>
</service>
<service>
<id>7</id>
<name>Professional License Verification</name>
<kroll_key>PROF_QUALS</kroll_key>
<sort_order>7</sort_order>
</service>
<service>
<id>8</id>
<name>Address Locator Database</name>
<kroll_key>SSN</kroll_key>
<sort_order>8</sort_order>
</service>
</services>
</xsl:variable>
<xsl:template match="/">
<table>
<tbody>
<tr>
<th>Qty</th>
<th>Service Description</th>
</tr>
<xsl:apply-templates/>
</tbody>
</table>
</xsl:template>
<xsl:template match="//package">
<xsl:choose>
<!--xsl:when test="./id=$package_id"-->
<xsl:when test="./id=4">
<xsl:for-each select=".//service[count>0]">
<xsl:variable name="service_id" select="./id"/>
<tr>
<td>
<xsl:value-of select="./count"/>
</td>
<td>
<xsl:value-of select="$services/services/service[id=
$service_id]/name"/>
</td>
</tr>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

M

unread,
Apr 26, 2007, 10:05:53 PM4/26/07
to
Hi,

The variable $services contains an RTF (Result Tree Fragment) rather than a
node-set. You will need to convert the RTF to a node-set with an
appropriate (for your transformation engine) extension function, e.g.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:param name="package_id" select="4"/>
<xsl:output method="html" indent="yes"/>
<xsl:variable name="rtf_services">

<xsl:variable name="services" select="msxsl:node-set($rtf_services)"/>

<xsl:template match="/">
<table>
<tbody>
<tr>
<th>Qty</th>
<th>Service Description</th>
</tr>
<xsl:apply-templates/>
</tbody>
</table>
</xsl:template>

<xsl:template match="package">


<xsl:choose>
<!--xsl:when test="./id=$package_id"-->

<xsl:when test="id = 4">
<xsl:for-each select="services/service[count &gt; 0]">
<xsl:variable name="service_id" select="id"/>
<tr>
<td>
<xsl:value-of select="count"/>


</td>
<td>
<xsl:value-of select="$services/services/service[id =
$service_id]/name"/>
</td>
</tr>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

Some other tips:-

> <xsl:template match="//package">

The recursive descent operator // at the start of a match pattern is utterly
redundant.

> <xsl:for-each select=".//service[count>0]">

In XPath expressions, you should avoid using // unless you absolutely need
to.

> <xsl:when test="./id=4">

You don't really need the ./ in front of expressions.

Also, your stylesheet is relying on built-in template rules to arrive at the
template to match <package> - this may come back to bite you if you aren't
aware of this. For example, if in the future, the XML contains additional
elements that you are uninterested in and which contain text nodes - you
will start to get spurious text in your output. e.g.

<packages>
<metadata>
<column>This shouldn't be in my output!!</column>
</metadata>
<package>
<id>1</id>
...
</package>
</packages>


HTH
M

"BradR" <bdr...@gmail.com> wrote in message
news:1177633448.2...@s33g2000prh.googlegroups.com...

0 new messages