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?
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">
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.
Related
I have several html files starting with a title and the author name, but I don't want them in the table of content. I used remove toc from toc in wkhtmltopdf to hard code the value of the h1/h2 to remove but I would like the xlst toc file to be independent of the name of the document and the author.
So I gave these specific titles a class attribute. The problem lies in the xlst filter, I didn't find a way to test or extract the class attribute.
Here is a part of the html file :
<h1 class="title">Me</h1>
<h2 class="author">My Title</h2>
Here is the xslt toc file part :
<xsl:template match="outline:item">
<li>
<xsl:if test="(#title!='') and (#title!='My little TOC')and (#class!='author')and (#class!='title')">
I'm a total newbie to xslt and don't know what outline:item really is, but it seems that it doesn't get the original class attribute. How could I get the job done ?
One simple solution is to use div tags instead of headings. In your CSS, make sure you specify display: block;.
if you run the
--dump-outline toc.xml
flag when you generate the pdf and look at the xml file you will see the xml nodes and attributes.
You can then test for either the title, page number, link and backlink the document. You can use these attributes for your if statment.
For example:
<xsl:if test="((#page!=1) and (#page!=2) and (#page!=5))">
blah blah blah
</xsl:if>
Note the brackets around the full test.
You can even then nest the if statements further if needed:
<xsl:if test="((#page!=1) and (#page!=2) and (#page!=5))">
<xsl:if test="(#title!='A title')">
more code
</xsl:if>
</xsl:if>
I am trying to apply regex pattern like this.
I want to apply pattern like this.
<a attributes="some set of attributes"><img attributes="some set of attribtes"/></a>
Rules:
<a> tag with attributes followed by <img> with attributes.
Sample Valid Data:
<a xlink:href="some link" title="Image" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/1999/xhtml">
<img alt="No Image" title="No Image" xlink:href="soem path for image" xlink:title="Image" xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" />
</a>
Invalid:
<a>data<img/></a>--Data Present, no attributes
<a><img>abcd</img></a>--data Present, No attributes
<a><img/></a>---No attributes
Can any one suggest how to write pattern for this.
Thank you.
You can do this in a completely bulletproof manner with XPath:
//*[local-name()='a' and count(#*)>0 and *[local-name()='img' and count(#*)>0] and count(.//*)=1 and normalize-space(.)='']
This selects all elements with a local name of 'a' which have no non-significant text content, attributes, and a single 'img' element with attributes.
However, since your example code is clearly XML with namespaces and all, perhaps you can reformulate your question to say what your overall task is instead of "what regex should I use". At the very least it seems that perhaps you should be paying attention to those namespaces instead of treating namespace declarations as attributes.
For example, maybe what you really mean is this?
//xhtml:a[#xlink:href and xhtml:img[#xlink:href] and count(.//*)=1 and normalize-space(.)='']
I’m using Umbraco 4.7.0
My goal is to get the image path from a hard coded media node id of 4191. If I create a new macro with the code:
<xsl:copy-of select="umbraco.library:GetMedia(4191, false())"/>
I get the output:
/media/17675/my image.jpg50033618497jpg
I was expecting some well formed xml, however, it appears I’m missing all the tags. I therefore cannot reference the path for the image directly.
Am I missing something really simple here?
EDIT
I discovered how to get the raw xml output from my copy-of statement. I needed to wrap it in a <textarea> tag:
<textarea>
<xsl:copy-of select="umbraco.library:GetMedia(4191, false())"/>
</textarea>
This should do it:
<xsl:copy-of select="umbraco.library:GetMedia(4191, 0)/umbracoFile"/>
See also http://our.umbraco.org/wiki/reference/umbracolibrary/getmedia
I'm trying to add a tag like
<div itemscope>
in my xslt transformation but I get an error.(The expected token is '=')
I'm working in C# .net 4.0 xslt 1.0.
XSLT is optimized for generating XML output. HTML5 is, in general, not XML. The syntax
<div itemscope>
is clearly not XML and therefore can't be generated using xsl:attribute. This is because XML doesn't support empty-valued attributes. That's the bad news.
The good news: There are two ways of writing the same thing as <div itemscope> that are valid HTML5 and valid XML:
<div itemscope="">
<div itemscope="itemscope">
So pick your favorite and generate that!
<xsl:text disable-output-escaping="yes"><![CDATA[<div itemscope>]]></xsl:text>
Lets say I have the following code snippet below, how do I also apply the disable-output-escaping to the {name} in the title attribute?
<a title="{name}"><xsl:value-of select="name" disable-output-escaping="yes" /></a>
This has really got me stumped.
Thanks guys.
This cannot be done with XSLT. The spec says:
It is an error for output escaping to
be disabled for a text node that is
used for something other than a text
node in the result tree.
Thus it makes no difference if you use Attribute Value Templates or xsl:attribute with xsl:value-of, because you're generating an attribute node, not a text node. It's a limitation in the language.
You can't as is. The {name} shortcut doesn't allow additional parameters. Use the <xsl:attribute> tag instead.