Jin Ho
Jin Ho

Reputation: 3665

Xsl - How to select value from attribute whose name contains special character

My purpose is to get data from google place. I have the following snippet html:

<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 want to get value of g:rating_override. I tried with following xsl

<Rating>

  <xsl:value-of 
    select="//div[@class='rsw-pp rsw-pp-widget']/div[@class='rsw-stars']/@g:rating_override" />

 </Rating>

It said that 'System.Xml.Xsl.XsltException: Prefix 'g' is not defined'. Could you help me?

Upvotes: 0

Views: 571

Answers (2)

Simon Mourier
Simon Mourier

Reputation: 138776

If you're using the Html Agility Pack embedded XSLT tools, you're facing two hard Html Agility Pack limits (at least with version 1.3.0.0):

  • Support for Namespaces is limited
  • The XPATH implementation does not support navigating to the attributes (only selection). This XPATH "//tag1/tag2/@myatt" does not work for example.

You can overcome these limits with C# code, but not easily with pure XPATH, hence not with XSLT.

In these case, it's often easier to convert the HTML to XML using the Html Agility Pack, and then use a regular XSLT on XML with the standard .NET classes, instead of XSLT on HTML with Html Agility Pack classes.

Upvotes: 1

Richard Schneider
Richard Schneider

Reputation: 35477

You need to define the "g" namespace. Typicaly this is done on the stylesheet element.

<xsl:stylesheet 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:g=" ... "
     version="1.0">

Upvotes: 1

Related Questions