XML to XML using XSL. Please, help me! Thank You!

S

shapper

Hello,

I am for days trying to apply a XSL transformation to a XML file and
display the result in a the browser. I am using Asp.Net 2.0.

Please, could someone post just a simple code example, either C# or
VB.NET, that I can try.

Thank You Very Much,

Miguel
 
S

sloan

Here is an example I had around. My xml is "excel flavored", but you should
be able to pick up the points.

1. I start with a source xml called "start out" xml
2. I supply the xsl.
3. I apply the xsl to the xml.
4. It results in ~another xml file.

If this isn't what you're after, then you need to describe your issue a
little more in detail.

Good luck, I think this will help.

-------------------"START OUT" XML--------------------------------

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:eek:ffice:spreadsheet"
xmlns:eek:="urn:schemas-microsoft-com:eek:ffice:eek:ffice"
xmlns:x="urn:schemas-microsoft-com:eek:ffice:excel"
xmlns:ss="urn:schemas-microsoft-com:eek:ffice:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:eek:ffice:eek:ffice">
<Author>sholliday</Author>
<LastAuthor>name</LastAuthor>
<Created>2003-12-22T18:58:08Z</Created>
<LastSaved>2003-12-23T15:51:38Z</LastSaved>
<Company>organization</Company>
<Version>11.8107</Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:eek:ffice:excel">
<WindowHeight>8835</WindowHeight>
<WindowWidth>11340</WindowWidth>
<WindowTopX>480</WindowTopX>
<WindowTopY>120</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="8" ss:ExpandedRowCount="4"
x:FullColumns="1"
x:FullRows="1">
<Column ss:Index="2" ss:AutoFitWidth="0" ss:Width="78"/>
<Column ss:AutoFitWidth="0" ss:Width="71.25"/>
<Column ss:AutoFitWidth="0" ss:Width="72"/>
<Column ss:Index="6" ss:AutoFitWidth="0" ss:Width="54.75"/>
<Column ss:AutoFitWidth="0" ss:Width="60.75"/>
<Row>
<Cell><Data ss:Type="String">myuid</Data></Cell>
<Cell><Data ss:Type="String">mylastname</Data></Cell>
<Cell><Data ss:Type="String">myfirstname</Data></Cell>
<Cell><Data ss:Type="String">myaddress1</Data></Cell>
<Cell><Data ss:Type="String">mycity</Data></Cell>
<Cell><Data ss:Type="String">mystate</Data></Cell>
<Cell><Data ss:Type="String">myzip</Data></Cell>
<Cell><Data ss:Type="String">mygender</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">123</Data></Cell>
<Cell><Data ss:Type="String">smith</Data></Cell>
<Cell><Data ss:Type="String">john</Data></Cell>
<Cell><Data ss:Type="String">123 main</Data></Cell>
<Cell><Data ss:Type="String">charlotte</Data></Cell>
<Cell><Data ss:Type="String">nc</Data></Cell>
<Cell><Data ss:Type="Number">55555</Data></Cell>
<Cell><Data ss:Type="String">M</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">234</Data></Cell>
<Cell><Data ss:Type="String">jones</Data></Cell>
<Cell><Data ss:Type="String">mary</Data></Cell>
<Cell><Data ss:Type="String">543 hickory</Data></Cell>
<Cell><Data ss:Type="String">statesville</Data></Cell>
<Cell><Data ss:Type="String">nc</Data></Cell>
<Cell><Data ss:Type="Number">44444</Data></Cell>
<Cell><Data ss:Type="String">F</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">345</Data></Cell>
<Cell><Data ss:Type="String">moore</Data></Cell>
<Cell><Data ss:Type="String">pat</Data></Cell>
<Cell><Data ss:Type="String">342 sycamore</Data></Cell>
<Cell><Data ss:Type="String">asheville</Data></Cell>
<Cell><Data ss:Type="String">nc</Data></Cell>
<Cell><Data ss:Type="Number">33333</Data></Cell>
<Cell><Data ss:Type="String">U</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:eek:ffice:excel">
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveCol>1</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet2">
<WorksheetOptions xmlns="urn:schemas-microsoft-com:eek:ffice:excel">
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet3">
<WorksheetOptions xmlns="urn:schemas-microsoft-com:eek:ffice:excel">
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>



---------------END----"START OUT" XML--------------------------------


-------- START XSL TO APPLY--------------------------

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:eek:="urn:schemas-microsoft-com:eek:ffice:eek:ffice"
xmlns:x="urn:schemas-microsoft-com:eek:ffice:excel"
xmlns:ss="urn:schemas-microsoft-com:eek:ffice:spreadsheet" >



<xsl:template match="/">
<OneWellDefinedDS>
<!--<myRootPosition><xsl:value-of select="position()"/></myRootPosition>-->
<xsl:apply-templates select="ss:Workbook"/>
</OneWellDefinedDS>
</xsl:template>


<xsl:template match="ss:Workbook">
<!--<myWorkbookPosition><xsl:value-of
select="position()"/></myWorkbookPosition>-->

<xsl:apply-templates select="ss:Worksheet"/>

</xsl:template>


<xsl:template match="ss:Worksheet">
<!--<myWorksheetPosition><xsl:value-of
select="position()"/></myWorksheetPosition>-->


<xsl:choose>
<xsl:when test="position()=1">

<xsl:apply-templates select="ss:Table"/>

</xsl:when>


<xsl:eek:therwise>


</xsl:eek:therwise>

</xsl:choose>





</xsl:template>


<xsl:template match="ss:Table">
<!--<myTablePosition><xsl:value-of
select="position()"/></myTablePosition>-->
<xsl:apply-templates select="ss:Row"/>

</xsl:template>


<xsl:template match="ss:Row">
<!--<myRowPosition><xsl:value-of select="position()"/></myRowPosition>-->


<xsl:choose>
<xsl:when test="position()=1">

</xsl:when>


<xsl:eek:therwise>
<customerInfo>
<!--<myPosition><xsl:value-of select="position()"/></myPosition>-->

<xsl:apply-templates select="ss:Cell"/>
</customerInfo>

</xsl:eek:therwise>

</xsl:choose>


</xsl:template>


<xsl:template match="ss:Cell">




<!--<myCellPosition><xsl:value-of
select="$varCurrentAttributePosition"/></myCellPosition>-->

<xsl:choose>
<xsl:when test="position()=1">
<xsl:call-template name="xuid" />
</xsl:when>
<xsl:when test="position()=2">
<xsl:call-template name="xlastname" />
</xsl:when>

<xsl:when test="position()=3">
<xsl:call-template name="xfirstname" />
</xsl:when>

<xsl:eek:therwise>

</xsl:eek:therwise>

</xsl:choose>

</xsl:template>


<xsl:template name="xuid">
<uid><xsl:value-of select="."/></uid>
</xsl:template>

<xsl:template name="xlastname">
<lastname><xsl:value-of select="."/></lastname>
</xsl:template>

<xsl:template name="xfirstname">
<firstname><xsl:value-of select="."/></firstname>
</xsl:template>




</xsl:stylesheet>





-------- END XSL TO APPLY--------------------------



C# Code START (this will probably show as deprecated, but that's what I had
at the time.. alter as necessary)


public void DoTranslation(string xmlFile, string xslFile, string outputFile)

{

try

{

//Create a new XslTransform object.

XslTransform xslt = new XslTransform();

//Load the stylesheet.

xslt.Load(xslFile);

//Create a new XPathDocument and load the XML data to be transformed.

XPathDocument mydata = new XPathDocument(xmlFile);

//Create an XmlTextWriter which outputs to the console.

//XmlWriter writer = new XmlTextWriter(Console.Out);

//Transform the data and send the output to the console.

//xslt.Transform(mydata,null,writer, null);

xslt.Transform (xmlFile, outputFile);

}

catch (Exception ex)

{

debugMsg = ex.Message;

Console.WriteLine (debugMsg);

}



}











------------ Expected Result ( you shouldn't need this ... as the xsl
transformation above should create the xml below ,,, I put it here as a
reference )





<?xml version="1.0" encoding="utf-8" ?>
- <OneWellDefinedDS xmlns:eek:="urn:schemas-microsoft-com:eek:ffice:eek:ffice"
xmlns:x="urn:schemas-microsoft-com:eek:ffice:excel"
xmlns:ss="urn:schemas-microsoft-com:eek:ffice:spreadsheet">
- <customerInfo>
<uid>123</uid>
<lastname>smith</lastname>
<firstname>john</firstname>
</customerInfo>
- <customerInfo>
<uid>234</uid>
<lastname>jones</lastname>
<firstname>mary</firstname>
</customerInfo>
- <customerInfo>
<uid>345</uid>
<lastname>moore</lastname>
<firstname>pat</firstname>
</customerInfo>
</OneWellDefinedDS>
 
S

shapper

Hi Sloan,

I followed your example and some information I found in internet and I
created a new version of my code and now I get the following:

This XML file does not appear to have any style information
associated with it. The document tree is shown below.
<urlset/>

This is really driving me crazy.

Do you or anyone else knows how to solve this?

----- New VB.NET code -----

' Create and load XSL document
Dim xslGoogle As XslCompiledTransform = New XslCompiledTransform

xslGoogle.Load(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings("By27.Assets")
& "/_Web/Google/SiteMap.xsl"))

' Create xpath document with XML document
Dim xpathGoogle As System.Xml.XPath.XPathDocument = New
System.Xml.XPath.XPathDocument(HttpContext.Current.Server.MapPath("~/Web.sitemap")

' Create memory stream
Dim msGoogle As System.IO.Stream = New System.IO.MemoryStream

' Create XSL arguments list
Dim googleXslArguments As XsltArgumentList = New XsltArgumentList
googleXslArguments.AddParam("Domain", "",
"http://www.domain.com")

' Transform XML document
xslGoogle.Transform(xpathGoogle, googleXslArguments, msGoogle)

' Flush the stream and set the position to 0
msGoogle.Flush()
msGoogle.Position = 0

' Create stream reader
Dim srGoogle As System.IO.StreamReader = New
System.IO.StreamReader(msGoogle)

' Display XML in browser
context.Response.Clear()
context.Response.ContentType = "text/xml; charset=utf-8"
context.Response.Write(srGoogle.ReadToEnd)
context.Response.End()


---- 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:eek:utput method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>
<xsl:param name="Domain"/>
<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="$Domain" />
<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>

Thank You Very Much,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top