Solved

Xsl transform for dynamically generated xml from .NET web service

Posted on 2004-08-04
9
401 Views
Last Modified: 2013-11-19
I would like to transform the Xml document that is returned by a Web service. I've already written all the code and it compiles well but produce an empty string. To be very precise, I am not sure how to build a XPathDocument from a XmlDocument (or an XmlNode). I saw that the Transform method of (XslTransform) also accepts XPathNavigator. But the ouput is still empty.

localhost.CHWebService chservice = new localhost.CHWebService();
XmlNode xmlnode = chservice.getClearingHouseDocumentTypes("en");
                  
XslTransform transform = new XslTransform();
transform.Load(Server.MapPath("xml/chDocTypeMenu.xslt"));

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlnode.OuterXml);
XPathNavigator xpn = xmldoc.CreateNavigator();

System.IO.StringWriter sw = new System.IO.StringWriter();

transform.Transform(xpn, null, sw);
                  
string result = sw.ToString();
0
Comment
Question by:fpasquier
  • 4
  • 3
  • 2
9 Comments
 
LVL 7

Accepted Solution

by:
Ceiled earned 500 total points
ID: 11714685
I don't see anything wrong with the code you've written...the problem is most likely either in the XML being returned, or in the XSL file itself. I don't think we'll be able to figure out what's wrong without seeing both of these files (a sample of the sort of data returned by getClearingHouseDocumentTypes() that demonstrates the structure should do, but please include the entire XSL file and any other XSL files it references). if you can post the contents of those files, then I'll see what I can do.
0
 

Author Comment

by:fpasquier
ID: 11715357
Here is a sample of the xml items returned by the web service:
<DocumentTypes dico="../LanguagesResources/DocumentTypeXMLWords.xml">

<DocumentSubject id="policy" name="Policy">
<DocumentContext id="int" name="International"/>
<DocumentContext id="eu" name="EU"/>
<DocumentContext id="nat" name="National"/>
</DocumentSubject>

<DocumentSubject id="legis" name="Legislation">
<DocumentContext id="int" name="International"/>
<DocumentContext id="eu" name="EU"/>
<DocumentContext id="nat" name="National"/>
</DocumentSubject>

</DocumentTypes>

and the entire xsl transform:

<xsl:stylesheet version="1.0" xmlns="http://www.thepep.org/schema"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
    <xsl:output method="xml" encoding="utf-8" indent="yes" />
   
      <xsl:template match="DocumentTypes">
            <NavMenu>
                  <xsl:apply-templates select="DocumentSubject"/>
            </NavMenu>
    </xsl:template>
      
    <xsl:template match="DocumentSubject">
            <SubMenuItem>
                  <xsl:attribute name="link">
                        <xsl:value-of select="@id" />
                  </xsl:attribute>
                  <xsl:attribute name="title">
                        <xsl:value-of select="name" />
                  </xsl:attribute>
                  <xsl:apply-templates select="DocumentContext"/>
            </SubMenuItem>
    </xsl:template>
   
    <xsl:template match="DocumentContext">
            <MainMenuItem>
                  <xsl:attribute name="link">
                        <xsl:value-of select="@id" />
                  </xsl:attribute>
                  <xsl:attribute name="title">
                        <xsl:value-of select="name" />
                  </xsl:attribute>
            </MainMenuItem>
    </xsl:template>
</xsl:stylesheet>
0
 
LVL 7

Expert Comment

by:Ceiled
ID: 11715446
Hmmm...when I run that transform against that data, I get the following:

<NavMenu xmlns="http://www.thepep.org/schema">
  <SubMenuItem link="policy" title="">
    <MainMenuItem link="int" title="" />
    <MainMenuItem link="eu" title="" />
    <MainMenuItem link="nat" title="" />
  </SubMenuItem>
  <SubMenuItem link="legis" title="">
    <MainMenuItem link="int" title="" />
    <MainMenuItem link="eu" title="" />
    <MainMenuItem link="nat" title="" />
  </SubMenuItem>
</NavMenu>

Could the problem be that none of the generated menu items have titles? I'm unclear on whether the result string itself is empty, or the output from whatever tool you're feeding this XML to is empty. Try referencing "@name" instead of "name" in the XSL where you generate the title attributes. If this doesn't help, then I can try running the code you posted, but like I said, I'm pretty sure it should work. I assume you've verified that the web service is returning the XML you expect?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:fpasquier
ID: 11715647
You're right, I miss the @ in front of the attributes. But anyway, the string "result" still contains only "". I tried to debug and I noticed that all objects, including "xpn" have the correct values. So it means until the "transform.Transform" sentence, everything is OK. There is sthg wrong with the transform or with the StringWriter... I don't know.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11716974
ok lets get some variables out in order to find the problem.

does this properly do the transformation ?
XslTransform xsltransform = new XslTransform();
xsltransform.Load("favorite.xsl");
xsltransform.Transform("MyDocument.xml", "TransformResult.xml");

as a side note ... this http://support.microsoft.com/default.aspx?scid=kb;EN-US;313997 is worth taking a read (esp on the performance of xpathdocs vs xmldoc) ...  

0
 

Author Comment

by:fpasquier
ID: 11717983
Of course this code works ! My program has already a lot of such transformations and they all work !!
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11718296
I was just making sure they was not an error in the transformation as opposed to the method you were running the transformation.
0
 
LVL 7

Expert Comment

by:Ceiled
ID: 11719659
How big is your project? Would it be possible for you to package it up and host it somewhere so I can try debugging it myself?
0
 

Author Comment

by:fpasquier
ID: 11723653
It works!!! I have changed the namespace and it's ok.
instead of
<xsl:stylesheet version="1.0" xmlns="http://www.thepep.org/schema"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
I've written:
<xsl:stylesheet version="1.0" xmlns:ch="http://www.thepep.org/schema"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
(and of course applied all necessary modifications in tags)

Anyway thanks a lot for your contribution.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question