Problem with XSLTransform in VB.net

Joined
May 1, 2006
Messages
6
Programming Experience
3-5
Hello,

I have a vb.net application that pulls an xml from a webservice, then runs through code to write it to a database. The XML from the webservice does not print blank elements, so I've had to use an XSL stylesheet (included below) for the push to a SQL Server database to convert the XML to show blank elements (i.e. <element></element>).

This works in the code that I have, however, you have to manually paste the xsl link into the xml file each time you pull the xml from the webservice.

To combat this, I found code to transform the xml file to include the xsl file. By messing around, the following vb.net code compiles and runs, and the xml it outputs to the Console is well formed, however, it is missing ALL data from the xml file, so only the elements tags print out.

My experiment is as follows:

'Create the XslTransform.
Dim xslt as XslTransform = new XslTransform()

'Create a resolver and set the credentials to use.
Dim resolver as XmlUrlResolver = new XmlUrlResolver()
resolver.Credentials = CredentialCache.DefaultCredentials

'Load the stylesheet.
xslt.Load("UnitType.xsl", resolver)

'Load the XML data file.
Dim doc as XPathDocument = new XPathDocument("UnitType.xml")

'Create the XmlTextWriter to output to the console.
Dim writer as XmlTextWriter = new XmlTextWriter(Console.Out)

'Transform the file.
xslt.Transform(doc, nothing, writer, nothing)
writer.Close()

---------------------
The Console Output of the above code:

<UnitType><UIC></UIC><Name></Name><Type></Type></UnitType>...etc

Instead of what it should be:

<UnitType><UIC>C2000</UIC><Name>N33</Name><Type>type1</Type></UnitType>....etc

--------------------
Here is an example of my XML "UnitType.xml"

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfUnitType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlsns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<UnitType>
<UIC xmlns="urn:ShipSchedules">C2000</UIC>
<Name xmlns="urn:ShipSchedules">N33</Name>
<Type xmlns="urn:ShipSchedules">type1</Type>
</UnitType>
<UnitType>
<UIC xmlns="urn:ShipSchedules">C2001</UIC>
<Type xmlns="urn:ShipSchedules">type2</Type>
</UnitType>


-------------------------
Here is my XSLT "UnitType.xsl"

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="UnitType">
<UIC><xsl:value-of select="UIC"/></UIC>
<Name><xsl:value-of select="Name"/></Name>
<Type>><xsl:value-of select="Type"/></Type>
</xsl:template>
</xsl:stylesheet>


Now, my question is this, is the reason no data values in the elements is writing to the Console bc of my xslt or because of my vb code?

Thanks in advance!!
icon_smile.gif
 
There's a typo in your xml, this "xmlsns:xsi" should be "xmlns:xsi", and the meaning of this is XML NameSpace, which is also your problem with the nodes.
They are nodes qualified by a specific namespace, which you have to import in the stylesheet and qualify (example ns is vb) when getting data.
HTML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:vb="urn:ShipSchedules">
<xsl:template match="UnitType">
<UIC><xsl:value-of select="vb:UIC"/></UIC>
<Name><xsl:value-of select="vb:Name"/></Name>
<Type>><xsl:value-of select="vb:Type"/></Type>
</xsl:template>
</xsl:stylesheet>
Your Xml data could also be written in the same manner, defining a namespace in the root node, and just qualify the nodes with the namespace identificator (ss) like this:
HTML:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfUnitType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ss="urn:ShipSchedules">
<UnitType>
<ss:UIC>C2000</ss:UIC>
<ss:Name>N33</ss:Name>
<ss:Type>type1</ss:Type>
</UnitType>
<UnitType>
<ss:UIC>C2001</ss:UIC>
<ss:Type>type2</ss:Type>
</UnitType>
</ArrayOfUnitType>
-----------------------------------------------------------------------
If all child elements is within the same namespace it is more common to only set the namespace on the parent, because you can't access the childs if don't have qualified access to the parent:
HTML:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfUnitType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ss="urn:ShipSchedules">
<ss:UnitType>
<UIC>C2000</UIC>
<Name>N33</Name>
<Type>type1</Type>
</ss:UnitType>
<ss:UnitType>
<UIC>C2001</UIC>
<Type>type2</Type>
</ss:UnitType>
</ArrayOfUnitType>
The stylesheet would then look like this:
HTML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:vb="urn:ShipSchedules">
<xsl:template match="vb:UnitType">
<UIC><xsl:value-of select="UIC"/></UIC>
<Name><xsl:value-of select="Name"/></Name>
<Type>><xsl:value-of select="Type"/></Type>
</xsl:template>
</xsl:stylesheet>
 
JohnH,

Thanks for your quick response.

In regards to your responses:
1) The typo actually isn't in the code I have been working with, it was created when I typed it into the box (I cannot copy and paste the code that I work with bc of the way the classified computer is setup)

2) What I need is a fix that requires no changes in the xml bc it is coming from a webservice and cannot be altered in the state I get it in (I know there are ways of editting xml, but I'm trying to find a way not to do this)

Any help in regards to the vb code or the xslt is what I am looking for, if possible. As I said earlier, the code works, part way bc it writes the xml schema perfectly, it just doesn't write any values of the elements (and the reason I need the xslt in the first place is bc of the occasional blank elements in the xml. I will try to modify the code per the namespace change you made and see if that makes a difference.
 
1) "xmlsns:xsi" errors out with me. It's an undefined prefix or something, or incompatible to the schema definition given by the URI.

2) disregarding 1) above, the solution is the Xsl stylesheet in first codebox in my reply.
 
XSL help

Hi,

I'm been having trouble with this xsl for the folllowing xml document. It is well formed, however, when you run it, none of the values for UIC, Name or Type appear in the results, just their blank elements. The reason I need this xsl transform is bc the xml is received from a webservice and it skips blank elements (meaning first row can have 3 elements, but if row two has an element that does not have data, it write 2 elements for that row).

At any rate, I need the actual data along with the elements to show up. If anyone can help me out with this I would GREATLY appreciate it.



<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:vb="urn:ShipSchedules">
<xsl:template match="ArrayOfUnitType">
<UnitType>
<UIC><xsl:value-of select="vb:UIC"/></UIC>
<Name><xsl:value-of select="vb:Name"/></Name>
<Type><xsl:value-of select="vb:Type"/></Type>
</UnitType>
</xsl:template>
</xsl:stylesheet>
-----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfUnitType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlsns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<?xml-stylesheet type="text/xsl" href="UnitType.xsl"?>
<UnitType>
<UIC xmlns="urn:ShipSchedules">C2000</UIC>
<Name xmlns="urn:ShipSchedules">N33</Name>
<Type xmlns="urn:ShipSchedules">type1</Type>
</UnitType>
<UnitType>
<UIC xmlns="urn:ShipSchedules">C2001</UIC>
<Type xmlns="urn:ShipSchedules">type2</Type>
</UnitType>
</ArrayOfUnitType>
 
It may be "well formed", but as I told you, it is not valid. "xmlsns:xsi" is not valid, Xml parser errors with "Reference to undeclared namespace prefix". Change it to correct "xmlns:xsi" and it works fine.
 
Just noticed that you changed the template match up one level to ArrayOfUnitType, so you have to (like that wrox dude told you earlier today :) ) address it like "UnitType/vb:UIC" instead of "vb:UIC". I also suddenly noticed that you have put a stylesheet declaration in the middle of your xml document, well that's well formed too I guess... anyway, move it up to processing-instruction level where it belongs.
 
Back
Top