XSLT - Select only one set of data - xslt

Need some help: I want to only pull back the results for a specific ItemId, so if i wanted the only data for ItemId how could I adapt the template below?
I've tried for about 2 hours without any luck...
Here is a sample of my XML:

This Xslt only brings the item with ItemId value equal to 2075.
Notice I added [ItemInfo/PeriodAttendanceInfo/ItemId=2075].
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:call-template name="dvt_2"/>
</xsl:template>
<xsl:template name="dvt_2">
<xsl:variable name="Rows" select="/ArrayOfPeriodAttendanceGroup/PeriodAttendanceGroup[ItemInfo/PeriodAttendanceInfo/ItemId=2075]"/>
<xsl:variable name="dvt_RowCount" select="count($Rows)"/>
<xsl:call-template name="dvt_2.rowContents">
<xsl:with-param name="Rows" select="$Rows"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="dvt_2.rowContents">
<xsl:param name="Rows"/>
<xsl:for-each select="$Rows">
<xsl:sort select="ItemInfo/PeriodAttendanceInfo" order="ascending"/>
<xsl:call-template name="dvt_2.rowContentsView"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="dvt_2.rowContentsView">
<xsl:variable name="subjectName" select="ItemInfo/PeriodAttendanceInfo/ItemText"/>
<tr class="{$subjectName}">
<xsl:element name="td">
<xsl:attribute name="class">
<xsl:value-of select="ItemInfo/PeriodAttendanceInfo/ItemText"/>
</xsl:attribute>
<xsl:variable name="subjectID" select="ItemInfo/PeriodAttendanceInfo/ItemId"/>
<a class="leftAlign" href="../Departments/{$subjectID}">
<xsl:value-of select="ItemInfo/PeriodAttendanceInfo/ItemText"/>
</a>
</xsl:element>
<td>
<p class="name">
<xsl:value-of select="Total"/>
</p>
</td>
<td>
<p class="name">
<xsl:value-of select="Present"/>
</p>
</td>
<td>
<p class="name">
<xsl:value-of select="Late"/>
</p>
</td>
<td>
<p class="name">
<xsl:value-of select="UnauthorisedAbsence"/>
</p>
</td>
</tr>
</xsl:template>
<xsl:template name="dvt_1.empty">
<xsl:variable name="dvt_ViewEmptyText">There is no timetable view for today</xsl:variable>
<table border="0" width="100%">
<tr>
<td class="ms-vb">
<xsl:value-of select="$dvt_ViewEmptyText"/>
</td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>

Related

XSL quoting hell?

I have a simple XML file that looks like this:
<bases xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<marker>
<name>Sample 1</name>
<adr>Boston</adr>
<state>Mass</state>
<wiki>Link_1</wiki>
</marker>
<marker>
<name>Sample 2</name>
<adr>Essex</adr>
<state>Vermont</state>
</marker>
If there is a <wiki> element, I want to create an HTML link (an <a href..> tag) in the output. If there is no <wiki> element, then output only the name. Here's my XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table><xsl:text>
</xsl:text>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
<xsl:for-each select="bases/marker">
<xsl:sort select="name" />
<tr>
<td>
<xsl:if test="wiki != ''">
<a href="https://en.wikipedia.com/wiki/<xsl:value-of select='wiki'/>">
</xsl:if>
<xsl:value-of select="name"/>
<xsl:if test="wiki != ''">
</a>
</xsl:if>
</td>
</tr>
<td><xsl:value-of select="adr"/>, <xsl:value-of select="state"/></td>
<xsl:text>
</xsl:text>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
I get a bunch of errors, starting with this one:
Unescaped '<' not allowed in attributes values
<a href="https://en.wikipedia.com/wiki/<xsl:value-of select='wiki'/>">
Help!
I am guessing you want to do:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/bases">
<table>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
<xsl:for-each select="marker">
<xsl:sort select="name" />
<tr>
<td>
<xsl:if test="wiki">
<a href="https://en.wikipedia.com/wiki/{wiki}">
<xsl:value-of select="name"/>
</a>
</xsl:if>
</td>
<td>
<xsl:value-of select="adr"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="state"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
To understand this, read about Attribute Value Templates.
Use an attribute value template:
<a href="https://en.wikipedia.com/wiki/{wiki}"/>
Otherwise, you could construct the value of the attribute like this:
<a>
<xsl:attribute name="href">https://en.wikipedia.com/wiki/<xsl:value-of select='wiki'/></xsl:attribute>
</a>
or like this:
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat('https://en.wikipedia.com/wiki/', wiki)"/>
</xsl:attribute>
</a>

Split a tag inside foreach

I have a xml below
<Report>
<rl>
<id>12345;12346</id>
<activity>a2/a3</activity>
<result>r2/r3</result>
<operator>test</operator>
<timestamp>12/18/2014 3:51:19 PM</timestamp>
<quantity>2</quantity>
</rl>
<rl>
<id>22345;22346</id>
<activity>a3/a4</activity>
<result>r3/r4</result>
<operator>test</operator>
<timestamp>12/18/2014 3:51:19 PM</timestamp>
<quantity>2</quantity>
</rl>
</Report>
and for my xsl,
<table border="1" style="border-width: 1px" width="90%" bordercolor="#C0C0C0" align="center">
<tr>
<th width="5%" align="center">
<font color="#000000" face="Verdana" size="3">Index</font>
</th>
<th width="15%" align="center">
<font color="#000000" face="Verdana" size="3">ID A:</font>
</th>
<th width="15%" align="center">
<font color="#000000" face="Verdana" size="3">ID B:</font>
</th>
</tr>
<xsl:for-each select="Report/rl">
<tr height="25">
<td width="5%" align="center" >
<font color="#000000" face="Verdana" size="2">
<xsl:value-of select="position()" />
</font>
</td>
<td align="center">
<font color="#000000" face="Verdana" size="2">
<xsl:value-of select="idA" />
</font>
</td>
<td align="center">
<font color="#000000" face="Verdana" size="2">
<xsl:value-of select="idB" />
</font>
</td>
</tr>
</xsl:for-each>
</table>
For 1st rl, there is 12345:12346 for tag, I want to split them into 12345 and 12346 and show them in the 'idA' and 'idB'. How should I do that?
My xslt version is 1.0.
Assuming there are always exactly two values, separated by a semicolon, use:
<xsl:value-of select="substring-before(id, ';')"/>
to populate the idA cell, and:
<xsl:value-of select="substring-after(id, ';')"/>
to populate the idB cell.
Added:
For the same example as posted, can you elaborate more about the
'recursive named template'?
The solution using a recursive named template would look something like this:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/Report">
<table border="1">
<tr>
<th>Index</th>
<th>ID A</th>
<th>ID B</th>
</tr>
<xsl:apply-templates select="rl"/>
</table>
</xsl:template>
<xsl:template match="rl">
<tr>
<td>
<xsl:value-of select="position()" />
</td>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="id"/>
</xsl:call-template>
</tr>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="';'"/>
<td>
<xsl:value-of select="substring-before(concat($text, $delimiter), $delimiter)"/>
</td>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Note that for the header we assume that the number of columns is known beforehand. Otherwise you'd have to use a similar recursive template to generate the header cells too.
Check this example
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*">
<xsl:for-each select="rl">
<node>
<idA>
<xsl:value-of select="substring-before(id,';')"/>
</idA>
<idB>
<xsl:value-of select="substring-after(id,';')"/>
</idB>
</node>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Template match not entered

I've the below code in XML.
<?xml version="1.0" encoding="UTF-8"?>
<toa>
<title>TABLE OF HONG KONG LEGISLATION</title>
<subtitle>All references are to paragraph number</subtitle>
<toa-section>
<toa-div level="div1">
<title/>
<toa-entry>
<primary-entry>
<entry-name>Banking Ordinance (Cap.155)</entry-name>
<pgs>7.040</pgs>
</primary-entry>
</toa-entry>
</toa-div>
</toa-section>
</toa>
and the XSLT is as below.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<xsl:text disable-output-escaping="yes"><!DOCTYPE html>
</xsl:text>
<html>
<head>
<xsl:text disable-output-escaping="yes"><![CDATA[</meta>]]></xsl:text>
<title>TABLE OF LEGISLATION</title>
<link rel="stylesheet" href="C:\Users\u0138039\Desktop\In Progress\Company Law-Insolvency 2014 Edition_xml\XSLT\main.css" type="text/css" /><xsl:text disable-output-escaping="yes"><![CDATA[</link>]]></xsl:text>
</head>
<body>
<section class="tr_toa">
<xsl:call-template name="toa"></xsl:call-template>
</section>
</body>
</html>
</xsl:template>
<xsl:template name="toa">
<div class="toa">
<a name="CLI_TOL_01"> </a>
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="toa/title">
<div class="toa-title">
<xsl:value-of select="current()/content-style/text()"/>
</div>
</xsl:template>
<xsl:template match="toa-section">
<div class="toa-section">
<xsl:for-each select="current()/toa-div">
<xsl:call-template name="toa-div" />
</xsl:for-each>
</div>
</xsl:template>
<xsl:template match="toa-div" name="toa-div">
<xsl:variable name="divClass" select="concat('toa-div level-', current()/#level)"></xsl:variable>
<div class="{$divClass}">
<div class="toa-div-title">
<xsl:variable name="fontStyle">
<xsl:value-of select="concat('font-style-',title/content-style/#font-style)"/>
</xsl:variable>
<span class="{$fontStyle}">
<xsl:value-of select="current()/title/content-style/text()"/>
</span>
</div>
<xsl:apply-templates select="toa-entry" />
</div>
</xsl:template>
<xsl:template match="toa-entry">
<xsl:choose>
<xsl:when test="not(preceding-sibling::toa-entry[1]/primary-entry/secondary-entry/node()) and position() != 1">
</xsl:when>
<xsl:otherwise>
<table class="toa-entry">
<tbody>
<xsl:apply-templates select="primary-entry" />
<xsl:if test="not(current()/primary-entry/secondary-entry/node())">
<xsl:apply-templates select="following-sibling::toa-entry[1]" mode="next"/>
</xsl:if>
</tbody>
</table>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="toa-entry" mode="next">
<xsl:apply-templates select="primary-entry"/>
<xsl:if test="not(current()/primary-entry/secondary-entry/node())">
<xsl:apply-templates select="following-sibling::toa-entry[1]" mode="next"/>
</xsl:if>
</xsl:template>
<xsl:template match="primary-entry">
<tr class="primary-entry">
<td class="entry-name">
<xsl:value-of select="current()/entry-name/text()"/>
</td>
<xsl:if test="current()/pgs/node()">
<td class="pgs" >
<xsl:variable name="pgcount" select="count(current()/pgs)"/>
<xsl:for-each select="current()/pgs">
<xsl:apply-templates select="./pgs"></xsl:apply-templates>
</xsl:for-each>
</td>
</xsl:if>
</tr>
<xsl:if test="current()/secondary-entry/node()">
<xsl:for-each select="current()/secondary-entry">
<tr class="secondary-entry">
<td class="entry-name">
<xsl:value-of select="current()/entry-name/text()"/>
</td>
<xsl:if test="current()/pgs/node()">
<td class="pgs" >
<xsl:variable name="pgcount" select="count(current()/pgs)"/>
<xsl:for-each select="current()/pgs">
<xsl:apply-templates></xsl:apply-templates>
</xsl:for-each>
</td>
</xsl:if>
</tr>
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template match="pgs">
<td class="pgs">
<xsl:analyze-string select="." regex="[^,\s]+">
<xsl:matching-substring>
<xsl:variable name="range" select="tokenize(.,'—')"/>
<xsl:variable name="pg" select="tokenize(.,'/')"/>
<xsl:choose>
<xsl:when test="contains($pg[3],'—')">
<xsl:variable name="range-pg" as="item()*">
<xsl:for-each select="$range">
<xsl:sequence select="tokenize(.,'/')"/>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="xs:integer($range-pg[3]) to xs:integer($range-pg[6])">
<a href="er:#HKWBV1_ORD_{
if (string(number($range-pg[1]))!='NaN') then
format-number(number($range-pg[1]),'00')
else
$range-pg[1]}/P{string-join($range-pg[position()=(1,2)],'/')}/{.}">
<xsl:value-of select="concat(string-join($range-pg[position()=(1,2)],'/'),'/',.)"/>
</a>
<xsl:text>, </xsl:text>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<a href="er:#HKWBV1_ORD_{
if (string(number($pg[1]))!='NaN') then
format-number(number($pg[1]),'00')
else
$pg[1]}/P{$pg[1]
}/{string-join($pg[position()>1],'/')}">
<xsl:value-of select="."/>
</a>
</xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</td>
</xsl:template>
</xsl:stylesheet>
though i have written a template match for pgs, while debugging, the flow is not entering the template. Please let me know where am i going wrong.
Thanks
change
<xsl:template match="primary-entry">
<tr class="primary-entry">
<td class="entry-name">
<xsl:value-of select="current()/entry-name/text()"/>
</td>
<xsl:if test="current()/pgs/node()">
<td class="pgs" >
<xsl:variable name="pgcount" select="count(current()/pgs)"/>
<xsl:for-each select="current()/pgs">
<xsl:apply-templates select="./pgs"></xsl:apply-templates>
</xsl:for-each>
</td>
</xsl:if>
to
<xsl:template match="primary-entry">
<tr class="primary-entry">
<td class="entry-name">
<xsl:value-of select="current()/entry-name/text()"/>
</td>
<xsl:if test="current()/pgs/node()">
<xsl:apply-templates select="pgs"/>
</xsl:if>
and do the same for the secondary template.

XSLT for-each failing to pick up fields

I've got a curious item, I'm trying to enumerate through an xml file with my XSLT transform, however I'm after putting a for-each loop into the code my records are not pulling any details (anymore). Help!
XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
<xsl:variable name="pathid" select="montage/path" />
<xsl:variable name="outputstr" select="''" />
<xsl:template match="vista">
<xsl:variable name="pathid" select="path" />
<html>
<body>
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="800" height="200">
<xsl:apply-templates select="item" />
</table>
</div>
</body>
</html>
</xsl:template>
<xsl:template match="item">
<xsl:variable name="monid" select="mon" />
<!--<xsl:value-of select="format-number(position(), '#')" />-->
<xsl:variable name="outputstr" select="file" />
<xsl:choose>
<xsl:when test="position() >= 0 and 4 > position()">
<tr height="200">
<xsl:for-each select="item">
<td width="157" height="108" valign="top">
<xsl:attribute name="height">
<xsl:value-of select="'200'"/>
</xsl:attribute>
<a>
<xsl:attribute name="href">
<xsl:value-of select="$pathid" />
<xsl:value-of select="thepage" />
</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="concat(substring-before($outputstr,'abc'),$pathid)"/>
<xsl:value-of select="$outputstr" />
</xsl:attribute>
</img>
</a>
</td>
</xsl:for-each>
</tr>
</xsl:when>
</xsl:choose>
<xsl:attribute name="id">mon</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
XML
<?xml version="1.0"?>
<vista>
<lastupdate>14/12/2010 14:32</lastupdate>
<path>C:\</path>
<item>
<thepage>00000657.html</thepage>
<heading>heading1</heading>
<file>test123.png</file>
<description>df bhdf hdfhdf hdfh he rher herh df bdf bdfb df rfbd bd</description>
</item>
<item>
<thepage>00000660.html</thepage>
<heading>heading2</heading>
<file>test456.jpg</file>
<description>reh erh erherh erh erher herh</description>
</item>
</vista>
Outputted HTML
<html>
<body>
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="800" height="200">
<tr height="200"></tr>
<tr height="200"></tr>
<tr height="200"></tr>
<tr height="100"></tr>
<tr height="100"></tr>
<tr height="100"></tr>
<tr height="100"></tr>
</table>
</div>
</body>
</html>
The line <xsl:for-each select="item"> - I think you're already in "item". You can't select it again.
You already have an item template - why do you need to for-each inside it?
I expect this to fail, as item nodes do not contain item nodes.

xsl : getting specific node values from a table

My XML Code
<DBE:Attribute name="Test1" type="Table">
<DBE:Table>
<DBE:TableHeader>
<DBE:TableColumn>t1</DBE:TableColumn>
<DBE:TableColumn>t2</DBE:TableColumn>
<DBE:TableColumn>t3</DBE:TableColumn>
<DBE:TableColumn>t4</DBE:TableColumn>
<DBE:TableColumn>t5</DBE:TableColumn>
<DBE:TableColumn>t6</DBE:TableColumn>
<DBE:TableColumn>t7</DBE:TableColumn>
<DBE:TableColumn>t8</DBE:TableColumn>
<DBE:TableColumn>t9</DBE:TableColumn>
<DBE:TableColumn>t10</DBE:TableColumn>
<DBE:TableColumn>t11</DBE:TableColumn>
<DBE:TableColumn>t12</DBE:TableColumn>
<DBE:TableColumn>t13</DBE:TableColumn>
</DBE:TableHeader>
<DBE:TableRow>
<DBE:TableData>0300 </DBE:TableData>
<DBE:TableData/>
<DBE:TableData>25</DBE:TableData>
<DBE:TableData>25</DBE:TableData>
<DBE:TableData>2009/09/03</DBE:TableData>
<DBE:TableData/>
<DBE:TableData>BAG</DBE:TableData>
<DBE:TableData>rrr</DBE:TableData>
<DBE:TableData>Yes</DBE:TableData>
<DBE:TableData>12</DBE:TableData>
<DBE:TableData>2009/03/09</DBE:TableData>
<DBE:TableData>GO</DBE:TableData>
<DBE:TableData/>
</DBE:TableRow>
</DBE:Table>
</DBE:Attribute>
I would like my output to be ->
t7 t5 t1 t13 --> Header
---------------------------------------------------------------
BAG 2009/09/03 0300 GO --> ROW1
.............................................................. --> ROW2
and so on
My XSL code --> (for only selected values to be displayed)
<xsl:for-each select="DBE:Attribute[#name='Test1']/DBE:Table/DBE:TableRow">
<tr bgcolor="white">
<xsl:for-each select="DBE:TableData">
<td>
<xsl:value-of select="node()|*">
</xsl:value-of>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
You could create a selector variable that holds the numeric position of columns you want. See the variable named "selector" below. Once you have that, a simple if in your TableData and TableColumn templates does the rest.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:DBE="foo.bar">
<xsl:variable name="selector">;1;5;7;12;</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="DBE:Attribute">
<html>
<body><xsl:apply-templates/></body>
</html>
</xsl:template>
<xsl:template match="DBE:Table">
<table border="1">
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="DBE:TableHeader">
<tr>
<xsl:apply-templates select="DBE:TableColumn"/>
</tr>
</xsl:template>
<xsl:template match="DBE:TableColumn">
<xsl:variable name="pos">
<xsl:text>;</xsl:text><xsl:value-of select="string(position())"/><xsl:text>;</xsl:text>
</xsl:variable>
<xsl:if test="contains($selector, $pos)">
<th>
<xsl:value-of select="."/>
</th>
</xsl:if>
</xsl:template>
<xsl:template match="DBE:TableRow">
<tr>
<xsl:apply-templates select="DBE:TableData"/>
</tr>
</xsl:template>
<xsl:template match="DBE:TableData">
<xsl:variable name="pos">
<xsl:text>;</xsl:text><xsl:value-of select="string(position())"/><xsl:text>;</xsl:text>
</xsl:variable>
<xsl:if test="contains($selector, $pos)">
<td>
<xsl:value-of select="."/>
</td>
</xsl:if>
</xsl:template>
</xsl:stylesheet>