I have a requirement to import a html website to Drupal and I have decided to using Import HTML module to do it.
I have to be able to grab just the text from html page (inside tag) without the html tags.
For this, I'm trying to create a custom xsl template based on the default template: html2simplehtml.xsl.
Currently my import is working fine with html2simplehtml.xsl template.
here is example of the result node body from the import:
<div class="container-narrow">
<div class="masthead">
<ul class="nav nav-pills pull-right">
<li class="active">
Home
</li>
<li>
Applications
</li>
<li>
Middleware
</li>
now, the requirement is to only get:
Home
Applications
Middleware
I have found this to remove html tags:
<!-- This will remove the tag -->
<xsl:template name="remove-html">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '<')">
<xsl:value-of select="substring-before($text, '<')"/>
<xsl:call-template name="remove-html">
<xsl:with-param name="text" select="substring-after($text, '>')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
but I am not sure where to put and how to call it using this:
<!-- Calling the template that removes tag -->
<xsl:call-template name="remove-html">
<xsl:with-param name="text" select="{HtmlBody}"/>
</xsl:call-template>
How can I do this?
I'm not quite familiar with the way that Drupal calls your XSLT but let's assume it's a simple XSLT 1.0 processor using some HTML page as input and generating the output that you showed above. Let's further assume that the original HTML is well formed with all required closing tags, so that it's in fact XHTML which can be processed by the XSLT processor. (This is not true for the HTML you included in your question, by the way.)
So what you want to do is basically prevent all tags in the XML/XHTML input from showing up in the output. I think the easiest way to achieve this to use the <xsl:value-of select> tag. Assuming that you copy all the child tags of the <body></body> section of your XHTML like this:
<xsl:template match="body">
<xsl:copy-of select="*">
</xsl:template>
instead you could do this:
<xsl:template match="body">
<xsl:value-of select=".">
</xsl:template>
<xsl:value-of> forces the evaluation of the XML sub tree into a string which is done (simply put) by concatenating all contained text elements. This does not, however, take care of white space yet. If you want to the eliminate disturbing white space you could brace the call like this:
<xsl:template match="body">
<xsl:value-of select="normalize-space(.)">
</xsl:template>
Now for the template you originally wanted to use: This does in fact remove tags from the input, too. But if I interpret the code right the input is NOT an XML node set but it must already be a STRING. So this works for other context in which you have a literal XML representation in a string. If you tried to use it here you would have to explicitly convert your XML representation into a string beforehand by using e.g. <xsl:value-of>. In this case the template would already be stripped off the tags (as described above) and would effectively not do anything at all but return the same string that it was passed as parameter. So IMHO, you will not need this template at all.
Related
The following template fixes the img/src attribute and is there since years:
<xsl:template match="xh:img/#src">
<xsl:attribute name="src">
<xsl:value-of select="
if( string-length(substring-before(substring-after(
subsequence(parent::node()/following-sibling::comment(),1,1),'src="'),'.eps')) > 0 )
then
concat('images/',tokenize(concat(substring-before(substring-after(
subsequence(parent::node()/following-sibling::comment(),1,1),'src="'),'.eps'),'.png'),'/')[last()])
else
data(self::node())"/>
</xsl:attribute>
</xsl:template>
Now I added the following template to move width and height to style:
<xsl:template match="xh:img">
<img style="width:{#width}; height:{#height};">
<xsl:copy-of select="#*[not(name()='width' or name()='height')]"/>
</img>
</xsl:template>
The second one works but it "disables" the first one.
If I comment the second one, the first works.
Is there a way to merge them?
I really don't have experience with XSL so any help would be appreciated.
This is because you are using xsl:copy-of to copy attributes in the template matching xh:img. This will not apply any matching templates, but just copy them exactly.
Simply change to using xsl:apply-templates...
<xsl:apply-templates select="#*[not(name()='width' or name()='height')]"/>
However, you might need to add an additional template to match attributes other than src should you wish them to still be created too.
<xsl:template match="#*">
<xsl:copy />
</xsl:template>
Hi and happy year to everyone !
Once again, I need your help with a transformation in XSLT.
I have an XML-TEI file with a lot of pb elements. For instance :
<pb ed="bnf" id="f77.image.r"/>
I have also in the same file one link element :
<link id="bnf" target="http://test.com:/12148/btv1b90621925/"/>
My transformation in xslt should not only transform the <pb> element to look like this in HTML : [77r] but the [77r] should also be a hyperlink.
Now, it only does the first stuff :
<xsl:template match="pb[#ed='bnf']"><span class="pb">
<xsl:text> [</xsl:text>
<xsl:value-of select="substring-after(substring-before(#id, '.image.'), 'f')"/><xsl:value-of select="substring-after(#id, '.image.')">
</xsl:value-of>
<xsl:text></xsl:text>
<xsl:apply-templates/>
<xsl:text>] </xsl:text>
</span>
</xsl:template>
How can I reach the link element and do so the [f77r] takes the value of the link and concat it with the id of the pb element ? Every pb should be a hyperlink like this (with the end changing for each pb) :
http://test.com:/12148/btv1b90621925/f77.image.r
Thank you so much for your help :)
Best wishes,
Micha
It looks like you could use a key here, to look up the link elements by there id attribute.
<xsl:key name="links" match="link" use="#id" />
Then you can use the key to get the target of the link when you construct the href for the hyperlink
<a href="{key('links', #ed)/#target}{#id}">
Note the double-use of Attribute Value Templates to create the attribute. You could also write this if you wanted
<a href="{concat(key('links', #ed)/#target, #id)}">
Try this XSLT template
<xsl:template match="pb[#ed='bnf']">
<span class="pb">
<a href="{concat(key('links', #ed)/#target, #id)}">
<xsl:text> [</xsl:text>
<xsl:value-of select="substring-after(substring-before(#id, '.image.'), 'f')"/><xsl:value-of select="substring-after(#id, '.image.')">
</xsl:value-of>
<xsl:apply-templates/>
<xsl:text>] </xsl:text>
</a>
</span>
</xsl:template>
This should output the following:
<span class="pb">
[77r]
</span>
<!-- Section: /report/detail -->
<xsl:template match="/report/detail">
<div href="./lf_web" style="left: 0.0ex; position: absolute"><xsl:value-of select="./lf_po"/></div>
<br/>
</xsl:template>
In this code I am trying to use the content of the taq element "./lf_web".
Can any one help me please.
It is in and .xsl file and linked to .xml data source
You should tag your question with XSL instead of div and href which are not important to your question.
One way is to first retrieve the value in a variable. I like that way because you can use functions:
<xsl:variable name="website_uri" as="xs:string">
<xsl:value-of select="info/uri"/>
</xsl:variable>
<xsl:variable name="protocol" as="xs:string">
<xsl:value-of select="substring-before($website_uri, '://')"/>
</xsl:variable>
And I show how you use a variable: with a $ sign before the name of the value. However, to do so inside a different attribute, you need to put it between curly brackets;
...
Now I think you can do the query you're trying to achieve inline too, but I don't see a good example for it.
I have a XSL/XML parser to produce html code.
The xml is like this.
<root>
<a>
<url> http://image.jpg </url>
<x> 100 </x>
<y> 200 </y>
...
</a>
</root>
and the XST should be something like this. In style I want do define a background url using the value of xml node. How can I do it?
<xsl:template match="root">
<xsl:for-each select="a">
<div class="crop_image" style="background:url("<xsl:value-of select="url"/>") -<xsl:value-of select="x"/>px -<xsl:value-of select="y"/>px">"</div>
</xsl:for-each>
</xsl:template>
Thanks
What you are looking for are "Attribute Value Templates", which allow you to write values from your XML directly into attributes
<xsl:template match="root">
<xsl:for-each select="a">
<div class="crop_image" style="background:url({url}) -{x}px -{y}px">"</div>
</xsl:for-each>
</xsl:template>
The curly braces indicate an expression to be evaluated, rather than output literally, so {url} for example will be replaced with http://image.jpg in your output.
I have been using abit of xslt to style my xml into something readable. However there is one thing I have not been able to figure out.
I was woundering how you can apply stylying to the text inside the xml elemnts. for instance this is what part of my xml looks like
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="mystylesheet.xsl" type="text/xsl"?>
<Collection>
<Tals>
<Indent="0">Weapon Training</Talent>
<Cost>1</Cost>
<Description>Confers <b>proficiency</b> of <i>two weapons</i>, either melee or ranged. This talent make be aquired multiple times</Description>
</Tals>
I would like to know how I could get my description element to output in a html format.. so ou can see the bold text and italic text.
This is how I am catching my Description element from my xml in mystylesheet.xsl
Description: </b><xsl:value-of select="Description"/>
any help would be greatly appreciated.
If my understanding is right you like to copy the content of Description.
This could easily done by changing the <xsl:value-of select="Description"/> to
<xsl:apply-templates select="Description/node()"/>
To make it work you have also to add an "identity transform template"
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
Update:
Alternative you can also use
<xsl:copy-of select="Description/node()"/>
But the "identity transform template" is the better solution, because it is possible to add still more specialized templates.