XML/XSLT -I think I'm going to put a hammer throught the screen!

Hi Everyone,
   for the sake of my monitor can someone please help me with an xml/xsl problem that I'm having.

I have the following xml file (really simple stuff):

<root rname="Directory">
<company name="XYZ Inc.">
 <addressLines addressdata="One Abc Way">
  <address2 val="his avenue"></address2>
  <city cname="Tech city"></city>
  <country cname="Neverland"></country>
 </addressLines>
</company>
<company name="ABC Inc.">
 <addressLines addressdata="One Abc Way">
  <address2 val="his avenue">Address Line2</address2>
  <city cname="Tech city">Derbyshire</city>
  <country cname="Neverland">Nverland</country>
 </addressLines>
</company>
</root>

as you can see, not rocket science.
I have the required code to read it in and populate a treeview - and it works :-)

I then have the following code (on a button) to load the data and transform it (hahahahahaa!!!!! - I wish)

string file1, file2;
od1.ShowDialog();
file1 = od1.FileName;
od1.ShowDialog();
file2 = od1.FileName;

XmlDocument xd = new XmlDocument();
xd.Load(file1);
XmlElement root = xd.DocumentElement;
XPathNavigator nav = root.CreateNavigator();
XslTransform xslt =
new XslTransform();
xslt.Load(file2);
XmlTextWriter writer =
new XmlTextWriter("books.xml", null);
xslt.Transform(nav,
null, writer, null);
FillTree(xd.DocumentElement, tv1.Nodes);


The filltree method is mine and merely populates the treeview.
For the above transformation I'm using the following xsl data:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" />  
 <xsl:template match="/">
  <xsl:for-each select="/ROOT/company"> 
   <xsl:value-of select="./city"/>
   <xsl:value-of select="./country"/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

So as you can probably imaine I'm expecting to see the city and country of each company.

ufortunaetly when I run this code the transformation has not occurred and books.xml is empty - can anyone tell me why it's not working before I use the keyboard as a monitor battering stick

thanks in advance,
Ed.



Answer this question

XML/XSLT -I think I'm going to put a hammer throught the screen!

  • Jean Philppe HERLOIN

    You're code is fine.  Your XSLT is wrong.

    Here is a snippit that works..

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="xml" />

    <xsl:template match="/">

    <xsl:for-each select="/root/company">

    <company>

    <CompanyName><xsl:value-of select="@name"/></CompanyName>

    <Address><xsl:value-of select="addressLines/address2/@val"/></Address>

    <City><xsl:value-of select="addressLines/city/@city"/></City>

    <Country><xsl:value-of select="addressLines/country/@country"/></Country>

    </company>

    </xsl:for-each>

    </xsl:template>

    </xsl:stylesheet>

    It translates your xml into..

    <company>
      <CompanyName>whatever</CompanyName>
      <Address>whatever</Address>
    ...
    </company>

    Shaun Bedingfield
    blogsb.blogspot.com
    shaunbed@houston.rr.com


  • Tico

    Your capitalization of root is different, it's lowercase in the XML and uppercase in the XSL.

    -Frank


  • I-DotNET

    Shaun (or anyone else who knows the answer),
        how do I specify the value of an element as an attribute

    so for example I currently have the line:

    <CompanyName><xsl:value-of select="@name"/></CompanyName>

    if I try and specify this as an attribute:

    <CompanyName val=<xsl:value-of select="@name"/>></CompanyName>

    or

    <CompanyName val="<xsl:value-of select="@name"/>"></CompanyName>

    it doesn't like it.
    The reason I'm trying to do this is because in my treeview I dont want to see a folder for company and then have to click it to get the company name.
    If I use just the attribute I can set the node to the company name and there's no click down just to see the name.

    thanks again,
    Ed.

  • GSXP

    Hi everyone,
       thanks for the help, much appreciated.

    If there's anyone out there using this as a working example there's just one change that's required to the xslt:

    <name of the new root node>
    <xsl:for-each select="/root/company">
    ...
    ...
    ...
    ...
    </xsl:for-each>
    </name of the new root node>

    aside from that Shaun was bang on the money, it was an xslt error.

    It's surprising that I couldn't find this type of info anywhere else (using c# to go from xml to xml).

    once again, thanks to everyone who had a look and especially to the guys who helped,
    Ed.

  • Ami06

    Hi all,
       don't worry, I've found how it's done (I think this just shows my lack of xlst knowledge).

    for those of you who are learning as well here's the required code:

    <company><xsl:attribute name="value"><xsl:value-of select="@name"/></xsl:attribute>


    hope this helps someone else out there,
    Ed.

  • XML/XSLT -I think I'm going to put a hammer throught the screen!