Dynamically change value of in an XSL file - html

Currently the displaying the XML in a browser along with the XSL data is correct except for one thing. In some colleges I have one department while in orders I have more than one (up to 9). How I can dynamically output the data based on the number of department for each college? Currently it only outputs one department per college.
College.xml File
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="colleges.xsl"?><colleges>
<college id="0">
<school>College of Education</school>
<mission>text</mission>
<department id="0">Educational Psychology and Leadership</department>
<department id="1">Health and Human Performance</department>
<department id="2">Language, Literacy and Intercultural Studies</department>
<department id="3">Teaching, Learning and Innovation</department>
</college>
<college id="1">
<school>College of Nursing</school>
<mission>text</mission>
<department id="0">Nursing</department>
</college>
<college id="2">
<school>School of Business</school>
<mission>text</mission>
<department id="0">Accounting and Management Information Systems</department>
<department id="1">Applied Business Technology</department>
</college></colleges>
College.xsl file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="colleges/college">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"><xsl:value-of select="school"/></span> - <br /><xsl:value-of select="mission"/>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<xsl:value-of select="department"/><br />
</p>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Try:
<xsl:template match="/"> ...
<xsl:for-each select="colleges/college">
...
<xsl:apply-templates select="department"/>
...
</xsl:for-each>
</xsl:template>
<xsl:template match="department">... what you want for each department</xsl:template>

Give this a shot...
<xsl:template match="/">
<html>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="colleges/college">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"><xsl:value-of select="school"/></span> - <br /><xsl:value-of select="mission"/>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<xsl:apply-templates select="department"/>
</p>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template match="department">
<xsl:value-of select="."/><br />
</xsl:template>

Related

XSLT How to print all attributes inside an element: <node id="" name="" random=""></node>

The only answers I come across is how to list the value of the node and child nodes itself. What I would like to know is how I can print all the values inside the node tag itself.
XML (there can be more books within catalog)
<data>
<info>
<catalog Id="1111" Locale="en_GB">
<books>
<book id="01" Name="Title 1" Author="John Doe"></book>
<book id="02" Name="Title 2" Author="Jane Doe"></book>
</books>
What I currently have:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="data/info/catalog">
<xsl:value-of select="#Id" />
<xsl:value-of select="/books/book/#Name"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Specifically, I would like the output to be:
Catalog Id: 1111
Attributes:
id: 01
Name: Title 1
Author: John Doe
id: 02
Name: Title 2
Author: Jane Doe
The reason it's being done this way because I have dozens upon dozens of values and it's easier to fit them inside the node tag itself instead of creating hundreds of sub-sub-subchilds.
You did not post the exact HTML code you want to have. Try something like:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/data">
<html>
<body>
<xsl:for-each select="info/catalog">
<xsl:text>Catalog Id: </xsl:text>
<xsl:value-of select="#Id" />
<br/>
<br/>
<xsl:text>Attributes:</xsl:text>
<br/>
<xsl:for-each select="books/book">
<xsl:for-each select="#*">
<xsl:value-of select="name()" />
<xsl:text>: </xsl:text>
<xsl:value-of select="." />
<br/>
</xsl:for-each>
<br/>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

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.

html subscript in XML

I am trying to use the < sub > < /sub > tag to make parts of my xml file subscripts when opened online but there is an issue when I open up the xml file in Internet Explorer (doesn't say what error just does not look right in IE). I think it is because xml is reading < sub > as another child element but < sub > is just supposed to be used to tell hmtl to make certain parts of the xml file subscripts online. Any ideas on what to change? I am also using xsl to convert xml to html for online publication. The code for that is below
XML File:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="ALDformat.xsl"?>
<ALD_data>
<Element Title = "lithium">
<Element>lithium </Element>
<Compound Subtitle = "Li<sub>2</sub>CO<sub>3</sub>">
<Compound> Li<sub>2</sub>CO<sub>3</sub> </Compound>
<Precursor>
<precursor1> Li(thd) </precursor1>
<precursor2> O<sub>3</sub> </precursor2>
<Ref2> Putkonen2009 </Ref2>
</Precursor>
</Compound>
<Compound Subtitle = "Li<sub>2</sub>O(LiOH)">
<Compound> Li<sub>2</sub>O(LiOH) </Compound>
<Precursor>
<precursor1> Li(O<sup>t</sup>Bu) </precursor1>
<precursor2> H<sub>2</sub>O </precursor2>
<Ref2> Aaltonen2010 </Ref2>
</Precursor>
</Compound>
</Element>
</ALD_data>
XSL File:
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<h1>ALD literature XML database</h1>
<p>Last update: 6 January 2016</p>
<xsl:for-each select="ALDdata/Element">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"> Element: <xsl:value-of select="Element"/> </span>
</div>
<xsl:for-each select="Compound">
<div style="background-color:lightgrey;margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<span> Compound: <xsl:value-of select="Compound"/> </span>
</p>
</div>
<xsl:for-each select="Precursor">
<div style="margin-left:30px;margin-bottom:1em;font-size:10pt">
<p>
Precursor 1: <xsl:value-of select="precursor1"/> <br/>
Precursor 2: <xsl:value-of select="precursor2"/>
</p>
Post-2005 review paper references
<ol>
<xsl:for-each select="Ref2">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</ol>
</div>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
In order for your sub and sup elements to be processed, you'll have to use xsl:apply-templates instead of xsl:value-of for the elements that contain sub and sup.
I don't think you can use a literal result element as a stylesheet and add templates.
Maybe try something like this instead...
XML Input (Note: I had to remove the values of the Compound/#Subtitle attributes as they are not well-formed.)
<?xml-stylesheet type="text/xsl" href="ALDformat.xsl"?>
<ALD_data>
<Element Title = "lithium">
<Element>lithium </Element>
<Compound Subtitle = "">
<Compound> Li<sub>2</sub>CO<sub>3</sub> </Compound>
<Precursor>
<precursor1> Li(thd) </precursor1>
<precursor2> O<sub>3</sub> </precursor2>
<Ref2> Putkonen2009 </Ref2>
</Precursor>
</Compound>
<Compound Subtitle = "">
<Compound> Li<sub>2</sub>O(LiOH) </Compound>
<Precursor>
<precursor1> Li(O<sup>t</sup>Bu) </precursor1>
<precursor2> H<sub>2</sub>O </precursor2>
<Ref2> Aaltonen2010 </Ref2>
</Precursor>
</Compound>
</Element>
</ALD_data>
XSLT 1.0 (ALDformat.xsl)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<html>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<h1>ALD literature XML database</h1>
<p>Last update: 6 January 2016</p>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="Element[#Title]">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"> Element: <xsl:apply-templates select="Element"/> </span>
</div>
<xsl:apply-templates select="Compound"/>
</xsl:template>
<xsl:template match="Compound[#Subtitle]">
<div style="background-color:lightgrey;margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<span> Compound: <xsl:apply-templates select="Compound"/> </span>
</p>
</div>
<xsl:apply-templates select="Precursor"/>
</xsl:template>
<xsl:template match="Precursor">
<div style="margin-left:30px;margin-bottom:1em;font-size:10pt">
<p>
Precursor 1: <xsl:apply-templates select="precursor1"/> <br/>
Precursor 2: <xsl:apply-templates select="precursor2"/>
</p>
Post-2005 review paper references
<ol>
<xsl:apply-templates select="Ref2"/>
</ol>
</div>
</xsl:template>
<xsl:template match="sub|sup">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="Ref2">
<li><xsl:apply-templates/></li>
</xsl:template>
</xsl:stylesheet>
Output
<html>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<h1>ALD literature XML database</h1>
<p>Last update: 6 January 2016</p>
<div style="background-color:teal;color:white;padding:4px"><span style="font-weight:bold"> Element: lithium </span></div>
<div style="background-color:lightgrey;margin-left:20px;margin-bottom:1em;font-size:10pt">
<p><span> Compound: Li<sub>2</sub>CO<sub>3</sub></span></p>
</div>
<div style="margin-left:30px;margin-bottom:1em;font-size:10pt">
<p>
Precursor 1: Li(thd) <br>
Precursor 2: O<sub>3</sub></p>
Post-2005 review paper references
<ol>
<li> Putkonen2009 </li>
</ol>
</div>
<div style="background-color:lightgrey;margin-left:20px;margin-bottom:1em;font-size:10pt">
<p><span> Compound: Li<sub>2</sub>O(LiOH) </span></p>
</div>
<div style="margin-left:30px;margin-bottom:1em;font-size:10pt">
<p>
Precursor 1: Li(O<sup>t</sup>Bu) <br>
Precursor 2: H<sub>2</sub>O
</p>
Post-2005 review paper references
<ol>
<li> Aaltonen2010 </li>
</ol>
</div>
</body>
</html>
You can't place HTML in a stylesheet right away, your XSLT must have the stylesheet element at the top level, and have template elements as child content, like
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<h1>ALD literature XML database</h1>
<p>Last update: 6 January 2016</p>
<xsl:for-each select="ALDdata/Element">
<!-- ... -->
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I must admit, it's been a while since I used in-browser XSLT. Are you using IE by chance?

Vertical output XSL and XML

I am working on a simple dictionary in XML, and now I'm trying to output some words vertical, but they all come out on a line without spaces.
This is some of the XML file
<thesaurus>
<dictionary>
<language>English</language>
<word type="1">word 1</word>
<word type="2">word 2</word>
<word type="3">word 3</word>
<word type="4">word 4</word>
<word type="5">word 5</word>
<word type="6">word 6</word>
</dictionary>
</thesaurus>
This is my first "almost" solution
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="//word">
<xsl:sort order="ascending"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
That solution only prints out all the word like this
AgentsColorFoundationsGrainPartialPogotypePretendSilentStrollTender
My second try is something like this
<xsl:for-each select="thesaurus">
<h1> <xsl:value-of select="//word"/></h1>
</xsl:for-each>
In that way I could style the words and they will print vertical, but the thing is that only the first of the words is printing. =/
Would be great with a hint :)
Thanks
Use this template:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="*/*/word">
<xsl:sort order="ascending"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="word">
<xsl:value-of select="."/>
<br/>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<body>word 1<br />word 2<br />word 3<br />word 4<br />word 5<br />word 6<br /></body>
</html>

How this style sheet can produce HTML output

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>