[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Xsl transform for dynamically generated xml from .NET web service

Posted on 2004-08-04
9
Medium Priority
?
422 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 1000 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
Industry Leaders: 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!

 

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

Industry Leaders: 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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…
Suggested Courses

649 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