Sorting using xsl:sort - sorting

I have XML file (some service documentation) looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<apis>
<api min="" max="">
<resource name="C">
<description>C from beggining to the end</description>
</resource>
</api>
<api min="2.2" max="">
<resource name="B">
<description>B from 2.2 to the end</description>
</resource>
<resource name="A">
<description>A from 2.2 to the end</description>
</resource>
</api>
</apis>
And XSL file to transform XML into html:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="api-version" select="2.2"></xsl:param>
<xsl:template match="apis">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<table border="1">
<th colspan="2">
<xsl:text>Doc for API: </xsl:text>
<xsl:value-of select="$api-version"/>
</th>
<xsl:call-template name="handleApis"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template name="handleApis">
<xsl:for-each select="api[(#min='' or #min<=$api-version) and (#max='' or #max>$api-version)]">
<xsl:for-each select="resource">
<xsl:call-template name="handleResource"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template name="handleResource">
<tr>
<td>
<xsl:value-of select="#name"/>
</td>
<td>
<xsl:value-of select="description"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
And now I want to sort results by resource atribute name.
I have tried to put
<xsl:sort select="#name" order="ascending"/>
inside for-each for resource nodes but like You know it won't sort resources between different api nodes and results is
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table border="1">
<th colspan="2">Doc for API: 2.2</th>
<tr>
<td>C</td>
<td>C from beggining to the end</td>
</tr>
<tr>
<td>A</td>
<td>A from 2.2 to the end</td>
</tr>
<tr>
<td>B</td>
<td>B from 2.2 to the end</td>
</tr>
</table>
</body>
</html>
Adding
<xsl:sort select="resource/#name" order="ascending"/>
inside for-each for api nodes also can't work because sort need only one item in select atrribute.
There is some way do sort this to get order A, B, C in output?

How about simply:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="api-version" select="2.2"/>
<xsl:template match="/apis">
<html>
<head/>
<body>
<table border="1">
<th colspan="2">
<xsl:text>Doc for API: </xsl:text>
<xsl:value-of select="$api-version"/>
</th>
<xsl:apply-templates select="api[(#min='' or #min <= $api-version) and (#max='' or #max > $api-version)]/resource">
<xsl:sort select="#name"/>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="resource">
<tr>
<td>
<xsl:value-of select="#name"/>
</td>
<td>
<xsl:value-of select="description"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>

Related

Internal Onebox show more than 3 content

I am trying to show content more than 3 for internal provider for onebox on GSA 7.4 version. By default it shows up 3 only. There is a setting "Maximum number of OneBox results per search" is a not working. Is there any other way or may be by editing xml or xslt to show more than 3 content?
Any suggestion would be greatly appreciated!!
Thanks in Advance!!
Enclsoed please find my code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template name="projects">
<xsl:variable name="URLReplace">
<xsl:value-of select="replace(title/urlLink,'search','GSASearchResults.aspx')"/>
</xsl:variable>
<table border="0" cellpadding="1" cellspacing="0">
<tbody>
<tr>
<td valign="top" width="99%">
<xsl:for-each select="MODULE_RESULT">
<font size="-1">
<a>
<xsl:attribute name="href">
<xsl:value-of select="U"/>
</xsl:attribute>
<xsl:value-of select="Title"/>
</a>
</font>
<br/>
</xsl:for-each>
</td>
</tr>
<tr>
<td colspan="2">
<nobr>
<a>
<xsl:attribute name="href">
<!--<xsl:value-of select="title/urlLink"/>-->
<xsl:value-of select="$URLReplace"/>
<!--<xsl:value-of select="concat('GSASearchResults.aspx',substring-after(//title/urlLink,'search'))"></xsl:value-of>-->
</xsl:attribute>
<b>
<!--<xsl:value-of select="title/urlText"/>-->
<xsl:value-of select="/GSP/PARAM[#name='q']/#original_value"/>
Click for More.. <!--<xsl:value-of select="$qurl"/>-->
</b>
</a>
</nobr>
</td>
</tr>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>

XSL Picture in table

I do read the relevant posts, but my code is still not working. I just want to add a avatar (png) to the players in my table.
My HTML:
<root version="2.0">
<game>
<players>
<nickname>thebeast</nickname>
<avatar><img src="/img/thebeast.png" height="30" width="30"></img></avatar>
<sum>220</sum>
</players>
<players>
<nickname>snowman</nickname>
<avatar><img src="/img/snowman.png" height="30" width="30"></img</avatar>
<sum>360</sum>
</players>
</game>
</root>
My table in xsl:
<table align = "center" border="transparent">
<tr><th>Nr.</th><th>Nickname</th><th>Collected <br/>Points</th></tr>
<xsl:for-each select = "//players">
<xsl:sort select = "sum" data-type="number" order="descending"/>
<tr>
<td><xsl:number value = "position()" format = "1."/></td>
<td><xsl:value-of select = "nickname"/></td>
<td><xsl:value-of select = "sum"/></td>
<td>
<img>
<xsl:attribute name="src">
<xsl:value-of select="/img" />
</xsl:attribute>
</img>
</td>
</tr>
</xsl:for-each>
</table>
Instead of:
<img>
<xsl:attribute name="src">
<xsl:value-of select="/img" />
</xsl:attribute>
</img>
try:
<img>
<xsl:attribute name="src">
<xsl:value-of select="avatar/img/#src" />
</xsl:attribute>
</img>
or, in short:
<img src="{avatar/img/#src}"></img>
Or, if you want to preserve the dimensions:
<xsl:copy-of select="avatar/img"/>

Sorting a table in XSLT 2.0

I have an index file, containing a list of xml files to process:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<part>
<numberGroup format="1" start="1">
<entry>
<file>04.xml</file>
<title>first file</title>
<css>stylesheet.css</css>
</entry>
<entry>
<file>05.xml</file>
<title>second file</title>
<css>stylesheet.css</css>
</entry>
<entry>
<file>06.xml</file>
<title>third file</title>
<css>stylesheet.css</css>
</entry>
.... more files
</numberGroup>
.... more NumberGroups
</part>
....more parts
</list>
every file 01.xml etc has one HTML-style table, like this:
<table class='wl'>
<tr class='header'>
<td><p>English</p></td>
<td><p>French</p></td>
</tr>
<tr>
<td><p>belly</p></td>
<td><p>ventre</p></td>
</tr>
<tr>
<td><p>leg</p></td>
<td><p>jambe</p>/td>
</tr>
... etc
</table>
I want to merge all the tables (3 in this example) into one, so I get a compete vocabulary.
So far I have this stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:result-document href="wl.xml">
<xsl:text disable-output-escaping="yes">
<!DOCTYPE html>
</xsl:text>
<html xmlns:epub="http://www.idpf.org/2007/ops" lang="nl" xml:lang="nl">
<head>
<title>
<xsl:value-of select="./title"/>
</title>
<xsl:apply-templates select="css"/>
</head>
<body>
<table>
<xsl:apply-templates select="//numberGroup[1]/entry">
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:result-document>
</xsl:template>
<xsl:template match="entry">
<xsl:apply-templates select="document(file)//table[#class='wl']/tr[not(contains(#class, 'header'))]"/>
</xsl:template>
<xsl:template match="tr">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
which merges as desired, except for the sorting.
How can I sort the generated table alphabetically, on the first column?
Replace
<xsl:apply-templates select="//numberGroup[1]/entry">
</xsl:apply-templates>
with
<xsl:apply-templates select="document(//numberGroup[1]/entry/file)//table[#class='wl']/tr[not(contains(#class, 'header'))]">
<xsl:sort select="td[1]" data-type="text"/>
</xsl:apply-templates>
then you can drop the template for entry elements.

Google Search Appliance XSLT header modification

I want to modify the XSLT in order to change home page header of the Google Search Appliance. I am unable to see my changes. I have done the following
included proxyreload=1 query parameter
Changed the home_page_header template to include my Hello world section.
<xsl:template name="home_page_header">
<input type="hidden" name="security_token" id="token">
<xsl:attribute name="value">
<xsl:value-of select="/GSP/SECURITY_TOKEN"/>
</xsl:attribute>
</input>
<table border="0" cellpadding="0" cellspacing="0">
<xsl:if test="$show_logo != '0'">
<tr>
<td rowspan="3" valign="top">
<xsl:call-template name="logo"/>
<xsl:call-template name="nbsp3"/>
</td>
</tr>
</xsl:if>
<xsl:if test="$show_top_search_box != '0'">
<tr>
<td valign="middle">
<xsl:call-template name="search_box">
<xsl:with-param name="type" select="'home'"/>
</xsl:call-template>
</td>
</tr>
</xsl:if>
</table>
<table>
<tr>
<td>Hello World</td>
</tr>
</table>
</xsl:template>
Any thoughts ?

Extract image from CDATA-xml rss file into xslt

I need show with xslt information about an xml RSS feed.
The xml source is:
<description><![CDATA[<p>
<img style="margin: 10px;
float: left;" alt="Nuevo modelo general de negocio"
src="http://mysite.es/images/figure1.jpg" width="196" height="147" />
La compañía apuesta por un marcado giro en el modelo]]>
</description>
I´m using:
<xsl:value-of select="description" disable-output-escaping="yes"/>
But the rendering is not good because I need show a resize image, with size, for example 70x70.
I´ve tried with this but its wrong:
<xsl:value-of select="replace("description","images/","images/resized/images/")"
disable-output-escaping="yes"/>
The perfect solution for me would be to extract separated, both the src property and the text from the tag.
Regards,
María
If your xml always is like your example then you should be able to use something like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xsl">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<div>
<xsl:apply-templates select="rss/channel"/>
</div>
</xsl:template>
<xsl:template match="rss/channel">
<ul>
<xsl:apply-templates select="item"/>
</ul>
</xsl:template>
<xsl:template match="item">
<xsl:variable name="item_link" select="link"/>
<xsl:variable name="item_title" select="substring-after(description, '/>')"/>
<xsl:variable name="item_image" select="substring-before(substring-after(description, 'src="'), '"')" />
<li>
<a href="{$item_link}">
<img alt="" src="{$item_image}" width="70" height="70" />
<xsl:value-of select="$item_title"/>
</a>
</li>
</xsl:template>
</xsl:stylesheet>

Resources