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.
Related
this is a sample of my XML output (which I cannot change because it's output by a CRM software over which I have no control):
<description><div><strong>Why</strong> is this not bold?</div> </description>
the <description> tags are proper xml tags, while <div> and <strong> are output as plain text in the pdf (see pic)
pdf output
I am trying to have them rendered properly and found this article online https://www.ibm.com/developerworks/library/x-xslfo2app/index.html but unfortunately the downloads are not available anymore. Can anyone explain to me how to apply the templates provided in the article to my XSLT file?
the XSLT for the snippet provided is currently this:
<fo:table margin-bottom="10mm">
<fo:table-column column-width="100%"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell xsl:use-attribute-sets="testBorderLeft">
<fo:block font-weight="bold">Detailed Description</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell xsl:use-attribute-sets="testBorderLeft">
<fo:block>
<xsl:value-of select="/hash/description"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
How can incorporate for example this?
<xsl:template match="strong">
<fo:inline font-weight="bold">
<xsl:apply-templates select="*|text()"/>
</fo:inline>
</xsl:template>
Coding is really not my job, but I have to do this for work, so if you could be patient and very very clear it would be great. Thanks!
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>
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.
<!-- 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 these escaped tags in my XML file: <strong> --> The whole xml is like this:
<test>
<TEST2>
<li><strong>blablablabla</li>
</TEST2>
<test>
I want to show the text in strong and bold letters (like this) and I want to show the list-item bullet. Now I only manage to show the text in strong and bold letters OR I can show the list-item bullet, but I never managed to show them both correctly. So my question is: how can I show them both in the right manner?
First, I tried <xsl:value-of select="/test/TEST2" disable-output-escaping="yes" />, which showed me the text in italic and bold version, but then, it did not output the list item bullets, because it just outputted the raw <li> tag...
Then, I tried <xsl:copy-of-select="/test/TEST">, which showed the list bullets, but this time, it also outputted the raw <strong> and <em> tags.
Besides, copy-of select, combined with disable-output-escaping did not work.
What should I do to show them both correctly?
Sample outputs:
*<em><strong>blalblblbal</strong></em> if I use the second one.
If I use the first one:
blalblblbal
Well use
<xsl:template match="li">
<xsl:copy>
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:copy>
</xsl:template>
of course with other templates for ancestors of li generating a ol or ul as needed and processing child elements e.g.
<xsl:template match="test">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
But disable-output-escaping is not supported for instance for client-side XSLT in Mozilla browsers. And generally XSLT processors do not need to support it.