Solved

Xsl transform for dynamically generated xml from .NET web service

Posted on 2004-08-04
9
394 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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 …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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)

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now