XSLT from Dataset questions

G

Guest

I'm very new to developing in .NET and even newer to XML. The past few days
have been pretty frustrating for me because I'm trying to perform a transform
on data in a dataset and it's not working.

Basically, all of the data is stored in an Access .mdb that is read into a
dataset when the application is loaded. There are some minor changes to the
schema between what is in the physical database to accomodate some things
that Access just plain won't do (I chose Access over MSDE or even just
storing everything in XML for a number of reasons).

I use the following code to transform the schema and the data from the
dataset and load the transform file:

Dim appbase As String =
AppDomain.CurrentDomain.SetupInformation.ApplicationBase()
Dim vcsxmldoc As XPathDocument
Dim _xmlstream As Stream = New MemoryStream
Dim _xsltstream As Stream = New MemoryStream
Dim _xsltreader As StreamReader = New StreamReader(appbase &
"/vcsxslt.xslt")
Dim _xsltxmlreader As XmlReader

Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
rf = Me
VDPMain.vcsdataset.WriteXml(_xmlstream, XmlWriteMode.WriteSchema)
VDPMain.vcsdataset.WriteXml("vcsds.xml", XmlWriteMode.WriteSchema)
_xmlstream.Position = 0
vcsxmldoc = New XPathDocument(_xmlstream)
_xsltxmlreader = New XmlTextReader(_xsltreader)
End Sub

Private Sub btnReport_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnReport.Click
Dim filename As String = appbase & "designs\test.htm"
Dim vcsxslt As XslTransform = New XslTransform
Dim vcsxslresolver As XmlResolver
Dim vcsxslargs As XsltArgumentList = New XsltArgumentList
Dim vcsxmlwriter As XmlWriter = New XmlTextWriter(filename,
System.Text.Encoding.UTF8)

vcsxslt.Load(_xsltxmlreader)
vcsxslt.Transform(vcsxmldoc, Nothing, vcsxmlwriter, Nothing)
vcsxmlwriter.Close()
_xsltreader.Close()
_xmlstream.Close()
_xsltstream.Close()
End Sub

This works great...fantastic...the problem is, nothing turns out in the
transform the way I expect. Here's the first few lines of the result of the
writexml from the dataset:

<?xml version="1.0" standalone="yes" ?>
- <VCSDS xmlns="http://www.tempuri.org/VCSDS.xsd">
- <xs:schema id="VCSDS" targetNamespace="http://www.tempuri.org/VCSDS.xsd"
xmlns:mstns="http://www.tempuri.org/VCSDS.xsd"
xmlns="http://www.tempuri.org/VCSDS.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified" elementFormDefault="qualified">
+ <xs:element name="VCSDS" msdata:IsDataSet="true">
- <xs:complexType>
- <xs:choice maxOccurs="unbounded">
- <xs:element name="factions">
....
</xs:element>
</xs:schema>
- <factions>
<faction_name>All</faction_name>
<game_id_fk>1</game_id_fk>
<id>1</id>
</factions>

As you can see, the schema is up at the top and then after that the actual
data (such as factions). My problem is that no matter what I do with the
XSLT file,I will only see Hi!, and never Success!!. What am I doing wrong
here?

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:blush:utput method="html"/>
<xsl:template match="/">
Hi!
<xsl:apply-templates select="//factions"/>
</xsl:template>

<xsl:template match="//factions">
Success!!
</xsl:template>
</xsl:stylesheet>
 
B

Bart Mermuys

Hi,

Wil said:
I'm very new to developing in .NET and even newer to XML. The past few
days
have been pretty frustrating for me because I'm trying to perform a
transform
on data in a dataset and it's not working.

Basically, all of the data is stored in an Access .mdb that is read into a
dataset when the application is loaded. There are some minor changes to
the
schema between what is in the physical database to accomodate some things
that Access just plain won't do (I chose Access over MSDE or even just
storing everything in XML for a number of reasons).

I use the following code to transform the schema and the data from the
dataset and load the transform file:

Dim appbase As String =
AppDomain.CurrentDomain.SetupInformation.ApplicationBase()
Dim vcsxmldoc As XPathDocument
Dim _xmlstream As Stream = New MemoryStream
Dim _xsltstream As Stream = New MemoryStream
Dim _xsltreader As StreamReader = New StreamReader(appbase &
"/vcsxslt.xslt")
Dim _xsltxmlreader As XmlReader

Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
rf = Me
VDPMain.vcsdataset.WriteXml(_xmlstream, XmlWriteMode.WriteSchema)
VDPMain.vcsdataset.WriteXml("vcsds.xml", XmlWriteMode.WriteSchema)
_xmlstream.Position = 0
vcsxmldoc = New XPathDocument(_xmlstream)
_xsltxmlreader = New XmlTextReader(_xsltreader)
End Sub

Private Sub btnReport_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnReport.Click
Dim filename As String = appbase & "designs\test.htm"
Dim vcsxslt As XslTransform = New XslTransform
Dim vcsxslresolver As XmlResolver
Dim vcsxslargs As XsltArgumentList = New XsltArgumentList
Dim vcsxmlwriter As XmlWriter = New XmlTextWriter(filename,
System.Text.Encoding.UTF8)

vcsxslt.Load(_xsltxmlreader)
vcsxslt.Transform(vcsxmldoc, Nothing, vcsxmlwriter, Nothing)
vcsxmlwriter.Close()
_xsltreader.Close()
_xmlstream.Close()
_xsltstream.Close()
End Sub

This works great...fantastic...the problem is, nothing turns out in the
transform the way I expect. Here's the first few lines of the result of
the
writexml from the dataset:

<?xml version="1.0" standalone="yes" ?>
- <VCSDS xmlns="http://www.tempuri.org/VCSDS.xsd">
- <xs:schema id="VCSDS" targetNamespace="http://www.tempuri.org/VCSDS.xsd"
xmlns:mstns="http://www.tempuri.org/VCSDS.xsd"
xmlns="http://www.tempuri.org/VCSDS.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified" elementFormDefault="qualified">
+ <xs:element name="VCSDS" msdata:IsDataSet="true">
- <xs:complexType>
- <xs:choice maxOccurs="unbounded">
- <xs:element name="factions">
...
</xs:element>
</xs:schema>
- <factions>
<faction_name>All</faction_name>
<game_id_fk>1</game_id_fk>
<id>1</id>
</factions>

As you can see, the schema is up at the top and then after that the actual
data (such as factions). My problem is that no matter what I do with the
XSLT file,I will only see Hi!, and never Success!!. What am I doing wrong
here?

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:blush:utput method="html"/>
<xsl:template match="/">
Hi!
<xsl:apply-templates select="//factions"/>
</xsl:template>

<xsl:template match="//factions">
Success!!
</xsl:template>
</xsl:stylesheet>

You're on the right track, but you are forgetting that xml has namespaces.
Meaning that not all elements belong to the same namespace.

There are two ways of using namespaces:

1. Default namespace, eg. :
<VCSDS xmlns="http://www.tempuri.org/VCSDS.xsd">
Here "http://www.tempuri.org/VCSDS.xsd" is the default namespace for child
elements unless they use something else.

2. Prefixed namespaces, eg. :
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
Here "xsl" is a prefix for the "http://www.w3.org/1999/XSL/Transform"
namespace. The namespace is used on the element or child elements if they
are prefixed with "xsl", eg.:
<xsl:blush:utput method="html"/>

---

Back to your problem. As you can see in your dataset xml there is a
default namespace used "http://www.tempuri.org/VCSDS.xsd" and it does apply
to your dataset factions elements.

This means that when you do XPath lookups (inside the xslt) you must also
use that namespace for the elements, you could use a prefixed namespace for
that:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ds="http://www.tempuri.org/VCSDS.xsd"
version="1.0">
<xsl:blush:utput method="html"/>
<xsl:template match="/">
Hi!
<xsl:apply-templates select="//ds:factions"/>
</xsl:template>

<xsl:template match="//ds:factions">
Success!!
</xsl:template>
</xsl:stylesheet>


hth,
greetings
 

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

Top