Solved

Xsl transform for dynamically generated xml from .NET web service

Posted on 2004-08-04
9
396 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why Does This Page Not Show Up? 4 24
Resolve Dependency Issues 4 48
Word Directory is not in the drop down list 4 26
Turn on intranet settings 1 42
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 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

23 Experts available now in Live!

Get 1:1 Help Now