By using the XslTransform object (TranslateXML function), I was able to =
get this working in .NET 2.0. TranslateXML20 is the function that did =
not work with using XslCompiledTransform. I have attached the code for =
both versions.
Jody
Private Function TranslateXML(ByVal xsltFileName As String, ByVal =
xmlText As String) As String
Dim xst As System.Xml.Xsl.XslTransform =3D Nothing
Dim xal As System.Xml.Xsl.XsltArgumentList =3D Nothing
Dim xtr As System.Xml.XmlTextReader =3D Nothing
Dim resolver As System.Xml.XmlUrlResolver =3D Nothing
Dim xsltExt As XSLTExtension =3D Nothing
Dim xtrIn As System.Xml.XmlTextReader =3D Nothing
Dim xpd As System.Xml.XPath.XPathDocument
Dim sw As StringWriterWithEncoding =3D Nothing
Dim strReturn As String =3D String.Empty
Try
'Setup the XSL Transform object
xtr =3D New System.Xml.XmlTextReader(xmlText, Xml.XmlNodeType.Document, =
Nothing)
xst =3D New System.Xml.Xsl.XslTransform
xst.Load(xsltFileName)
'Prepare the XML document to be transformed
xtrIn =3D New System.Xml.XmlTextReader(xmlText, =
Xml.XmlNodeType.Document, Nothing)
xpd =3D New System.Xml.XPath.XPathDocument(xtrIn)
'Prepare the output objects
'This prevents unprintable characters from being printed at the top of =
the resulting string
sw =3D New StringWriterWithEncoding(New System.Text.UTF8Encoding(False))
'Transform the XML document using the XSLT file and place the output in =
"sw"
resolver =3D New System.Xml.XmlUrlResolver
xal =3D New System.Xml.Xsl.XsltArgumentList
xsltExt =3D New XSLTExtension
xal.AddExtensionObject("urn:date-format", xsltExt)
xst.Transform(xpd, xal, sw, resolver)
strReturn =3D sw.ToString.Replace(" ", " ") 'Replace the   =
character with
Catch ex As Exception
Dim strMessage As String
strMessage =3D "xsltFileName: " & xsltFileName & ControlChars.CrLf
strMessage &=3D "xmlText: " & ControlChars.CrLf & xmlText & =
ControlChars.CrLf
RaiseEvent Exception(Me, New EMailExceptionEventArgs(ex))
Finally
If Not xal Is Nothing Then
xal =3D Nothing
End If
If Not sw Is Nothing Then
sw.Close()
sw =3D Nothing
End If
If Not xtrIn Is Nothing Then
xtrIn.Close()
xtrIn =3D Nothing
End If
If Not xtr Is Nothing Then
xtr.Close()
xtr =3D Nothing
End If
End Try
Return strReturn
End Function
Private Function TranslateXML20(ByVal xsltFileName As String, ByVal =
xmlText As String) As String
Dim xst As System.Xml.Xsl.XslCompiledTransform =3D Nothing
Dim xtrXSLT As System.Xml.XmlTextReader =3D Nothing
Dim xal As System.Xml.Xsl.XsltArgumentList =3D Nothing
Dim resolver As System.Xml.XmlUrlResolver =3D Nothing
Dim xsltExt As XSLTExtension =3D Nothing
Dim xtrXML As System.Xml.XmlTextReader =3D Nothing
Dim xw As Xml.XmlTextWriter =3D Nothing
Dim sw As StringWriterWithEncoding =3D Nothing
Dim strReturn As String =3D String.Empty
Try
'Create the XslTransform object and load the stylesheet
xst =3D New System.Xml.Xsl.XslCompiledTransform
xtrXSLT =3D New System.Xml.XmlTextReader(New =
IO.StreamReader(xsltFileName))
xtrXSLT.ProhibitDtd =3D False
xst.Load(xtrXSLT)
'Prepare the XML document to be transformed
xtrXML =3D New System.Xml.XmlTextReader(xmlText, =
Xml.XmlNodeType.Document, Nothing)
'Prepare the output objects
'This prevents unprintable characters from being printed at the top of =
the resulting string
sw =3D New StringWriterWithEncoding(New System.Text.UTF8Encoding(False))
xw =3D New Xml.XmlTextWriter(sw)
'Transform the XML document using the XSLT file and place the output in =
"sw"
resolver =3D New System.Xml.XmlUrlResolver
xal =3D New System.Xml.Xsl.XsltArgumentList
xsltExt =3D New XSLTExtension
xal.AddExtensionObject("urn:date-format", xsltExt)
xst.Transform(xtrXML, xal, xw, resolver)
strReturn =3D sw.ToString.Replace(" ", " ") 'Replace the   =
character with
Catch ex As Exception
RaiseEvent Exception(Me, New EMailExceptionEventArgs(ex))
Finally
If Not xal Is Nothing Then
xal =3D Nothing
End If
If Not sw Is Nothing Then
sw.Close()
sw =3D Nothing
End If
If Not xw Is Nothing Then
xw.Close()
xw =3D Nothing
End If
If Not xtrXML Is Nothing Then
xtrXML.Close()
xtrXML =3D Nothing
End If
If Not xtrXSLT Is Nothing Then
xtrXSLT.Close()
xtrXSLT =3D Nothing
End If
End Try
Return strReturn
End Function
[quoted text, click to view] "Jody Gelowitz" <jgelowitz_nospam@blah.leevalley.com> wrote in message =
news:u5uzLnOeGHA.2188@TK2MSFTNGP04.phx.gbl...
I have run into an issue with variable scope within an XSLT document =
that is translated in VS.NET 2.0. Under VS.NET 1.1 (XslTransform), this =
code works fine. However, when using VS.NET 2.0 (XslCompiledTransform), =
the exact same XSLT transformation fails with the error:
System.Xml.Xsl.XslLoadException: The variable or parameter =
'lastrecordwaskit' was duplicated within the same scope. An error =
occurred at (174,12).
at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object =
stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
at System.Xml.Xsl.XslCompiledTransform.Load(XmlReader stylesheet)
at MyProject.MyClass.TranslateXML(String xsltFileName, String =
xmlText) in C:\Source\MySolution\MyProject\MyClass.vb:line 155
I have included the XSL code below which is related to the issue. In =
order to maintain the previous value of "iskit", I am declaring a =
variable "lastrecordwaskit" at the end of the loop. Near the beginning =
of the loop, prior to the variable declaration of "lastrecordwaskit", I =
am checking for "position() > 1" before trying to access =
"lastrecordwaskit". In .NET 1.1, this would allow the =
"lastrecordwaskit" variable to be declared prior to it being accessed. =
In .NET 2.0, it appears that this will no longer work.
I need to be able to access the previous value of a variable (or =
previous XML element) while inside of a loop. Are there any solutions =
to this problem with .NET 2.0?
Thanks,
Jody
<xsl:for-each select=3D"Order/Invoice/InvoiceDetails/InvoiceDetail">
<xsl:variable name=3D"iskit">
<xsl:choose>
<xsl:when test=3D"@idt_set_master=3D'N' and =
@idt_set_component_seq>0">
1
</xsl:when>
<xsl:otherwise>
0
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test=3D"position()>1">
<xsl:if test=3D"$lastrecordwaskit=3D1 and $iskit=3D0">
<tr>
<td> </td>
</tr>
</xsl:if>
</xsl:if>=20
{Additional Code ...}
<xsl:variable name=3D"lastrecordwaskit"><xsl:value-of =
select=3D"$iskit"/></xsl:variable>