I'm trying to transform the following XML..
<?xml version="1.0" encoding="utf-16"?><Member TextRank="unknown" FullName="My Name" ..etc.. />
Into something like the following,
<div class="member">
<span class="label">
Text Rank (note: i want to override the labels in the xslt)
</div>
<span class="value">
unknown
</span>
<span class="label">
Full Name
</div>
<span class="value">
My Name
</span>
..etc..
</div>
How if possible could I do this using xslt?
Here's a different approach, that does away for the need for the xsl:choose element. Instead, you could take advantage of the matching templates to have specific templates for the cases of attributes who names you want to override, and a generic template for other case.
To avoid repetition of code, you could also make the generic template a named template, with a parameter to override the name
<xsl:template match="#*" name="attribute">
<xsl:param name="label" select="local-name()" />
So, the default for most attributes would be to use the attribute name, but the specific template for #FullName could then call this with a different name.
<xsl:template match="#FullName">
<xsl:call-template name="attribute">
<xsl:with-param name="label" select="'Full Name'" />
</xsl:call-template>
</xsl:template>
Here is the full XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="*">
<div class="{local-name()}">
<div> Title: </div>
<xsl:apply-templates select="#*"/>
</div>
</xsl:template>
<xsl:template match="#FullName">
<xsl:call-template name="attribute">
<xsl:with-param name="label" select="'Full Name'" />
</xsl:call-template>
</xsl:template>
<xsl:template match="#*" name="attribute">
<xsl:param name="label" select="local-name()" />
<span class="label">
<xsl:value-of select="concat($label, ' : ')"/>
</span>
<span class="value">
<xsl:value-of select="."/>
</span>
<br/>
</xsl:template>
</xsl:stylesheet>
When applied to the following XML:
<Member TextRank="unknown" ID="12" FullName="My Name" Dob="01/01/1970" />
The following is output:
<div class="Member">
<div> Title: </div>
<span class="label">TextRank : </span>
<span class="value">unknown</span>
<br>
<span class="label">ID : </span>
<span class="value">12</span>
<br>
<span class="label">Full Name : </span>
<span class="value">My Name</span>
<br>
<span class="label">Dob : </span>
<span class="value">01/01/1970</span>
<br>
</div>
This is the solution I came up with.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" />
<xsl:template match="*">
<xsl:element name="div">
<xsl:attribute name="class">className</xsl:attribute>
<div>
Title:
</div>
<!-- UID, Name, DBO-->
<xsl:apply-templates select="#ID"/>
<xsl:apply-templates select="#FullName"/>
<xsl:apply-templates select="#Dob"/>
</xsl:element>
</xsl:template>
<xsl:template match="#*">
<xsl:element name="span">
<xsl:attribute name="class">label</xsl:attribute>
<xsl:choose>
<xsl:when test="name() = 'FullName'">
Full Name
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="name()"/>
</xsl:otherwise>
</xsl:choose>
:
</xsl:element>
<xsl:element name="span">
<xsl:attribute name="class">value</xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
<br/>
</xsl:template>
</xsl:stylesheet>
Related
i've the below xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ntw="Number2Word.uri" exclude-result-prefixes="ntw">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<xsl:text disable-output-escaping="yes"><![CDATA[</meta>]]></xsl:text>
<link rel="stylesheet" href="C:\Documents and Settings\u0138039\Desktop\Per\NEW.css" type="text/css"></link>
</head>
<body>
<section class="tr_chapter">
<div class="chapter">
<xsl:apply-templates/>
</div>
</section>
</body>
</html>
</xsl:template>
<xsl:template match="case">
<xsl:apply-templates select="case.head"/>
</xsl:template>
<xsl:template match="case.head">
<div class="section-sect0">
<div class="para">
<xsl:value-of select="./party.line/party[#role='Plaintiff']"/>
</div>
</div>
<div class="section-sect0">
<div class="para">
<xsl:text disable-output-escaping="yes">V</xsl:text>
</div>
</div>
<div class="section-sect0">
<div class="para">
<xsl:value-of select="./party.line/party[#role='Defendant']"/>
</div>
</div>
<xsl:apply-templates select="case.ref.no.group | judge.line"/>
</xsl:template>
<xsl:template match="para">
<div class="para">
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="case.ref.no.group">
<div class="section-sect3">
<span class="font-style-bold">Court of Appeal</span>
<xsl:text> - </xsl:text>
<xsl:value-of select="case.ref.no[1]/prefix" />
<xsl:text> Nos. </xsl:text>
<xsl:for-each select="case.ref.no">
<xsl:value-of select="number" />
<xsl:text>-</xsl:text>
<xsl:value-of select="year" />
<xsl:if test="not(position() = 2)">
</xsl:if>
</xsl:for-each>
</div>
</xsl:template>
<xsl:template match="judge.line">
<div class="section-sect3">
<xsl:for-each select="judge">
<xsl:value-of select="name"/>
<xsl:if test="not(position() = last())">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text> JC</xsl:text>
</div>
<xsl:apply-templates select="following-sibling::date.group"></xsl:apply-templates>
</xsl:template>
<xsl:template match="date.group">
<div class="section-sect4">
<div class="para">
<xsl:value-of select="./date.line/date"/>
</div>
</div>
<xsl:apply-templates select="//catchwords.group"/>
</xsl:template>
<xsl:template match="catchwords.group">
<div class="y">
<xsl:for-each select="catchwords/catchword">
<xsl:choose>
<xsl:when test="#level=1"><br/>
<span class="font-style-bolditalic">
<xsl:value-of select="."/>
</span>
<xsl:text disable-output-escaping="yes">-</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select=".">
<xsl:value-of select="."></xsl:value-of>
<xsl:if test="not(position() = last()-1)">
<xsl:text disable-output-escaping="yes"> – </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</div>
<xsl:apply-templates select="//headnotes"/>
</xsl:template>
<xsl:template match="headnotes/para">
<xsl:choose>
<xsl:when test="position()=1">
<div class="x">
<xsl:apply-templates></xsl:apply-templates>
</div>
</xsl:when>
<xsl:otherwise>
<div class="m">
<xsl:apply-templates/>
</div>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="para.group"/>
</xsl:template>
<xsl:template match="para.group">
<div class="section-sect1">
<xsl:value-of select="./heading"/>
</div>
<xsl:for-each select="./para">
<xsl:apply-templates select="node()[not(self::label)]"/>
</xsl:for-each>
<xsl:apply-templates select="//counsel.group"/>
</xsl:template>
<xsl:template name="orderedlist" match="list">
<ol class="eng-orderedlist orderedlist">
<xsl:apply-templates select="list.item/label"/>
</ol>
</xsl:template>
<xsl:template name="orderitempara" match="list.item/label">
<li class="item">
<div class="para">
<span class="item-num">
<xsl:choose>
<xsl:when test="following-sibling::case.considered">
<xsl:apply-templates/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</span>
<xsl:apply-templates select="parent::list.item"/>
</div>
</li>
</xsl:template>
<xsl:template match="list.item">
<xsl:variable name="a">
<xsl:value-of select="./label"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="./label">
<xsl:apply-templates select="child::node()[not(self::label|case.ref)]"/>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="counsel.group" name="j">
<div class="ital">
<xsl:for-each select="./counsel.line">
<div class="para">
<xsl:value-of select="."/>
</div>
</xsl:for-each>
<xsl:text disable-output-escaping="yes">Judgment received: December 4, 2008</xsl:text>
</div>
<xsl:apply-templates select="//ref.group"/>
</xsl:template>
<xsl:template match="ref.group/leg.mentioned">
<div class="section-sect1">
<xsl:text>Legislation mentioned in the judgment</xsl:text>
</div>
<xsl:for-each select="./leg.ref">
<div class="para">
<xsl:value-of select="./citetitle"/>
<xsl:text>, </xsl:text>
<xsl:for-each select="./leg.ptr.group/leg.ptr">
<xsl:value-of select="."/>
<xsl:if test="not(position() = last())">
<xsl:text disable-output-escaping="yes">, </xsl:text>
</xsl:if>
</xsl:for-each>
</div>
</xsl:for-each>
<xsl:text>Cases cited in the judgment</xsl:text>
<xsl:for-each select="//citetitle">
<xsl:if test="parent::case.ref/#annotation">
<div class="ital">
<div class="para">
<xsl:value-of select="."/>
<xsl:value-of select="//citecitation"/>
<xsl:value-of select="parent::case.ref/#court"/>
<xsl:text> (</xsl:text>
<xsl:value-of select="parent::case.ref/#annotation"/>
<xsl:text>)</xsl:text>
</div>
</div>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates select="//judgment"/>
</xsl:template>
<xsl:template match="judgment">
<div class="section-sect1">
<xsl:value-of select="./judge.block/heading/judgename"/>
</div>
<xsl:for-each select="./judge.block/para">
<div class="para">
<span class="new">
<xsl:value-of select="./label"></xsl:value-of>
</span>
<xsl:value-of select="./text()"/>
</div>
<xsl:apply-templates select="./list"/>
</xsl:for-each>
<xsl:for-each select="./judge.block/para.group">
<xsl:if test="./heading">
<div class="section-sect1">
<xsl:value-of select="./heading/text()"/>
</div>
</xsl:if>
<xsl:for-each select="para">
<div class="para">
<span class="new">
<xsl:value-of select="./label"></xsl:value-of>
</span>
<xsl:apply-templates select="child::node()[not(self::label)]"/>
</div>
<xsl:apply-templates select="./list"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template match="emphasis">
<xsl:choose>
<xsl:when test="citetitle">
<span class="font-style-italic">
<xsl:apply-templates select="./list.item"/>
</span>
</xsl:when>
<xsl:when test="./#type">
<xsl:variable name="fontStyle">
<xsl:value-of select="concat('font-style-',#type)"/>
</xsl:variable>
<span class="{$fontStyle}">
<xsl:apply-templates/>
</span>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
when i apply this on my below part of my xml it is giving me a stackoverflow error.
<?xml version="1.0" encoding="UTF-8"?>
<case>
<counsel.group>
<counsel.line>Azar Irwan b Moh Arifin (AG’s Chambers) for Public Prosecutor</counsel.line>
<counsel.line>SK Pari (Pari & Co) for first accused</counsel.line></counsel.group>
<ref.group>
<leg.mentioned>
<leg.ref country="India">
<citetitle type="leg" legtype="ord">Indian Penal Code</citetitle>
<leg.ptr.group>
<leg.ptr provision="s" print="yes">302</leg.ptr>
</leg.ptr.group>
</leg.ref>
<leg.ref country="Malaysia">
<citetitle type="leg" legtype="ord">Criminal Procedure Code</citetitle>
<leg.ptr.group>
<leg.ptr provision="ss" print="yes">112</leg.ptr>
<leg.ptr provision="s" print="no">112</leg.ptr>
</leg.ptr.group>
</leg.ref>
<leg.ref country="Malaysia">
<citetitle type="leg" legtype="ord">Penal Code</citetitle>
<leg.ptr.group>
<leg.ptr provision="ss" print="yes">109</leg.ptr>
<leg.ptr provision="s" print="no">299</leg.ptr>
</leg.ptr.group>
</leg.ref>
</leg.mentioned></ref.group>
</case>
please let me know how do i resolve this error and what are the steps to be followed so that in future i don't encounter this error.
Thanks
Your XSLT contains an infinite loop. Your para.group template contains this:
<xsl:apply-templates select="//counsel.group"/>
And then the template for counsel.group has this:
<xsl:apply-templates select="//ref.group"/>
The template for ref.group has this:
<xsl:apply-templates select="//judgment"/>
The template for judgment has this:
<xsl:for-each select="para">
...
<xsl:apply-templates select="child::node()[not(self::label)]"/>
...
</xsl:for-each>
Which cascades down to apply templates on a para.group and then you're back at the beginning. This process goes around in circles until the stack overflows. I think you need to rethink your XSLT to figure out how to prevent infinite recursion.
I am trying to change some text from XML. However it did not work.
I have to display all of [intro] text from XML
some Path(text) should change before display [intro].
For example
<a href="3DD3D025-2236-49C9-A169-DD89A36DA0E6/eee.pdf"> --> wrong path
I would like to change to
<a href="Content\3\D\D\3DD3D025-2236-49C9-A169-DD89A36DA0E6/eee.pdf">
Any help would be greatly appreciated.
Sample XML
<?xml version="1.0"?>
<root>
<intro xml:lang="en">
<div class="blueBar">
<h2>Highlights</h2>
<ul>
<li>aaaa</li>
<li>bbbb</li>
<li>ccc</li>
<li>pdf</li>
</ul>
</div>
</intro>
<intro> .....</intro>
</root>
Sample XSLT
<xsl:param name="language"/>
<xsl:template match="root">
<xsl:for-each select="intro[lang($language)]//#href">
<xsl:choose>
<xsl:when test="contains(.,'pdf') and not(contains(.,'Content'))">
<xsl:variable name="pdfGuid">
<xsl:value-of select="substring(.,0,36)"/>
</xsl:variable>
<xsl:variable name="pdfPath">
<xsl:value-of select="concat('/','Content')"/>
<xsl:value-of select="concat('/', substring($pdfGuid, 1,1))"/>
<xsl:value-of select="concat('/', substring($pdfGuid, 2,1))"/>
<xsl:value-of select="concat('/', substring($pdfGuid, 3,1))"/>
<xsl:value-of select="concat('/', $pdfGuid)"/>
</xsl:variable>
<xsl:value-of select="strJS:replace(string(.),string($pdfGuid),string($pdfPath))" disable-output-escaping="yes"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<div class="greenBox">
<xsl:value-of select="intro[lang($language)]" disable-output-escaping="yes"/>
</div>
</xsl:template>
When you ask a question here, please clearly explain the output you want and what the problem is that you are encountering.
There is a discrepancy between your description of the problem and your attempt, wherein you are using forward slashes, and one at the beginning of the path in your XSLT, and backslashes in your description. I am going to assume you want forward slashes, but these can easily be changed. I suspect what you want is something like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:param name="language" select="'en'" />
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<div class="greenBox">
<xsl:apply-templates select="intro[lang($language)]/node()" />
</div>
</xsl:template>
<xsl:template match="#href[contains(., 'pdf') and not(contains(., 'Content'))]">
<xsl:attribute name="href">
<xsl:value-of select="concat('/Content/',
substring(., 1, 1), '/',
substring(., 2, 1), '/',
substring(., 3, 1), '/',
.)"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
When run on your sample input, the result is:
<div class="greenBox">
<div class="blueBar">
<h2>Highlights</h2>
<ul>
<li>
aaaa
</li>
<li>
bbbb
</li>
<li>
ccc
</li>
<li>
pdf
</li>
</ul>
</div>
</div>
Hi my XML document and XSLT are not working to produce good HTML... what is going on?
This is basically XML file and I have validated this with the XML Schema but
when I use XSLT file to convert it into HTML file it just generates Courses Catalogue heading
with one paragraph of whole bunch of text.
What kinds of problem Do I have here?
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Generate HTML output -->
<xsl:output method="html"/>
<!-- The root template is defined here -->
<xsl:template match="/">
<html>
<head>
<title>Courses Catalogue</title>
</head>
<body>
<h2>Courses catalogue</h2>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="course">
<p>
<xsl:apply-templates select="code" />
<xsl:apply-templates select="title" />
<xsl:apply-templates select="year" />
<xsl:apply-templates select="science" />
<xsl:apply-templates select="area" />
<xsl:apply-templates select="subject" />
<xsl:apply-templates select="updated" />
<xsl:apply-templates select="unit" />
<xsl:apply-templates select="description" />
<xsl:apply-templates select="outcomes" />
<xsl:apply-templates select="incompatibility" />
</p>
</xsl:template>
<xsl:template match="code">
Course Code: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="title">
Course Title: <span style="color:#000">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="year">
Student Year: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="science">
Science Group: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="area">
Area: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="subject">
Course Subject: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="updated">
Page was updated in: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="unit">
Unit: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="description">
Course Description: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="outcomes">
Course Outcomes: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
< xsl:template match="incompatibility">
Incompatible courses: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
</xsl:stylesheet>
AND MY XML FILE
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"
href="courses.xsl"?>
<!--catalogue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="catalogue.xsd"-->
<catalogue xmlns="file://"
xmlns:xsi="http:/e"
xsi:schemaLocation="file://">
<course>
<code>COMP3410</code>
<title> Information Technology in Electronic Commerce </title>
<year>later year</year>
<science>C</science>
<area> Research School of Computer Science </area>
<subject> Computer Science </subject>
<updated>2012-03-13T13:12:00</updated>
<unit>6</unit>
Thanks
Your problem is that all of the elements are in the file://Volumes/u1234567/Assignment namespace, but in your XSLT your templates are matching on elements without a namespace.
If you look closely at the <catalogue> you will see a namespace declaration without a prefix. <catalogue xmlns="file://Volumes/u1234567/Assignment" All of the descendant elements inherit that namespace.
Define that namespace with a prefix in your XSLT and then change the places where you refer to those elements to use that namespace prefix:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="file://Volumes/u1234567/Assignment">
<!-- Generate HTML output -->
<xsl:output method="html"/>
<!-- The root template is defined here -->
<xsl:template match="/">
<html>
<head>
<title>Courses Catalogue</title>
</head>
<body>
<h2>Courses catalogue</h2>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="a:course">
<p>
<xsl:apply-templates select="a:code" />
<xsl:apply-templates select="a:title" />
<xsl:apply-templates select="a:year" />
<xsl:apply-templates select="a:science" />
<xsl:apply-templates select="a:area" />
<xsl:apply-templates select="a:subject" />
<xsl:apply-templates select="a:updated" />
<xsl:apply-templates select="a:unit" />
<xsl:apply-templates select="a:description" />
<xsl:apply-templates select="a:outcomes" />
<xsl:apply-templates select="a:incompatibility" />
</p>
</xsl:template>
<xsl:template match="a:code">
Course Code:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:title">
Course Title:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:year">
Student Year: <span style="color:#C66">
<xsl:value-of select="." /> </span>
<br />
</xsl:template>
<xsl:template match="a:science">
Science Group:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:area">
Area:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:subject">
Course Subject:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:updated">
Page was updated in:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:unit">
Unit:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:description">
Course Description:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:outcomes">
Course Outcomes:
<xsl:value-of select="." />
<br />
</xsl:template>
<xsl:template match="a:incompatibility">
Incompatible courses:
<xsl:value-of select="." />
<br />
</xsl:template>
</xsl:stylesheet>
I have this xsl bellow :
<xsl:template match="/" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:SharePoint="Microsoft.SharePoint.WebControls">
<div id="vtab">
<ul>
<xsl:call-template name="dvt_1.body"/>
</ul>
</div>
</xsl:template>
<xsl:template name="dvt_1.body">
<xsl:for-each select="/dsQueryResponse/Rows/Row[generate-id(.)=generate-id(key('TitleKey', #Title))]">
<li>
<xsl:value-of select="#Title"/>
</li>
<xsl:variable name="thisClassification" select="#Title" />
<div>
<xsl:for-each select="../Row[#Title = $thisClassification]">
<xsl:value-of select="#Model"/>
</xsl:for-each>
</div>
</xsl:for-each>
</xsl:template>
I would like have this output:
<div id="vtab">
<ul>
<li>HTC</li>
<li>Nokia</li>
</ul>
<div>HTC Sensation 345-HTC Ev</div>
<div>Nokia</div>
</div>
But now this is what i'm getting
<div id="vtab">
<ul>
<li>HTC</li>
<div>HTC Sensation 345HTC Evo</div>
<li>Nokia</li>
<div>Nokia</div>
</ul>
</div>
How can i exit the UL element and start the DIV element.
Thanks alot
It is not optimal, but consider making two loops something like the following:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<div id="vtab">
<xsl:call-template name="LOOP_1"/>
<xsl:call-template name="LOOP_2"/>
</div>
</xsl:template>
<xsl:template name="LOOP_1">
<ul>
<xsl:for-each select="/dsQueryResponse/Rows/Row[generate-id(.)=generate-id(key('TitleKey', #Title))]">
<li>
<xsl:value-of select="#Title"/>
</li>
<xsl:variable name="thisClassification" select="#Title"/>
<div>
<xsl:for-each select="../Row[#Title = $thisClassification]">
<xsl:value-of select="#Model"/>
</xsl:for-each>
</div>
</xsl:for-each>
</ul>
</xsl:template>
<xsl:template name="LOOP_2">
<div>
<xsl:for-each select="/dsQueryResponse/Rows/Row[generate-id(.)=generate-id(key('TitleKey', #Title))]">
<xsl:variable name="thisClassification" select="#Title"/>
<div>
<xsl:for-each select="../Row[#Title = $thisClassification]">
<xsl:value-of select="#Model"/>
</xsl:for-each>
</div>
</xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>
I am not sure of your requirements, but it may be more efficient to review the desired output, and output nested lists and format it using CSS rather than creating separate loops
I need to check if a param has got a value in it and if it has then do this line otherwise do this line.
I've got it working whereas I don't get errors but it's not taking the right branch
The branch that is wrong is in the volunteer_role template
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="volunteers-by-region" match="volunteer" use="region" />
<xsl:template name="hoo" match="/">
<html>
<head>
<title>Registered Volunteers</title>
<link rel="stylesheet" type="text/css" href="volunteer.css" />
</head>
<body>
<h1>Registered Volunteers</h1>
<h3>Ordered by the username ascending</h3>
<xsl:for-each select="folktask/member[user/account/userlevel='2']">
<xsl:for-each select="volunteer[count(. | key('volunteers-by-region', region)[1]) = 1]">
<xsl:sort select="region" />
<xsl:for-each select="key('volunteers-by-region', region)">
<xsl:sort select="folktask/member/user/personal/name" />
<div class="userdiv">
<xsl:call-template name="volunteer_volid">
<xsl:with-param name="volid" select="../volunteer/#id" />
</xsl:call-template>
<xsl:call-template name="volunteer_role">
<xsl:with-param name="volrole" select="../volunteer/roles" />
</xsl:call-template>
<xsl:call-template name="volunteer_region">
<xsl:with-param name="volloc" select="../volunteer/region" />
</xsl:call-template>
</div>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
<xsl:if test="position()=last()">
<div class="count">
<h2>Total number of volunteers: <xsl:value-of select="count(/folktask/member/user/account/userlevel[text()=2])" />
</h2>
</div>
</xsl:if>
</body>
</html>
</xsl:template>
<xsl:template name="volunteer_volid">
<xsl:param name="volid" select="'Not Available'" />
<div class="heading2 bold"><h2>VOLUNTEER ID: <xsl:value-of select="$volid" /></h2></div>
</xsl:template>
<xsl:template name="volunteer_role">
<xsl:param name="volrole" select="'Not Available'" />
<div class="small bold">ROLES:</div>
<div class="large">
<xsl:choose>
<xsl:when test="string-length($volrole)!=0">
<xsl:value-of select="$volrole" />
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:template>
<xsl:template name="volunteer_region">
<xsl:param name="volloc" select="'Not Available'" />
<div class="small bold">REGION:</div>
<div class="large"><xsl:value-of select="$volloc" /></div>
</xsl:template>
</xsl:stylesheet>
And the XML:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="volunteers.xsl"?>
<folktask xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="folktask.xsd">
<member>
<user id="1">
<personal>
<name>Abbie Hunt</name>
<sex>Female</sex>
<address1>108 Access Road</address1>
<address2></address2>
<city>Wells</city>
<county>Somerset</county>
<postcode>BA5 8GH</postcode>
<telephone>01528927616</telephone>
<mobile>07085252492</mobile>
<email>adrock#gmail.com</email>
</personal>
<account>
<username>AdRock</username>
<password>269eb625e2f0cf6fae9a29434c12a89f</password>
<userlevel>4</userlevel>
<signupdate>2010-03-26T09:23:50</signupdate>
</account>
</user>
<volunteer id="1">
<roles></roles>
<region>South West</region>
</volunteer>
</member>
<member>
<user id="2">
<personal>
<name>Aidan Harris</name>
<sex>Male</sex>
<address1>103 Aiken Street</address1>
<address2></address2>
<city>Chichester</city>
<county>Sussex</county>
<postcode>PO19 4DS</postcode>
<telephone>01905149894</telephone>
<mobile>07784467941</mobile>
<email>ambientexpert#yahoo.co.uk</email>
</personal>
<account>
<username>AmbientExpert</username>
<password>8e64214160e9dd14ae2a6d9f700004a6</password>
<userlevel>2</userlevel>
<signupdate>2010-03-26T09:23:50</signupdate>
</account>
</user>
<volunteer id="2">
<roles>Van Driver,gas Fitter</roles>
<region>South Central</region>
</volunteer>
</member>
</folktask>
The following should do the trick:
<xsl:template name="volunteer_volid">
<xsl:param name="volid" />
<xsl:choose>
<xsl:when test="string-length($volid) > 0">
<div class="heading2 bold"><h2>VOLUNTEER ID: <xsl:value-of select="$volid" /></h2></div>
</xsl:when>
<xsl:otherwise>
<!-- No volid -->
</xsl:otherwise>
</xsl:choose>
</xsl:template>
I've replaced the default value with an empty string so that not providing a parameter value is the same as providing the parameter value as "". If this isn't the desired behaviour then change the parameters select and modify the test expression accordingly, for example:
$volid != 'Not Available'