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: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"?
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.
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:
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):
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.
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.
<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>
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
I'm sure I haven't read correctly, but does
<xsl:copy-of select="$FixedS01" disable-output-escaping="yes" />
output the html?
Dan
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
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
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):
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...
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:
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
Ah..
I knew there had to be a better way to do what I was trying to do :-)
Gives me all of it in my nice little string blog :-)
Heather
is working on a reply...