By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

Convert XML file

P: n/a
Hello,

Could someone tell me how to convert a XML file into another XML file
using a XSL file with a parameter? I created the code to do it, and it
seems ok, but it is not working.

Could somebody, please, help me out? I am on this for days.

My code is as follows:

Dim webSiteMap As XmlDocument = New XmlDocument
webSiteMap.Load(HttpContext.Current.Server.MapPath ("~/INPUT.XML"))
Dim googleXsl As XslCompiledTransform = New XslCompiledTransform

googleXsl.Load(HttpContext.Current.Server.MapPath( "~/CONVERT.XSL"))

Dim googleXslArguments As XsltArgumentList = New XsltArgumentList
googleXslArguments.AddParam("Domain", "",
"http://www.domain.com")

Dim webSiteMapStream As MemoryStream = New MemoryStream
googleXsl.Transform(webSiteMap, googleXslArguments,
webSiteMapStream)

context.Response.Clear()
context.Response.ContentType = "text/xml; charset=utf-8"

context.Response.Write(Encoding.UTF8.GetString(web SiteMapStream.GetBuffer))
context.Response.End()

Thanks,
Miguel

Nov 15 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Han
Hello shapper

What do you get from the code, error or empty page?

Temporarily I found a problem in your code. I don't know VB, anyway,

xsl.transform((ixpathNavigable)xmldocument, args, stream);

You should convert the xmldocument into xpathnavigator EXPLICITLY. I think
VB version is ctype.

"shapper" <md*****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Hello,

Could someone tell me how to convert a XML file into another XML file
using a XSL file with a parameter? I created the code to do it, and it
seems ok, but it is not working.

Could somebody, please, help me out? I am on this for days.

My code is as follows:

Dim webSiteMap As XmlDocument = New XmlDocument
webSiteMap.Load(HttpContext.Current.Server.MapPath ("~/INPUT.XML"))
Dim googleXsl As XslCompiledTransform = New XslCompiledTransform

googleXsl.Load(HttpContext.Current.Server.MapPath( "~/CONVERT.XSL"))

Dim googleXslArguments As XsltArgumentList = New XsltArgumentList
googleXslArguments.AddParam("Domain", "",
"http://www.domain.com")

Dim webSiteMapStream As MemoryStream = New MemoryStream
googleXsl.Transform(webSiteMap, googleXslArguments,
webSiteMapStream)

context.Response.Clear()
context.Response.ContentType = "text/xml; charset=utf-8"

context.Response.Write(Encoding.UTF8.GetString(web SiteMapStream.GetBuffer))
context.Response.End()

Thanks,
Miguel

Nov 16 '06 #2

P: n/a
Hi,

I am posting my XSL and XML files as well the new version of my VB.NET
(I also post the conversion to C# using the VB.NET to C# converter
available in
http://www.developerfusion.co.uk/uti...btocsharp.aspx)

The error I am getting is:
XML Parsing Error: not well-formed
Location: http://localhost:1132/GaragemRamos%202006a/Google
Line Number 2, Column 62:<urlset
xmlns="http://www.google.com/schemas/sitemap/0.84" />

Thanks,
Miguel

--------- XSL CODE ---------

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.google.com/schemas/sitemap/0.84"
xmlns:dk="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>
<xsl:param name="WebSiteUrl"/>
<xsl:template match="dk:*"/>
<xsl:template match="@*|text()|comment()"/>
<xsl:template match="/">
<xsl:element name="urlset">
<xsl:apply-templates select="//dk:siteMapNode[@google='true']"/>
</xsl:element>
</xsl:template>
<xsl:template match="dk:siteMapNode">
<xsl:element name="url">
<xsl:element name="loc">
<xsl:value-of select="$WebSiteUrl" />
<xsl:value-of select="substring(@url, 3)"/>
</xsl:element>
<xsl:element name="lastmod">
<xsl:value-of select="@lastmod"/>
</xsl:element>
<xsl:element name="changefreq">
<xsl:value-of select="@changefreq"/>
</xsl:element>
<xsl:element name="priority">
<xsl:value-of select="@priority"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

--------- VB.NET Code ---------

' Create ASP.NET web site map
Dim webSiteMap As XmlDocument = New XmlDocument

' Load ASP.NET's site map

webSiteMap.Load(HttpContext.Current.Server.MapPath ("~/Web.sitemap"))

' Create google xsl document
Dim googleXsl As XslCompiledTransform = New XslCompiledTransform

' Load google xml document

googleXsl.Load(HttpContext.Current.Server.MapPath( "~/SiteMap.xsl"))

' Create xsl arguments list
Dim googleXslArguments As XsltArgumentList = New XsltArgumentList
googleXslArguments.AddParam("WebSiteUrl", "",
"http://www.mydomain.com")

' Create the ASP.NET's site map memory stream
Dim googleSiteMapStream As MemoryStream = New MemoryStream

' Transform Asp.Net's site map to Google's site map and add it to
stream
googleXsl.Transform(CType(webSiteMap, XPath.IXPathNavigable),
googleXslArguments, googleSiteMapStream)

' Flush google site map stream
googleSiteMapStream.Flush()

' Set google site map stream position to 0
googleSiteMapStream.Position = 0

' Output Google sitemap
context.Response.Clear()
context.Response.ContentType = "text/xml; charset=utf-8"

context.Response.Write(Encoding.UTF8.GetString(goo gleSiteMapStream.GetBuffer))
context.Response.End()
--------- C# CODE ---------

XmlDocument webSiteMap = new XmlDocument();
webSiteMap.Load(HttpContext.Current.Server.MapPath ("~/Web.sitemap"));
XslCompiledTransform googleXsl = new XslCompiledTransform();
XsltArgumentList googleXslArguments = new XsltArgumentList();
googleXslArguments.AddParam("WebSiteUrl", "",
"http://www.mydomain.com");
MemoryStream googleSiteMapStream = new MemoryStream();
googleXsl.Transform(((XPath.IXPathNavigable)(webSi teMap)),
googleXslArguments, googleSiteMapStream);
googleSiteMapStream.Flush();
googleSiteMapStream.Position = 0;
context.Response.Clear();
context.Response.ContentType = "text/xml; charset=utf-8";
context.Response.Write(Encoding.UTF8.GetString(goo gleSiteMapStream.GetBuffer));
context.Response.End();
--------- XML File (Web.sitemap) ---------

<?xml version="1.0" encoding="utf-8" ?>
<siteMap
xmlns="http://schemas.microsoft.com/ASPNet/SiteMap-File-1.0" >
<siteMapNode>
<siteMapNode
url="~/Default.aspx"
title="Home"
description="Desc1"
changefreq="daily"
lastmod="2006-11-01T20:25:42+01:00"
priority="1"
google="true" />
<siteMapNode
url="~/Company.aspx"
title="Company"
description="Desc2"
changefreq="daily"
lastmod="2006-11-01T20:25:42+01:00"
priority="0.2"
google="true" />
<siteMapNode
url="~/Contacts.aspx"
title="Contacts"
description="Contacts"
changefreq="daily"
lastmod="2006-11-01T20:25:42+01:00"
priority="0.4"
google="false" />
</siteMapNode>
</siteMap>

Well, that is all I think. I hope someone can help me out.

Thank You Very Much,
Miguel

Han wrote:
Hello shapper

What do you get from the code, error or empty page?

Temporarily I found a problem in your code. I don't know VB, anyway,

xsl.transform((ixpathNavigable)xmldocument, args, stream);

You should convert the xmldocument into xpathnavigator EXPLICITLY. I think
VB version is ctype.

"shapper" <md*****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Hello,

Could someone tell me how to convert a XML file into another XML file
using a XSL file with a parameter? I created the code to do it, and it
seems ok, but it is not working.

Could somebody, please, help me out? I am on this for days.

My code is as follows:

Dim webSiteMap As XmlDocument = New XmlDocument
webSiteMap.Load(HttpContext.Current.Server.MapPath ("~/INPUT.XML"))
Dim googleXsl As XslCompiledTransform = New XslCompiledTransform

googleXsl.Load(HttpContext.Current.Server.MapPath( "~/CONVERT.XSL"))

Dim googleXslArguments As XsltArgumentList = New XsltArgumentList
googleXslArguments.AddParam("Domain", "",
"http://www.domain.com")

Dim webSiteMapStream As MemoryStream = New MemoryStream
googleXsl.Transform(webSiteMap, googleXslArguments,
webSiteMapStream)

context.Response.Clear()
context.Response.ContentType = "text/xml; charset=utf-8"

context.Response.Write(Encoding.UTF8.GetString(web SiteMapStream.GetBuffer))
context.Response.End()

Thanks,
Miguel
Nov 16 '06 #3

P: n/a
Please,

Could someone help me out?

I just posted my entire code. I still wasn't able to solve my problem

Thank You Very Much,

Miguel

shapper wrote:
Hi,

I am posting my XSL and XML files as well the new version of my VB.NET
(I also post the conversion to C# using the VB.NET to C# converter
available in
http://www.developerfusion.co.uk/uti...btocsharp.aspx)

The error I am getting is:
XML Parsing Error: not well-formed
Location: http://localhost:1132/GaragemRamos%202006a/Google
Line Number 2, Column 62:<urlset
xmlns="http://www.google.com/schemas/sitemap/0.84" />

Thanks,
Miguel

--------- XSL CODE ---------

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.google.com/schemas/sitemap/0.84"
xmlns:dk="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>
<xsl:param name="WebSiteUrl"/>
<xsl:template match="dk:*"/>
<xsl:template match="@*|text()|comment()"/>
<xsl:template match="/">
<xsl:element name="urlset">
<xsl:apply-templates select="//dk:siteMapNode[@google='true']"/>
</xsl:element>
</xsl:template>
<xsl:template match="dk:siteMapNode">
<xsl:element name="url">
<xsl:element name="loc">
<xsl:value-of select="$WebSiteUrl" />
<xsl:value-of select="substring(@url, 3)"/>
</xsl:element>
<xsl:element name="lastmod">
<xsl:value-of select="@lastmod"/>
</xsl:element>
<xsl:element name="changefreq">
<xsl:value-of select="@changefreq"/>
</xsl:element>
<xsl:element name="priority">
<xsl:value-of select="@priority"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

--------- VB.NET Code ---------

' Create ASP.NET web site map
Dim webSiteMap As XmlDocument = New XmlDocument

' Load ASP.NET's site map

webSiteMap.Load(HttpContext.Current.Server.MapPath ("~/Web.sitemap"))

' Create google xsl document
Dim googleXsl As XslCompiledTransform = New XslCompiledTransform

' Load google xml document

googleXsl.Load(HttpContext.Current.Server.MapPath( "~/SiteMap.xsl"))

' Create xsl arguments list
Dim googleXslArguments As XsltArgumentList = New XsltArgumentList
googleXslArguments.AddParam("WebSiteUrl", "",
"http://www.mydomain.com")

' Create the ASP.NET's site map memory stream
Dim googleSiteMapStream As MemoryStream = New MemoryStream

' Transform Asp.Net's site map to Google's site map and add it to
stream
googleXsl.Transform(CType(webSiteMap, XPath.IXPathNavigable),
googleXslArguments, googleSiteMapStream)

' Flush google site map stream
googleSiteMapStream.Flush()

' Set google site map stream position to 0
googleSiteMapStream.Position = 0

' Output Google sitemap
context.Response.Clear()
context.Response.ContentType = "text/xml; charset=utf-8"

context.Response.Write(Encoding.UTF8.GetString(goo gleSiteMapStream.GetBuffer))
context.Response.End()
--------- C# CODE ---------

XmlDocument webSiteMap = new XmlDocument();
webSiteMap.Load(HttpContext.Current.Server.MapPath ("~/Web.sitemap"));
XslCompiledTransform googleXsl = new XslCompiledTransform();
XsltArgumentList googleXslArguments = new XsltArgumentList();
googleXslArguments.AddParam("WebSiteUrl", "",
"http://www.mydomain.com");
MemoryStream googleSiteMapStream = new MemoryStream();
googleXsl.Transform(((XPath.IXPathNavigable)(webSi teMap)),
googleXslArguments, googleSiteMapStream);
googleSiteMapStream.Flush();
googleSiteMapStream.Position = 0;
context.Response.Clear();
context.Response.ContentType = "text/xml; charset=utf-8";
context.Response.Write(Encoding.UTF8.GetString(goo gleSiteMapStream.GetBuffer));
context.Response.End();
--------- XML File (Web.sitemap) ---------

<?xml version="1.0" encoding="utf-8" ?>
<siteMap
xmlns="http://schemas.microsoft.com/ASPNet/SiteMap-File-1.0" >
<siteMapNode>
<siteMapNode
url="~/Default.aspx"
title="Home"
description="Desc1"
changefreq="daily"
lastmod="2006-11-01T20:25:42+01:00"
priority="1"
google="true" />
<siteMapNode
url="~/Company.aspx"
title="Company"
description="Desc2"
changefreq="daily"
lastmod="2006-11-01T20:25:42+01:00"
priority="0.2"
google="true" />
<siteMapNode
url="~/Contacts.aspx"
title="Contacts"
description="Contacts"
changefreq="daily"
lastmod="2006-11-01T20:25:42+01:00"
priority="0.4"
google="false" />
</siteMapNode>
</siteMap>

Well, that is all I think. I hope someone can help me out.

Thank You Very Much,
Miguel

Han wrote:
Hello shapper

What do you get from the code, error or empty page?

Temporarily I found a problem in your code. I don't know VB, anyway,

xsl.transform((ixpathNavigable)xmldocument, args, stream);

You should convert the xmldocument into xpathnavigator EXPLICITLY. I think
VB version is ctype.

"shapper" <md*****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Hello,
>
Could someone tell me how to convert a XML file into another XML file
using a XSL file with a parameter? I created the code to do it, and it
seems ok, but it is not working.
>
Could somebody, please, help me out? I am on this for days.
>
My code is as follows:
>
Dim webSiteMap As XmlDocument = New XmlDocument
>
>
webSiteMap.Load(HttpContext.Current.Server.MapPath ("~/INPUT.XML"))
Dim googleXsl As XslCompiledTransform = New XslCompiledTransform
>
googleXsl.Load(HttpContext.Current.Server.MapPath( "~/CONVERT.XSL"))
>
Dim googleXslArguments As XsltArgumentList = New XsltArgumentList
googleXslArguments.AddParam("Domain", "",
"http://www.domain.com")
>
Dim webSiteMapStream As MemoryStream = New MemoryStream
googleXsl.Transform(webSiteMap, googleXslArguments,
webSiteMapStream)
>
context.Response.Clear()
context.Response.ContentType = "text/xml; charset=utf-8"
>
context.Response.Write(Encoding.UTF8.GetString(web SiteMapStream.GetBuffer))
context.Response.End()
>
Thanks,
Miguel
>
Nov 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.