HighTechTalks DotNet Forums  

XslTransform - Pass XmlDocument.DocumentElement as Parameter

Dotnet Framework (SDK) microsoft.public.dotnet.framework.sdk


Discuss XslTransform - Pass XmlDocument.DocumentElement as Parameter in the Dotnet Framework (SDK) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default XslTransform - Pass XmlDocument.DocumentElement as Parameter - 02-08-2006 , 04:19 PM






Hello,

I am trying to pass an XmlDocument's DocumentElement as a parameter to
an XSL template. I'm doing so because I want to process two XML
documents in the XSL template, and output one XML document as a result.


My results yield blanks for the second XML file. Note that I've tried
using a variable instead of a parameter, and that works. But I don't
want to use the document('Xml2.xml') logic in my XSL template.

Here is my code.

Dim m_xmlDom1 As XmlDocument
Dim m_xmlDom2 As XmlDocument

'Create document 1
m_xmlDom1 = New XmlDocument
m_xmlDom1.Load("Xml1.xml")
'Create document 2
m_xmlDom2 = New XmlDocument
m_xmlDom2.Load("Xml2.xml")
' Create the XslTransform and load the stylesheet.
Dim xslt As XslTransform = New XslTransform
xslt.Load("TwoXmlFiles.xsl")
' Create an XsltArgumentList.
Dim xslArg As XsltArgumentList = New XsltArgumentList
' Add parameters
xslArg.AddParam("xml2", "", m_xmlDom2.DocumentElement)

'Create an XmlTextWriter to handle the output, but put it in a
string.
Dim sb As StringBuilder = New StringBuilder
Dim sw As StringWriter = New StringWriter(sb)
Dim xw As XmlTextWriter = New XmlTextWriter(sw)

'Transform the file.
Try
xslt.Transform(m_xmlDom1.CreateNavigator(), xslArg, xw,
Nothing)

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'Get the output
MessageBox.Show(sb.ToString())
'Close the xml text writer
xw.Close()

Here is Xml1.xml.

<?xml version="1.0" encoding="UTF-8"?>
<CONTACTS>
<CONTACT>
<NAME>John Smith</NAME>
</CONTACT>
</CONTACTS>

Here is Xml2.xml.

<?xml version="1.0"?>
<PROPERTIES>
<PROPERTY>
<NAME>1600 West York Ave., York, MN 21838</NAME>
</PROPERTY>
</PROPERTIES>

Here is my Xsl template.

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

<xslaram name="xml2" select="."/>

<xsl:template match="/">
<xsl:apply-templates select="CONTACTS/CONTACT"/>
</xsl:template>

<xsl:template match="CONTACT">
<xsl:value-of select="NAME"/>
<xsl:value-of select="count($xml2/PROPERTIES/PROPERTY)"/>
<xsl:apply-templates select="$xml2/PROPERTIES/PROPERTY"/>
</xsl:template>

<xsl:template match="PROPERTY">
<xsl:value-of select="NAME"/>
</xsl:template>

</xsl:stylesheet>

Thanks.

Rob


Reply With Quote
  #2  
Old   
AT
 
Posts: n/a

Default Re: XslTransform - Pass XmlDocument.DocumentElement as Parameter - 02-09-2006 , 08:08 AM






I've figured out the problem. You do not pass a DocumentElement to the
AddParam method. You pass an XPathNavigator as follows.

xslArg.AddParam("xml2", "", m_xmlDom2.CreateNavigator())

And there is it.


rmgalante (AT) yahoo (DOT) com wrote:
Quote:
Hello,

I am trying to pass an XmlDocument's DocumentElement as a parameter to
an XSL template. I'm doing so because I want to process two XML
documents in the XSL template, and output one XML document as a result.


My results yield blanks for the second XML file. Note that I've tried
using a variable instead of a parameter, and that works. But I don't
want to use the document('Xml2.xml') logic in my XSL template.

Here is my code.

Dim m_xmlDom1 As XmlDocument
Dim m_xmlDom2 As XmlDocument

'Create document 1
m_xmlDom1 = New XmlDocument
m_xmlDom1.Load("Xml1.xml")
'Create document 2
m_xmlDom2 = New XmlDocument
m_xmlDom2.Load("Xml2.xml")
' Create the XslTransform and load the stylesheet.
Dim xslt As XslTransform = New XslTransform
xslt.Load("TwoXmlFiles.xsl")
' Create an XsltArgumentList.
Dim xslArg As XsltArgumentList = New XsltArgumentList
' Add parameters
xslArg.AddParam("xml2", "", m_xmlDom2.DocumentElement)

'Create an XmlTextWriter to handle the output, but put it in a
string.
Dim sb As StringBuilder = New StringBuilder
Dim sw As StringWriter = New StringWriter(sb)
Dim xw As XmlTextWriter = New XmlTextWriter(sw)

'Transform the file.
Try
xslt.Transform(m_xmlDom1.CreateNavigator(), xslArg, xw,
Nothing)

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'Get the output
MessageBox.Show(sb.ToString())
'Close the xml text writer
xw.Close()

Here is Xml1.xml.

?xml version="1.0" encoding="UTF-8"?
CONTACTS
CONTACT
NAME>John Smith</NAME
/CONTACT
/CONTACTS

Here is Xml2.xml.

?xml version="1.0"?
PROPERTIES
PROPERTY
NAME>1600 West York Ave., York, MN 21838</NAME
/PROPERTY
/PROPERTIES

Here is my Xsl template.

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

xslaram name="xml2" select="."/

xsl:template match="/"
xsl:apply-templates select="CONTACTS/CONTACT"/
/xsl:template

xsl:template match="CONTACT"
xsl:value-of select="NAME"/
xsl:value-of select="count($xml2/PROPERTIES/PROPERTY)"/
xsl:apply-templates select="$xml2/PROPERTIES/PROPERTY"/
/xsl:template

xsl:template match="PROPERTY"
xsl:value-of select="NAME"/
/xsl:template

/xsl:stylesheet

Thanks.

Rob


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.