Sorting a table in XSLT 2.0 - sorting

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.

Related

Is there a way to conditionally sort in xslt?

I need to sort some content but only when an attribute is equal to CAT. I think I should be able to pass a property from my ant build file to the use-when attribute but it is not working. Any help would be appreciated
Here is the xslt that I have:
<xsl:for-each select="document(#fileRef)/foo/bar">
<xsl:sort select="translate(child::Title/text(), '>', '')" order="ascending" use-when="system-property('customerCode')='CAT'"
collation="http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive"/>
<!-- do some stuff here -->
</xsl:for-each>
Using oXygen I got the following to work in an Ant file:
<xslt in="sample1.xml" out="sample1-transformed.xml" force="true" style="sheet1.xsl">
<factory name="net.sf.saxon.TransformerFactoryImpl"/>
<sysproperty key="cat" value="bar"/>
</xslt>
XML sample1.xml is e.g.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>c</item>
<item>a</item>
</root>
XSLT is
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:comment select="system-property('cat')"/>
<xsl:next-match/>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="item">
<xsl:sort select="." use-when="system-property('cat') = 'foo'"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
and then the output items are sorted only if the Ant sets e.g. <sysproperty key="cat" value="foo"/>.

XSLT IF evaluation rules

From the XML file :
<store >
<tools>
<tool IDT="T1">
<container>B1</container>
<container>B2</container>
</tool>
<tool IDT="T2">
<container>B1</container>
</tool>
<tool IDT="T3">
<container>B2</container>
</tool>
</tools>
<boxes>
<box IDB="B1" height="10" width="20" length="30" weight="4"/>
<box IDB="B2" height="5" width="40" length="30" weight="2"/>
</boxes>
</store>
I try to display for each box the list of tools that go into each box. For that, I wrote the following XSL:
<?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="html"
encoding="UTF-8"
doctype-public="-//W3C//DTD HTML 4.01//EN"
doctype-system="http://www.w3.org/TR/html4/strict.dtd"
indent="yes" />
<xsl:template match="/">
<html>
<head>
<title>Boxes contents</title>
<link type="text/css" rel="stylesheet" href="styles.css" />
</head>
<body>
<h1>Boxes contents</h1>
<ul>
<xsl:apply-templates select="/store/boxes/box" />
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="box" >
<li><xsl:text>Box </xsl:text>
<xsl:value-of select="#ID"/>
<xsl:text>contains the following tools : </xsl:text>
</li>
<xsl:call-template name="findTools" >
<xsl:with-param name="currentBOX" select="#IDB"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="findTools" >
<xsl:param name="currentBOX" />
<xsl:for-each select="/store/tools/tool/container" >
<xsl:if test="container = $currentBOX" >
<br><xsl:value-of select="#IDT"/></br>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
When I do it, I never see the tools. In debug under OXYGEN, I see that the IF is never true. I do not understand why? I start in XPath and XSLT, thanks for your help
You already are at a <container> element inside the <xsl:for-each>. There are no children, so selecting another <container> inside the <xsl:if> won't return anything.
You mean to execute your check from the <tool> node.
<xsl:for-each select="/store/tools/tool">
<xsl:if test="container = $currentBOX">
<xsl:value-of select="#IDT"/><br />
</xsl:if>
</xsl:for-each>
which is easier written as
<xsl:for-each select="/store/tools/tool[container = $currentBOX]">
<xsl:value-of select="#IDT"/><br />
</xsl:for-each>
Overall a more straight-forward way to write the two templates would be this:
<xsl:template match="box">
<li>
<xsl:text>Box </xsl:text>
<xsl:value-of select="#ID"/>
<xsl:text>contains the following tools : </xsl:text>
</li>
<xsl:apply-templates select="/store/tools/tool[container = current()/#IDB]" />
</xsl:template>
<xsl:template match="tool">
<xsl:value-of select="#IDT"/><br />
</xsl:template>
And alternatively you can use an <xsl:key> to index <tool> elements by their <container> value:
<xsl:key name="kToolByContainer" match="/store/tools/tool" use="container" />
<xsl:template match="box">
<li>
<xsl:text>Box </xsl:text>
<xsl:value-of select="#ID"/>
<xsl:text>contains the following tools : </xsl:text>
</li>
<xsl:apply-templates select="key('kToolByContainer', #IDB)" />
</xsl:template>
<xsl:template match="tool">
<xsl:value-of select="#IDT"/><br />
</xsl:template>

Sorting using xsl:sort

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>

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>

Output Context Node (full path) in XSLT 1.0?

For debugging purposes it would be handy to output the full path of the context node from within a template, is there unabbreviated xpath or function to report this ?
Example Template:
<xsl:template match="#first">
<tr>
<td>
<xsl:value-of select="??WHAT TO PUT IN HERE??"/>
</td>
</tr>
</xsl:template>
Example (Abridged) input document:
<people>
<person>
<name first="alan">
...
The output from the template would be something like:
people / person / name / #first
Or something similar.
This transformation produces an XPath expression for the wanted node:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:variable name="vNode" select=
"/*/*[2]/*/#first"/>
<xsl:apply-templates select="$vNode" mode="path"/>
</xsl:template>
<xsl:template match="*" mode="path">
<xsl:value-of select="concat('/',name())"/>
<xsl:variable name="vnumPrecSiblings" select=
"count(preceding-sibling::*[name()=name(current())])"/>
<xsl:variable name="vnumFollSiblings" select=
"count(following-sibling::*[name()=name(current())])"/>
<xsl:if test="$vnumPrecSiblings or $vnumFollSiblings">
<xsl:value-of select=
"concat('[', $vnumPrecSiblings +1, ']')"/>
</xsl:if>
</xsl:template>
<xsl:template match="#*" mode="path">
<xsl:apply-templates select="ancestor::*" mode="path"/>
<xsl:value-of select="concat('/#', name())"/>
</xsl:template>
</xsl:stylesheet>
when applied on the following XML document:
<people>
<person>
<name first="betty" last="jones"/>
</person>
<person>
<name first="alan" last="smith"/>
</person>
</people>
the wanted, correct result is produced:
/people/person[2]/name/#first
Here's a stylesheet (of dubious value) that prints the path to every element and attribute in a document:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:strip-space elements="*" />
<xsl:template match="*">
<xsl:param name="pathToHere" select="''" />
<xsl:variable name="precSiblings"
select="count(preceding-sibling::*[name()=name(current())])" />
<xsl:variable name="follSiblings"
select="count(following-sibling::*[name()=name(current())])" />
<xsl:variable name="fullPath"
select="concat($pathToHere, '/', name(),
substring(concat('[', $precSiblings + 1, ']'),
1 div ($follSiblings or $precSiblings)))" />
<xsl:value-of select="concat($fullPath, '
')" />
<xsl:apply-templates select="#*|*">
<xsl:with-param name="pathToHere" select="$fullPath" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="#*">
<xsl:param name="pathToHere" select="''" />
<xsl:value-of select="concat($pathToHere, '/#', name(), '
')" />
</xsl:template>
</xsl:stylesheet>
When applied to this input:
<people>
<person>
<name first="betty" last="jones" />
</person>
<person>
<name first="alan" last="smith" />
</person>
<singleElement />
</people>
Produces:
/people
/people/person[1]
/people/person[1]/name
/people/person[1]/name/#first
/people/person[1]/name/#last
/people/person[2]
/people/person[2]/name
/people/person[2]/name/#first
/people/person[2]/name/#last
/people/singleElement

Resources