Copied to clipboard

Flag this post as spam?

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


  • Heather Floyd 605 posts 1004 karma points MVP 5x c-trib
    Mar 16, 2010 @ 17:43
    Heather Floyd
    0

    Convert returned XML to HTML string?

    This is very useful and I think it will help me in a project I am working on where I want to grab all the HTML from a page in my umbraco site that is using a specific template to render, then run some simple "replace()" functions on the entire HTML and output the "cleaned" HTML to the browser as a new page.

    I am able to grab the original html with no problem, and I can output it as-is and it displays fine, however, as soon as I run a "replace" against it, it converts to a "value-of" and drops all the HTML tags:

    <xsl:variable name="PageHTML">
        <xsl:if test="$url!=''">
            <xsl:copy-of select="parser.html2xml2:Htm2XmlNodeset($url)"/>
        </xsl:if>   
    </xsl:variable>

    <xsl:variable name="FixedS01">
        <xsl:copy-of select="umbraco.library:Replace($PageHTML, '%$firstname$% ', '')"/>
    </xsl:variable>

    <xsl:copy-of select="$FixedS01" />  <-- Does not output HTML, only text.

    Do you have any tips on how I could convert the $PageHTML data from a nodeset to a string, but still including the HTML tags, so I can run my replace functions on the whole "blob"?

    Thanks!

    Heather

  • dandrayne 1138 posts 2262 karma points
    Mar 16, 2010 @ 17:50
    dandrayne
    0

    I'm sure I haven't read correctly, but does

    <xsl:copy-of select="$FixedS01" disable-output-escaping="yes" /> 

    output the html?

    Dan

  • Heather Floyd 605 posts 1004 karma points MVP 5x c-trib
    Mar 16, 2010 @ 19:20
    Heather Floyd
    0

    Hi Dan,

    No, you can't use "disable-output-escaping="yes"" with "copy-of" (you get an XSLT compile error)

    The problem is that the XSLT is treating the html tags as xml tags, so it's not like a normal use of HTML inside an XSLT where you are dealing with data[@alias='bodytext'] as a string value, for instance.

    Heather

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Mar 16, 2010 @ 22:34
    Chriztian Steinmeier
    0

    Hi Heather,

    I haven't tried this package so I can not know if this works, but normal XSLT behavior (if such a thing exists) is to return the string value when using value-of or copy-of, whereas if you use the select attribute when assigning a variable, you get the expected node-set representation. So you should try to handle the possibility of $url being empty somehow before assigning the variable, and assign the variable using the select attribute:

    <xsl:variable name="PageHTML" select="parser.html2xml2:Htm2XmlNodeset($url)" />

    Let us hear if it works.

    /Chriztian

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Mar 16, 2010 @ 22:46
    Chriztian Steinmeier
    0

    Oh, someone should slap my hands for replying too quickly :-)

    What you can do is to run all of the XML'ed HTML through what's called an "Identity Transform" by applying templates to it, and then you create a couple of special templates for the elements you want to change, say you want to remove all FONT tags (and who wouldn't):

        <!-- Identity transform -->
        <xsl:template match="* | text()">
            <xsl:copy>
                <xsl:copy-of select="@*" />
                <xsl:apply-templates select="* | text()" />
            </xsl:copy>
        </xsl:template>
    
        <!-- Remove font and marquee tags (mandatory) -->
        <xsl:template match="font | FONT | marquee | MARQUEE">
            <xsl:apply-templates select="* | text()" />
        </xsl:template>
    
    /Chriztian
  • Heather Floyd 605 posts 1004 karma points MVP 5x c-trib
    Mar 17, 2010 @ 02:51
    Heather Floyd
    0

    Thanks, Chriztian, for your thoughts.

    Well, as you might have expected,

    <xsl:variable name="PageHTML" select="parser.html2xml2:Htm2XmlNodeset($url)" />

    didn't do anything different...

    I am intrigued by your templates suggestion, but not being familar with the technique, I am a bit unsure how I would lay it out... Also, there are several different things I want to remove... and the things I want to remove are just text - not specific tags...

    You see what I am trying to do is take a page that includes some merge codes related to my e-mail sending service, and I would like to have a version where those things are stripped out for online display.

    So this is the "original" page: http://www.wholewebimpact.com/1686.aspx?alttemplate=tpMailCode - and I want to create a version (using a different template) which would change, for instance, "Hi %$firstname$%! ..." to just "Hi!".

    Because the way I generate the "MailCode" version is kind of complex, pulling elements from different places, I'd rather just start with that "final" html, and replace the few bits of stuff I want to clean up.

    So...

    I tried to do this...

    ...
    <xsl:param name="currentPage"/>

    <xsl:variable name="url">
        <xsl:text>http://</xsl:text>;
        <xsl:value-of select="umbraco.library:RequestServerVariables('SERVER_NAME')"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="$currentPage/@id"/>
        <xsl:text>.aspx?alttemplate=tpMailCode</xsl:text>
    </xsl:variable>

    <xsl:template match="/">

        <xsl:variable name="PageHTML">
            <xsl:if test="$url!=''">
                <xsl:copy-of select="parser.html2xml2:Htm2XmlNodeset($url)"/>
            </xsl:if>
        </xsl:variable>



        <xsl:variable name="FindOptIn">
            <![CDATA[
            <p>This message was sent to:<br>
            <strong>%$name$% - %$email$%</strong><br>
            %$firstname$%, you can change your current contact information or subscription preferences by using these links:
            %$optoutlink$%</p>
            ]]>
        </xsl:variable>

     </xsl:template>

     <!-- Identity transform -->
            <xsl:template match="* | text()">
                    <xsl:copy>
                            <xsl:copy-of select="@*" />
                            <xsl:apply-templates select="* | text()" />
                    </xsl:copy>
            </xsl:template>

            <xsl:template match="$FindOptIn">
                    <xsl:apply-templates select="* | text()" />
            </xsl:template>

    But that generated a compile error:

    System.Xml.Xsl.XslLoadException: Unexpected token '$' in the expression.

    So, I guess I can't use my variable....

    I also tried something simpler:

            <xsl:template match="%$firstname$% ">
                    <xsl:apply-templates select="* | text()" />
            </xsl:template>

    but got the compile error:

    System.Xml.Xsl.XslLoadException: Unexpected token '%' in the expression.

    Which leads me to believe that it does want me to use tags - not text.

    I really just want to get everything into one big 'ol text blob - not any fancy node stuff..

    sigh...

    Heather

  • Heather Floyd 605 posts 1004 karma points MVP 5x c-trib
    Mar 17, 2010 @ 03:21
    Heather Floyd
    0

    Ah..

    I knew there had to be a better way to do what I was trying to do :-)

    <xsl:variable name="PageHTML">
            <xsl:if test="$url!=''">
                <xsl:value-of select="umbraco.library:RenderTemplate($currentPage/@id, 1512)"/>
            </xsl:if>
        </xsl:variable>

    Gives me all of it in my nice little string blog :-)

    Heather

Please Sign in or register to post replies

Write your reply to:

Draft