Solved

Xsl transform for dynamically generated xml from .NET web service

Posted on 2004-08-04
9
404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML - Color not displaying correctly in EMAIL. 6 55
bound data table problem 2 41
Where is this file? 3 33
Making exceptions for ValidationAttribute 2 27
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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 learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 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