XSL - How to select innerhtml of a node

html-agility-pack xpath xslt

Question

I have following html snippet:

<div class="rsw-pp rsw-pp-widget">
  <div g:type="AverageStarRating" g:secondaryurls="http://maps.google.com/?cid=12948004443906002997" g:decorateusingsecondary="http://maps.google.com/?cid=12948004443906002997" g:groups="maps" g:rating_override="2.998000" class="rsw-stars ">   </div>
</div>

I'd like to get innerhtml of the first div. The expected result is:

<div g:type="AverageStarRating" g:secondaryurls="http://maps.google.com/?cid=12948004443906002997" g:decorateusingsecondary="http://maps.google.com/?cid=12948004443906002997" g:groups="maps" g:rating_override="2.998000" class="rsw-stars ">     </div>

How can I do that? Ex <xsl:value-of select="//div[@class='rsw-pp rsw-pp-widget']/html()" /> This will not work because there's no html() function. Could anyone help me?

Accepted Answer

Use:

//div[@class='rsw-pp rsw-pp-widget']/node()

This selects any node (element, text-node, processing instruction or comment-node) that is a child of any element selected by the expression //div[@class='rsw-pp rsw-pp-widget'].

XSLT verification:

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "//div[@class='rsw-pp rsw-pp-widget']/node()"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document (corrected to be wellformed):

<div class="rsw-pp rsw-pp-widget">
    <div xmlns:g="g:g"
        g:type="AverageStarRating"
        g:secondaryurls="http://maps.google.com/?cid=12948004443906002997"
        g:decorateusingsecondary="http://maps.google.com/?cid=12948004443906002997"
        g:groups="maps" g:rating_override="2.998000" class="rsw-stars "></div>
 </div>

selects and outputs exactly the wanted nodes:

    <div xmlns:g="g:g" g:type="AverageStarRating"
 g:secondaryurls="http://maps.google.com/?cid=12948004443906002997" 
 g:decorateusingsecondary="http://maps.google.com/?cid=12948004443906002997" 
 g:groups="maps" g:rating_override="2.998000" class="rsw-stars "/>

Popular Answer

As you've found, XSLT does not have an html() method. Your select statement is just about right. If you remove the /html() you'll have selected the <div> element you're and using <xsl:value-of> will output all of the node's contents.

If you're using thsi in the context of the HtmlAgilityPacl (as you've tagged it) then follow Oded's approach (with a slightly modified select):

var outerDivNode = doc.DocumentNode.SelectSingleNode("//div[@class='rsw-pp rsw-pp-widget']");
var innerDivText = outerDivNode.InnerHtml;



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why