How this style sheet can produce HTML output - html

How to complete the following style sheet that should produce HTML output in figure 1.
I have styled some part of it but could not make it exactly the same as in figure 1. I have tried "copy element" in XSL but gave me duplicate results.
This is my XML:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="research.xsl"?>
<ResearchGroups xmlns="http://www.sam.com/sam.html">
<Group name="Intelligent Systems Group" id="ISG"> The Intelligent
Systems Group pursues internationally-leading research in a wide
range of intelligent systems. </Group>
<Group name="Robotics" id="RBT"> The Essex robotics group is one of
the largest mobile robotics groups in the UK. </Group>
<Staff name="Callaghan, Vic" title="Professor" groups="ISG RBT">
Intelligent environments and robotics. </Staff>
<Staff name="Gu, Dongbing" title="Dr" groups="RBT"> Multi-agent
and distributed control systems. </Staff>
</ResearchGroups>
My XSLT:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rg="http://www.sam.com/sam.html"
xmlns="http://wwww.w3.org/1999/xhtml"
version="2.0"> <xsl:template match="/">
<html>
<head> <title>Research Groups</title> </head>
<body> <xsl:apply-templates select="//rg:Group"/> </body>
</html> </xsl:template> <xsl:template match="rg:Group">
<xsl:variable name="ID" select="#id"/>
<h3> <a name="{$ID}"> <xsl:value-of select="#name"/> </a> </h3>
<p> <xsl:value-of select="text()"/> </p>
</xsl:template>
</xsl:stylesheet>
HTML Figure 1
the XSLT style sheet should output the following HTML

Something like this I think:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rg="http://www.sam.com/sam.html" xmlns="http://wwww.w3.org/1999/xhtml" version="2.0">
<xsl:template match="/">
<html>
<head>
<title>Research Groups</title>
</head>
<body>
<xsl:apply-templates select="//rg:Group"/>
</body>
</html>
</xsl:template>
<xsl:template match="rg:Group">
<xsl:variable name="ID" select="#id"/>
<h3>
<a name="{$ID}">
<xsl:value-of select="#name"/>
</a>
</h3>
<p>
<xsl:value-of select="text()"/>
</p>
<ul>
<xsl:apply-templates select="//rg:Staff[contains(#groups, current()/#id)]">
<xsl:with-param name="curGroup"><xsl:value-of select="#id"/></xsl:with-param>
</xsl:apply-templates>
</ul>
</xsl:template>
<xsl:template match="rg:Staff">
<xsl:param name="curGroup"/>
<li>
<xsl:value-of select="#name"/>
<xsl:text>: </xsl:text>
<xsl:value-of select="text()"/>
<xsl:if test="//rg:Group[(#id != $curGroup) and contains(current()/#groups, #id)]">
<xsl:text> ( </xsl:text>
<xsl:apply-templates select="//rg:Group[(#id != $curGroup) and contains(current()/#groups, #id)]" mode="otherGroups"/>
<xsl:text> ) </xsl:text>
</xsl:if>
</li>
</xsl:template>
<xsl:template match="rg:Group" mode="otherGroups">
<a href="#{#id}">
<xsl:value-of select="#id"/>
</a>
</xsl:template>
</xsl:stylesheet>

Related

XSL: xsl:if test not working and calculate sum

For the below simplified transformation sheet, I'd like to add the following functions:
For "Turnover 2019" and "Turnover 2020" I'd like to get the values below 1mio in a red badge and over 1mio in a green badge. I tried to implement this for "Turnover 2019", however I just get a blank output. The problem seems to be with the <xsl:if test="...">-part, as I do get the correct value if I just enter <xsl:value-of select="key('keyToCreditcard', id)/turnover_2019"/> (see "Turnover 2020").
For "Total Turnover" I need to sum up the values of "Turnover 2019" and "Turnover 2020". How do I do that?
XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="keyToPerson" match="person" use="id"/>
<xsl:key name="keyToCreditcard" match="creditcard" use="cardowner"/>
<xsl:template match="/">
<xsl:for-each select="data/persons/person">
<xsl:if test="turnoverYear1 < 1000000">
<span class="badge bg-warning mx-2"><xsl:value-of select="key('keyToCreditcard', id)/turnoverYear1"/></span>
</xsl:if>
<xsl:if test="turnoverYear1 > 999999.99">
<span class="badge bg-danger mx-2"><xsl:value-of select="key('keyToCreditcard', id)/turnoverYear1"/></span>
</xsl:if>
<xsl:value-of select="key('keyToCreditcard', id)/turnoverYear2"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Input:
<data>
<persons>
<person>
<id>1</id>
<name>Jeramie Bischoff</name>
<city>Luzern</city>
<birthdate>04/16/1951</birthdate>
</person>
</persons>
<creditcards>
<creditcard>
<id>1</id>
<cardtype>Visa</cardtype>
<cardnumber>4041592612048990</cardnumber>
<turnoverYear1>952411.33</turnoverYear1>
<turnoverYear2>6135840.0</turnoverYear2>
<cardowner>1</cardowner>
</creditcard>
</creditcards>
</data>
Expected Output:
Name:Jeramie Bischoff
City: Luzern
birthdate: 04/16/1951
Card Type: Visa
Card Number: 4041592612048990
Turnover 2019: 952411.33
Turnover 2020: 6135840.0
Total Turnover: 7088251.33
Based on your sample XML, a good approach would be to split things up into multiple templates.
One to handle creating the basic HTML document structure (match="/"), one to handle <person> elements, one to handle the turnover badges, and so on. In general it's beneficial to prefer <xsl:apply-templates> over cramming everything into a single template with a bunch of nested <xsl:for-each>.
Since you have an <xsl:key> that links person IDs to credit cards, use that to fetch the <creditcard> into a variable ($cc) and work with it.
<xsl:output method="html" indent="yes" />
<xsl:key name="keyToCreditcard" match="creditcard" use="cardowner"/>
<xsl:template match="/">
<html>
<!-- ... -->
<xsl:apply-templates select="data/persons/person" />
<!-- ... -->
</html>
</xsl:template>
<xsl:template match="person">
<div class="person">
<xsl:variable name="cc" select="key('keyToCreditcard', id)" />
<!-- your question #1 -->
<div>
<xsl:text>Turnover 2019: </xsl:text>
<xsl:apply-templates select="$cc/turnoverYear1" />
</div>
<div>
<xsl:text>Turnover 2020: </xsl:text>
<xsl:apply-templates select="$cc/turnoverYear2" />
</div>
<!-- your question #2 -->
<div>
<xsl:text>Total Turnover: </xsl:text>
<xsl:value-of select="$cc/turnoverYear1 + $cc/turnoverYear2" />
</div>
</div>
</xsl:template>
<xsl:template match="turnoverYear1|turnoverYear2">
<span>
<xsl:attribute name="class">
<xsl:text>badge mx-2 </xsl:text>
<xsl:choose>
<xsl:when test=". < 1000000">bg-warning</xsl:when>
<xsl:when test=". < 99999.99">bg-danger</xsl:when>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="." />
</span>
</xsl:template>
which results in
<html>
<div class="person">
<div>Turnover 2019: <span class="badge mx-2 bg-warning">952411.33</span></div>
<div>Turnover 2020: <span class="badge mx-2">6135840.0</span></div>
<div>Total Turnover: 7088251.33</div>
</div>
</html>
Because the year numbers and XML element names are a moving target in your XML, you would have to keep adjusting the XSLT code every year. That's completely unnecessary. It's a lot smarter to keep the year out of the XML element names. Move it into an attribute:
<creditcards>
<creditcard>
<id>1</id>
<cardtype>Visa</cardtype>
<cardnumber>4041592612048990</cardnumber>
<turnover year="2019">952411.33</turnover>
<turnover year="2020">6135840.0</turnover>
<cardowner>1</cardowner>
</creditcard>
</creditcards>
With a <creditcard> setup like this, the XSLT code gets more generic.
<xsl:template match="person">
<div class="person">
<xsl:variable name="cc" select="key('keyToCreditcard', id)" />
<!-- your question #1 -->
<xsl:apply-templates select="$cc/turnover" />
<!-- your question #2 -->
<div>
<xsl:text>Total Turnover: </xsl:text>
<xsl:value-of select="sum($cc/turnover)" />
</div>
</div>
</xsl:template>
<xsl:template match="turnover">
<div>
<xsl:value-of select="concat('Turnover ', #year, ': ')" />
<span>
<xsl:attribute name="class">
<xsl:text>badge mx-2 </xsl:text>
<xsl:choose>
<xsl:when test=". < 1000000">bg-warning</xsl:when>
<xsl:when test=". < 99999.99">bg-danger</xsl:when>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="." />
</span>
</div>
</xsl:template>

structure, under certain conditions

I'm trying my hand at html but I don't know how to do it. That's my but doesnt work:(:
<xsl:template match="*[contains(local-name(), '.')]">
<xsl:element name="{translate(local-name(), '.', '_')}" namespace="{namespace-uri()}">
<xsl:apply-templates select="#* | node()"/>
</xsl:element>
</xsl:template>
Does anyone have an idea how to deal with it?
It seems a recursive grouping problem though the single example doesn't really spell out when to wrap and/or nest items as lists; nevertheless with XSLT 2 or 3 it could be tackled with a recursive function using for-each-group:
<?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"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="html" indent="yes" html-version="5"/>
<xsl:function name="mf:wrap" as="element()*">
<xsl:param name="elements" as="element()*"/>
<xsl:param name="level" as="xs:integer"/>
<xsl:for-each-group select="$elements" group-adjacent="boolean(self::*[matches(local-name(), '^li[' || $level || '-9]+$')])">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<ul class="li{$level}">
<xsl:sequence select="mf:wrap(current-group(), $level + 1)"/>
</ul>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:function>
<xsl:template match="*[matches(local-name(), '^li[0-9]+$')]">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
<xsl:template match="*[*[matches(local-name(), '^li[0-9]+$')]]">
<div>
<xsl:apply-templates select="mf:wrap(*, 1)"/>
</div>
</xsl:template>
<xsl:template match="uz">
<h5>
<xsl:apply-templates/>
</h5>
</xsl:template>
<xsl:template match="/">
<html>
<head>
<title>.NET XSLT Fiddle Example</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/bnnZWK
Note that the list structure the above creates is a bit different, all liX items of the same X level are wrapped into a single ul class="liX" wrapper while your wanted sample at some places seems to wrap several items and at other places wrap only single items.

xpath with xsl:when test

I need of display BIG on planets with diameter higher than average diameter or SMALL without using avg function (XSLT 1.0)
I have tried to use xsl:when with a condition like diameter > sum(....) div count(nom), but it doesn't work :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/systeme_solaire">
<html lang="fr">
<head>
<title>Les planètes</title>
</head>
<body>
<xsl:apply-templates select="planete[nom!='Terre']">
<xsl:sort select ="diametre" order="descending" data-type="number" />
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="planete" >
<ul>
<p><b><xsl:value-of select="nom"/> : </b></p>
<li>Distance au soleil: <xsl:value-of select="distance"/><xsl:value-of select="distance/#unit"/></li>
<li>Masse: <xsl:value-of select="masse"/> <xsl:value-of select="masse/#unit"/></li>
<li>
<xsl:choose>
<xsl:when test="diametre > ((sum(diametre[unit='diamètre terrestre']*sum(diametre[unit='km']))+sum(diametre[unit='km'])) div count(nom))">
BIG
</xsl:when>
<xsl:otherwise>
SMALL
</xsl:otherwise>
</xsl:choose> Diamètre: <xsl:value-of select="diametre"/> <xsl:value-of select="diametre/#unit"/></li>
<xsl:if test="satellite>0"><li>Nombre de satellites: <xsl:value-of select="satellite"/></li></xsl:if>
</ul>
</xsl:template>
</xsl:stylesheet>
XML file used (diameter of planets differents from earth are defined according to earth diameter ratio) :
<?xml version="1.0" encoding="ISO-8859-1" ?>
<systeme_solaire>
<planete type="tellurique">
<nom>Vénus</nom>
<distance unit="UA" >0.7</distance>
<masse unit="masse terrestre">0.8</masse>
<diametre unit="diamètre terrestre">0.9</diametre>
</planete>
<planete type="tellurique">
<nom>Terre</nom>
<distance unit="km" >149600000</distance>
<masse unit="kg">5.98e24</masse>
<diametre unit="km">12756</diametre>
<satellite>1</satellite>
</planete>
<planete type="tellurique">
<nom>Mars</nom>
<distance unit="UA" >1.5</distance>
<masse unit="masse terrestre">0.1</masse>
<diametre unit="diamètre terrestre">0.5</diametre>
<satellite>2</satellite>
</planete>
</systeme_solaire>
You have two problems :
First you need to calculate you average diameter outside the
template planete in order to reach all planets then pass this
average to you template
Then you xpath is incorrect : wrong parenthesis, unit is an attribute so you need to use #. You need something like this :
((sum(//diametre[#unit='diamètre
terrestre'])*//diametre[#unit='km'])+//diametre[#unit='km']) div
count(//nom)
Edit : You also need to calculate the actual diameter of your current planet base on earth diameter, you can do this by adding another parameter <xsl:with-param name="terre" select="//diametre[#unit='km']"/> and using it <xsl:when test="diametre*$terre > $avg">
I've updated you XSLT like so :
Solution 1
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/systeme_solaire">
<html lang="fr">
<head>
<title>Les planètes</title>
</head>
<body>
<xsl:apply-templates select="planete[nom!='Terre']">
<xsl:sort select ="diametre" order="descending" data-type="number" />
<xsl:with-param name="avg" select="((sum(//diametre[#unit='diamètre terrestre'])*//diametre[#unit='km'])+//diametre[#unit='km']) div count(//nom)"/>
<xsl:with-param name="terre" select="//diametre[#unit='km']"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="planete" >
<xsl:param name="avg"/>
<xsl:param name="terre"/>
<ul>
<p><b><xsl:value-of select="nom"/> : </b></p>
<li>Distance au soleil: <xsl:value-of select="distance"/><xsl:value-of select="distance/#unit"/></li>
<li>Masse: <xsl:value-of select="masse"/> <xsl:value-of select="masse/#unit"/></li>
<li>
<xsl:choose>
<xsl:when test="diametre*$terre > $avg">
BIG
</xsl:when>
<xsl:otherwise>
SMALL
</xsl:otherwise>
</xsl:choose> Diamètre: <xsl:value-of select="diametre"/> <xsl:value-of select="diametre/#unit"/></li>
<xsl:if test="satellite>0"><li>Nombre de satellites: <xsl:value-of select="satellite"/></li></xsl:if>
</ul>
</xsl:template>
</xsl:stylesheet>
Edit to add michael suggestion (calculate the average diameter as the ratio) :
Solution 2
<xsl:apply-templates select="planete[nom!='Terre']">
<xsl:sort select ="diametre" order="descending" data-type="number" />
<xsl:with-param name="avg" select="(sum(//diametre[#unit='diamètre terrestre'])+1) div count(//nom)"/>
</xsl:apply-templates>
...
<xsl:template match="planete" >
<xsl:param name="avg"/>
...
<xsl:when test="diametre > $avg">
...

JSON to XML SXLT

Currently, i try to mapping json response from Rocket Chat (RC) webhook to OTRS XSLT.
Here what i received from RC.
{
"_id":"4jA2dgTGzwsHSsiLd",
"messages":[
{
"username":"guest-3",
"msg":"youuu",
"ts":"2018-12-13T15:13:56.906Z",
},
{
"username":"agent",
"msg":"hahahaha",
"ts":"2018-12-13T15:14:06.268Z"
},
{
"username":"agent",
"msg":"mane tn?",
"ts":"2018-12-13T15:14:08.817Z"
},
]
}
and below is my current OTRS XSLT mapping.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="title">
<xsl:value-of select="//type" />
</xsl:variable>
<xsl:variable name="custname">
<xsl:value-of select="//visitor/name" />
</xsl:variable>
<xsl:variable name="custemail">
<xsl:value-of select="//visitor/email/address" />
</xsl:variable>
<xsl:variable name="tn">
<xsl:value-of select="//tags" />
</xsl:variable>
<xsl:variable name="createtime">
<xsl:value-of select="//createdAt" />
</xsl:variable>
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*" />
</xsl:copy>
</xsl:template>
<xsl:template match="RootElement">
<xsl:copy>
<TicketNumber><xsl:value-of select="$tn" /></TicketNumber>
<Article>
<Body> ? </Body>
<ContentType>text/html; charset=ISO-8859-15</ContentType>
<Subject><xsl:value-of select="$title" /></Subject>
<From><xsl:value-of select="$custemail" /></From>
<ArticleType>webrequest</ArticleType>
<SenderType>customer</SenderType>
<To>Misc</To>
<HistoryType>AddNote</HistoryType>
<HistoryComment>Note from RC</HistoryComment>
</Article>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="content" />
</xsl:stylesheet>
I would like to get all the value of messages->username and messages->msg from JSON and pass it to the Body tag. Perhaps like :
Msg: hello world
By: guest3
Msg: hello there
By: agent
or maybe html table ?
Thanks ind advance.
Figured this out..
this should be put in tag
<xsl:for-each select="//messages">
<xsl:value-of select="msg" />
<br/>
<xsl:value-of select="username" />
<br/>
<br/>
<xsl:if test="./following-sibling::cd">,</xsl:if>
</xsl:for-each>

XSLT calling template inside of an HTML

If someone would be kind enough to tell me why the following <xsl:call-template name="Log"> won't work?
XML File:
<?xml version="1.0" encoding="UTF-8"?>
<TextMessages>
<Message>[Step]</Message>
<Message>Step ID: 1</Message>
<Message>Description</Message>
</TextMessages>
XSLT File:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection ></h2>
<p>
<xsl:call-template name="Log">
</xsl:call-template>
</p>
</body>
</html>
</xsl:template>
<xsl:template name ="Log">
<xsl:variable name="break"><br/></xsl:variable>
<xsl:for-each select="TextMessages">
<p>
<xsl:value-of select="."/>
<xsl:value-of select="$break"/>
</p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The wanted output:
My CD Collection >
[Step]<br/>
Step ID: 1<br/>
Description<br/>
The real problem has also parameters which I call with parameters, but not sure if that is the problem.
<p>
<xsl:call-template name="Log">
<xsl:with-param name="testId" select="#testId" />
</xsl:call-template>
</p>
<xsl:template name ="Log">
<xsl:param name="testId" />
<xsl:variable name="break"><br/></xsl:variable>
<xsl:for-each select="/t:TestRun/t:Results/t:UnitTestResult[#testId=$testId]/t:Output/t:TextMessages/t:Message">
<p>
<xsl:value-of select="."/>
<xsl:value-of select="$break"/>
</p>
</xsl:for-each>
</xsl:template>
If i am guessing correctly, you want to change:
<xsl:template name ="Log">
<xsl:variable name="break"><br/></xsl:variable>
<xsl:for-each select="TextMessages">
<p>
<xsl:value-of select="."/>
<xsl:value-of select="$break"/>
</p>
</xsl:for-each>
</xsl:template>
to:
<xsl:template name="Log">
<xsl:for-each select="TextMessages/Message">
<p>
<xsl:value-of select="."/>
</p>
<hr/>
</xsl:for-each>
</xsl:template>
Note:
You should never have to use a hack like <br/> to output HTML or XML valid markup;
I am not sure why you need to call a named template here, instead of simply including the xsl:for-each in the first template, or just applying templates to the Message elements.
Adding the line
<xsl:output method="text" version="1.0" encoding="UTF-8" />
to your XSLT file directly after the <xsl:stylesheet... > line will you the (partly desired) result of
My CD Collection >
[Step]
Step ID: 1
Description
With "partly" I do refer to the first line - which you explicitly want to be output by including <h2>My CD Collection ></h2> in your XSLT - but not mention in your output text.