I must be an idiot because I STILL cannot coax .NET to
successfully execute a transform which uses the XSLT id()
function. Here's the bare-bones code:
XmlValidatingReader xvr = new XmlValidatingReader(
new XmlTextReader( @"s:\data\test.xml",
new NameTable() ) );
xvr.EntityHandling = EntityHandling.ExpandEntities;
xvr.ValidationType = ValidationType.Schema;
xvr.ValidationEventHandler +=
new ValidationEventHandler( this.veh );
// this.veh() simply echos any validation errors.
XslTransform xsl = new XslTransform();
xsl.Load( @"s:\data\test.xsl" );
XPathDocument doc = new XPathDocument( xvr );
XmlDocument xml = new XmlDocument();
xml.Load( xsl.Transform( doc, xArgs,
new XmlUrlResolver() ) );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.xml
<mydata xsi:noNamespaceSchemaLocation="test.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance">
<items>
<file id="F001" title="abc" size="123"/>
<file id="F002" title="xyz" size="789"/>
<notification id="PINK" title="Pink Flowers"/>
</items>
</mydata>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"
encoding="ISO-8859-1" />
<xsl:template match="/">
<results>
<by-id>
<xsl:copy-of select="id('PINK')"/>
</by-id>
<by-expression>
<xsl:copy-of select="//*
[@id='PINK']"/>
</by-expression>
</results>
</xsl:template>
</xsl:stylesheet>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I'm amazed at how much effort I've expended to get this
working since it was SO SIMPLE with MSXML 4 in WSH (or
ASP):
<job>
<script language="JavaScript">
{
var xml = getXml( "s:\\data\\test.xml" );
var xsl = getXml( "s:\\data\\test.xsl" );
if (xml != null && xsl != null)
{
WScript.echo( xml.transformNode( xsl ) );
}
}
function getXml( path )
{
var xml = new ActiveXObject
( "Msxml2.FreeThreadedDOMDocument.4.0" );
xml.setProperty( "SelectionLanguage", "XPath" );
xml.async = false;
xml.resolveExternals = true;
xml.load( path );
if (xml.parseError.errorCode != 0)
{
var e = xml.parseError;
WScript.echo( e.errorCode + " - "+ e.reason + "\n" +
+ e.url + " line " + e.line + ", char "+ e.linepos
+ "\n" + e.srcText );
return null;
}
return xml;
}
</script>
</job>
Pulling my hair out...
Antonio Collins