Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 10th, 2003, 03:40 AM
Registered User
 
Join Date: Sep 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT and CSV

Hi there,
I have a problem with transforming from XML to CSV using a very simple stylesheet.

Following is an example input file
------------------------------------
<CD>
 <PROJECT ID="TEST" CODE="TESTCODE"></PROJECT>
 <PROJECT ID="TEST2" CODE="TESTCODE2"></PROJECT>
</CD>

Following is the VB 6 code
----------------------------

Public Function TransformFromDOM(xmlSource As MSXML2.FreeThreadedDOMDocument40, strXSL As String) As FreeThreadedDOMDocument40
    On Error GoTo ErrHandler

    Dim xmlOutput As New MSXML2.FreeThreadedDOMDocument40
    Dim xmlStylesheet As New MSXML2.FreeThreadedDOMDocument40
    Dim xslTemplate As New MSXML2.XSLTemplate40
    Dim xslProcessor As MSXML2.IXSLProcessor

    xmlStylesheet.Load strXSL

    If xmlStylesheet.parseError.reason <> "" Then
        MsgBox "ERROR: " & xmlStylesheet.parseError.reason & vbCrLf & "SOURCE: " & _
                    xmlStylesheet.parseError.srcText, vbExclamation, App.Title
        Exit Function
    End If

    Set xslTemplate.stylesheet = xmlStylesheet

    Set xslProcessor = xslTemplate.createProcessor

    xslProcessor.input = xmlSource
    xslProcessor.output = xmlOutput

    xslProcessor.Transform

    Set TransformFromDOM = xmlOutput

    Exit Function

ErrHandler:
    MsgBox "ERROR: " & err.Number & " " & err.Description, _
                vbCritical, App.Title
    Set TransformFromDOM = Nothing
End Function


Now here is the simple transformation which isn't working at all
-----------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output version="1.0" indent="no" method="text" omit-xml-declaration="yes" />

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

    <xsl:template match="CD"><xsl:text>Ref,Start,Finish,Title#13;</xsl:text>
        <xsl:for-each select="//Project">
            <xsl:value-of select="concat( @ID, ',', @CODE)"/>
            <xsl:text>#13;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>


The output being generated is a blank file completely.

Does anyone know what is happening.. Note that when I apply the transformation from within XML Spy using the same XML parser (MSXML4.0) everything works fine.

Thankx in advance
Jonathan


 
Old September 10th, 2003, 04:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

A few things about your stylesheet:
1. XML is case-sensistive so you need to select/match PROJECT not Project
2. there is no need to use the expensive // operation to match the PROJECT node
3. a new-line is written like this #13;#10;
4. there is no need for the concat() function - just output the items and output a comma between them
try this stylesheet and see what happens
Code:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/">
      <xsl:apply-templates select="CD"/>
    </xsl:template>

    <xsl:template match="CD"><xsl:text>Ref,Start,Finish,Title#13;#10;</xsl:text>
        <xsl:apply-templates select="PROJECT"/>
    </xsl:template>

    <xsl:template match="PROJECT">
        <xsl:value-of select="@ID"/>,<xsl:value-of select="@CODE"/>
        <xsl:text>#13;#10;</xsl:text>
    </xsl:template>
</xsl:stylesheet>
I'm surprised it works in XML Spy!

hth
Phil
 
Old September 10th, 2003, 04:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

How do we get an ampersand to show up in this forum?

The new-line should be [ampersand]#13;[ampersand]#10;

rgds
Phil
 
Old September 10th, 2003, 05:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Just noticed a couple of things about your VB
1. "xmlStylesheet.Load strXSL" should read "xmlStylesheet.LoadXML strXSL" because the Load methos is for loading files
2. you are setting the output of your transformation to a DOM Document, but this will not work because the output is text not XML. You need to output to a string. The way to do that is do not set the output property, but instead read it into a string variable after the transform has been done. Like this:
Code:
Public Function TransformFromDOMToString(xmlSource As MSXML2.FreeThreadedDOMDocument40, strXSL As String) As String ' return type changed
    On Error GoTo ErrHandler

    'Dim xmlOutput As New MSXML2.FreeThreadedDOMDocument40  : don't need this anymore
    Dim xmlStylesheet As New MSXML2.FreeThreadedDOMDocument40
    Dim xslTemplate As New MSXML2.XSLTemplate40
    Dim xslProcessor As MSXML2.IXSLProcessor

    xmlStylesheet.loadXML strXSL ' note use of LoadXML here

    If xmlStylesheet.parseError.reason <> "" Then
        MsgBox "ERROR: " & xmlStylesheet.parseError.reason & vbCrLf & "SOURCE: " & _
                    xmlStylesheet.parseError.srcText, vbExclamation, App.Title
        Exit Function
    End If

    Set xslTemplate.stylesheet = xmlStylesheet

    Set xslProcessor = xslTemplate.createProcessor

    xslProcessor.input = xmlSource
    'xslProcessor.output = xmlOutput : don't need this either

    xslProcessor.Transform
    TransformFromDOMToString = xslProcessor.output ' get the output into a string

    'Set TransformFromDOM = xmlOutput : don't need this

    Exit Function

ErrHandler:
    MsgBox "ERROR: " & Err.Number & " " & Err.Description, _
                vbCritical, App.Title
    'Set TransformFromDOM = Nothing : don't need this
End Function
hth
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
XML To CSV with XSLT and Java pcroadkill XSLT 3 February 8th, 2012 11:55 AM
Handle Null or Blank values using xslt in csv data mums XSLT 1 April 3rd, 2008 07:30 PM
Can XSLT read DTD/schema and Generate XSLT.. ROCXY XSLT 1 November 6th, 2006 09:39 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
Problem applying simple XSLT to XML to create CSV gregclark XSLT 2 August 25th, 2005 07:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.