posted on Saturday, May 22, 2004 7:12 AM
by
jdixon
Embeddng XML in an XSLT in .NET
I have a XSLT that needs to reference a table of data, such as states. This data is very static, so it didn't seem to be worth the trouble to store this data in a table and pass it in as a node set. (See one my previous posts for an example of this.) I decided to embed this data as an XML “data island” in my XSLT and reference it via an XSLT variable. This is pretty standard stuff, but I couldn't get it to work.
I consulted my copy of XSLT and XPath On The Edge, by Jeni Tennison, and found that I was doing everything correctly. It still didn't work, so what was wrong?
.NET was the problem. In the 1.1 version of the framework, the transform method has a fourth argument, which is for a resolver. I've always passed null (or nothing) here, and everything worked well. When using the XSLT document() function, however, the resolver becomes important. Without a resolver, the document function is ignored. Examine the working code:
XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:st="http://www.mysite.com/States">
<xsl:output method="xml" indent="yes" />
<st:States>
<st:State Code="AL" Name="Alabama"/>
<st:State Code="MS" Name="Mississippi"/>
<st:State Code="TN" Name="Tennessee"/>
</st:States>
<xsl:variable name="VisaCodes" select="document('')//st:States/st:State"/>
</xsl:stylesheet>
.NET
myTransform.Transform(myXPathDoc, myArgumentList, OutputFile, new XmlUrlResolver());