Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ryan 14 posts 35 karma points
    Aug 13, 2010 @ 20:03
    Ryan
    0

    Send xml to .net method

    I am trying to call a .Net method from an xslt like this:

    <xsl:copy-of select="Calendar.Library:TestMethod($currentPage)" />

    TestMethod just accepts the string, and then returns it back (I am only testing right now). It looks like this:

            public static string TestMethod(string data)

            {

                return data.ToString();

            }

    The problem is that the xml tags are getting stripped when the method is called. Does anyone know how to pass the full xml from the xslt to my method?

    -Thanks

  • Chriztian Steinmeier 2798 posts 8787 karma points MVP 7x admin c-trib
    Aug 13, 2010 @ 20:27
    Chriztian Steinmeier
    0

    Hi Ryan,

    I'm by no means a .NET master, but given your TestMethod requires a string, I know that the XSLT processor will provide just that, so it'll return the string representation, which is akin to using <xsl:value-of select="" /> which returns all the text nodes below that element (not entirely correct, but ok for illustration).

    You may be able to change the method signatur to take an XPathIterator or an IXmlDomNode or something similar and get access to the actual DOM of the currentPage.

    /Chriztian

     

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Aug 13, 2010 @ 20:37
    Morten Bock
    0

    As Christian suggests, try setting your input parameter to a XPathNodeIterator. Should make it possible for you to access the XML.

  • Ryan 14 posts 35 karma points
    Aug 13, 2010 @ 20:39
    Ryan
    0

    That's the problem! I switched to XPathNodeIterator and it worked. I was just using string because of its genericness and without documentation for how to do this I am left guessing. I was thinking that it would pass it as a stirng and then I would parse that into my own XmlDocument or something along those lines. Thanks for your help!

  • Ryan 14 posts 35 karma points
    Aug 13, 2010 @ 21:01
    Ryan
    0

    So, now on to the next half- I am trying to return xml to an xsl variable that will get transformed instead of the $currentPage parameter.

    I am setting the variable like this:

    <xsl:variable name="recurringEvents">

    <xsl:copy-of select="Calendar.Library:TestMethodXP($currentPage)"/>

    </xsl:variable>

     

    My method now looks like this:

            public static XPathNodeIterator TestMethodXP(XPathNodeIterator data)

            {

                return data;

            }

     

    But when I try to use xsl on recurringEvents, like this:

    <xsl:value-of select="count($recurringEvents/ancestor-or-self::root//node [@nodeTypeAlias='event'])" />

    I get:

    System.Xml.Xsl.XslTransformException: To use a result tree fragment in a path expression, first convert it to a node-set using the msxsl:node-set() function. 

    When I try to save the xslt file. I'm clearly an xslt rookie, so I don't really know what it wants me to do. But I'll bet 75 cents that I am not supposed to be returning an XPathNodeIterator in my method, since that is the part I guessed on...

    Thanks for the help guys,

    -Ryan

  • Hendrik Jan 71 posts 137 karma points
    Aug 13, 2010 @ 21:05
    Hendrik Jan
    1


    When u use copy-of in a variable or if u put elements in a variable, you need to make a node-set of that variable to use it:


    <xsl:variable name="recurringEventsBAKEMIX">

      <xsl:copy-of select="Calendar.Library:TestMethodXP($currentPage)"/>

    </xsl:variable>

    <xsl:variable name="recurringEvents" select="msxml:node-set($recurringEventsBAKEMIX)"/>


    <xsl:value-of select="count($recurringEvents/ancestor-or-self::root//node [@nodeTypeAlias='event'])" />

     

     

     

  • Ryan 14 posts 35 karma points
    Aug 13, 2010 @ 21:27
    Ryan
    0

    Progress, but not quite there... I implemented Niels' suggestion, and now there are no errors. However, the data is not right. This:

    <xsl:value-of select="count($currentPage/ancestor-or-self::root//node [@nodeTypeAlias='event'])" />

    returns "90" which is the correct result.

    This:

    <xsl:value-of select="count($recurringEvents/ancestor-or-self::root//node [@nodeTypeAlias='event'])" />

    returns "0"

    Just to confirm, this is how I am calling my method and setting the variable:

    <xsl:variable name="recurringEventsXML">

    <xsl:copy-of select="Calendar.Library:TestMethodXP($currentPage)"/>

    </xsl:variable>

    <xsl:variable name="recurringEvents" select="msxml:node-set($recurringEventsXML)" />

    And this is my .Net method, which at this point does nothing:

            public static XPathNodeIterator TestMethodXP(XPathNodeIterator data)

            {

                return data;

            }

    Right now, I am just trying to send the data through a .net method and get the same thing back. Once I have that working, I am going to use the .Net method to manipulate the data.

  • Hendrik Jan 71 posts 137 karma points
    Aug 13, 2010 @ 21:48
    Hendrik Jan
    0


    Do you get xml back if u do this?

    <xsl:copy-of select="Calendar.Library:TestMethodXP($currentPage)"/>

    Or maybe its clearer if u do this: <textarea>

    <xsl:copy-of select="Calendar.Library:TestMethodXP($currentPage)/*"/>

    </textarea> Because its possiuble yo have a wrong xpath or something

     

  • Ryan 14 posts 35 karma points
    Aug 13, 2010 @ 23:37
    Ryan
    0

    I am selecting the root node. Check this out- here is where I call my function and set the variables:

    <xsl:variable name="recurringEventsXML">

    <xsl:copy-of select="Calendar.Library:TestMethodXP($currentPage/ancestor-or-self::root)"/>

    </xsl:variable>

     

    <xsl:variable name="recurringEvents" select="msxml:node-set($recurringEventsXML)" />

    Now, when I do this:

    <xsl:copy-of select="$recurringEvents"/>

    It outputs the entire xml document, which is exactly what I am looking for. However, when I do this:

    <xsl:copy-of select="$recurringEvents/ancestor-or-self::root"/>

    My guess is that $recurringEvents is a string with tags in it, and is not being interpreted as actual XML that can be searched with XPath. 

     

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Aug 13, 2010 @ 23:56
    Morten Bock
    0

    What if you do this:

    <xsl:copy-of select="$recurringEvents/root"/>

    I suspect that your variable does not have it selector placed at the <root> element, but at the very root of the document, thus the ancestor-or-self axis will not work

  • Ryan 14 posts 35 karma points
    Aug 13, 2010 @ 23:58
    Ryan
    0

    Got it... Niels- you were on the right path (get it?!?!). This works (notice the /* in the recurringEvents):

    <xsl:variable name="recurringEventsXML">

    <xsl:copy-of select="Calendar.Library:TestMethodXP($currentPage/ancestor-or-self::root)"/>

    </xsl:variable>

     

    <xsl:variable name="recurringEvents" select="msxml:node-set($recurringEventsXML)/*">

    </xsl:variable>

  • Ryan 14 posts 35 karma points
    Aug 14, 2010 @ 00:00
    Ryan
    0

    Morten- Yes that was exactly it. I was misunderstanding what was going on with the ancestor or self. Thanks again everyone for all of the help!

Please Sign in or register to post replies

Write your reply to:

Draft