I am trying to transform the following code into 3 tables:
<bold>Hello</bold>
<bold>world</bold>
<p>Hello world!</p>
<bold>Please</bold>
<bold>help</bold>
<bold>me</bold>
<p>Please help me.</p>
<h1>This community is great<h1>
<bold>Thank</bold>
<bold>you</bold>
<bold>very</bold>
<bold>much</bold>
The final result should look like this:
<table>
<th>NewHeader1<th>
<tr>
<td>Hello</td>
<td>world</td>
</tr>
</table>
<p>Hello world!</p>
<table>
<th>NewHeader2<th>
<tr>
<td>Please</td>
<td>help</td>
<td>me</td>
</tr>
</table>
<p>Please help me.</p>
<h1>This community is great.<h1>
<table>
<th>NewHeader3<th>
<tr>
<td>Thank</td>
<td>you</td>
<td>very</td>
<td>much</td>
</tr>
</table>
Unfortunately, I achieve only to put all the bold-elements in a single table. Thanks for your help!
Given a well-formed XML input such as:
XML
<root>
<bold>Hello</bold>
<bold>world</bold>
<p>Hello world!</p>
<bold>Please</bold>
<bold>help</bold>
<bold>me</bold>
<p>Please help me.</p>
<h1>This community is great</h1>
<bold>Thank</bold>
<bold>you</bold>
<bold>very</bold>
<bold>much</bold>
</root>
you could use a technique known as sibling recursion:
XSLT 1.0
<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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="bold">
<table>
<th>
<xsl:text>NewHeader</xsl:text>
<xsl:number count="bold[not(preceding-sibling::*[1][self::bold])]"/>
</th>
<tr>
<xsl:apply-templates select="." mode="cell"/>
</tr>
</table>
</xsl:template>
<xsl:template match="bold" mode="cell">
<td>
<xsl:value-of select="."/>
</td>
<!-- sibling recursion -->
<xsl:apply-templates select="following-sibling::*[1][self::bold]" mode="cell"/>
</xsl:template>
<xsl:template match="bold[preceding-sibling::*[1][self::bold]]"/>
</xsl:stylesheet>
to produce:
Result
<?xml version="1.0" encoding="UTF-8"?>
<root>
<table>
<th>NewHeader1</th>
<tr>
<td>Hello</td>
<td>world</td>
</tr>
</table>
<p>Hello world!</p>
<table>
<th>NewHeader2</th>
<tr>
<td>Please</td>
<td>help</td>
<td>me</td>
</tr>
</table>
<p>Please help me.</p>
<h1>This community is great</h1>
<table>
<th>NewHeader3</th>
<tr>
<td>Thank</td>
<td>you</td>
<td>very</td>
<td>much</td>
</tr>
</table>
</root>
Related
In below xml file contain many author elements. I have mentioned in xslt and display the first element only. I want to show all author elements. Kindly provide the xslt coding for element not in the text will be shown in browser.
XML Code:
<?xml version="1.0" encoding="US-ASCII" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="view.xsl"?>
<!DOCTYPE WileyML3G [
<!ENTITY % wileyml3g.ent SYSTEM "http://v.wiley.com:3535/dtds/wileyml3g/wiley.ent">
%wileyml3g.ent;
]>
<bibliography xml:id="aic16349-bibl-0001" style="numbered" cited="no">
<title type="main">REFERENCE<!--<QUERY xml:id="Q2"><p>References "3–35" were not cited anywhere in the text. Please provide a citation. Alternatively, delete the items from the list.</p></QUERY>--></title>
<bib xml:id="aic16349-bib-0001">
<citation type="journal" xml:id="aic16349-cit-0001"><!--<QUERY xml:id="Q3"><p>Reference "1" is not cited in the text. Please indicate where it should be cited; or delete from the reference list.</p></QUERY>-->
<author><familyName>Deer</familyName> <givenNames>TR</givenNames></author>, <author><familyName>Provenzano</familyName> <givenNames>DA</givenNames></author>, <author><familyName>Hanes</familyName> <givenNames>M</givenNames></author>, et al. <articleTitle>The Neurostimulation Appropriateness Consensus Committee (NACC) Recommendations for Infection Prevention and Management</articleTitle>. <journalTitle>Neuromodulation.</journalTitle> <pubYear year="2017">2017</pubYear>;<vol>20</vol>(<issue>1</issue>):<pageFirst>31</pageFirst>‐<pageLast>50</pageLast>.</citation>
</bib>
XSLT Code:
<?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>
<h1>HTML VIEW</h1>
<table border="1" cellpadding="10px">
<tr>
<th>Authors</th>
<th>Year</th>
<th>Article_Title</th>
<th>Journal_Title</th>
<th>Volume</th>
<th>Issue</th>
<th>First_Page</th>
<th>Last_Page</th>
</tr>
<xsl:for-each select="bibliography/bib/citation">
<tr>
<td><span style="background-color:skyblue;"><xsl:value-of select="author"/></span>, </td>
<td><xsl:value-of select="pubYear"/></td>
<td width="75%"><xsl:value-of select="articleTitle"/></td>
<td width="25%"><xsl:value-of select="journalTitle"/></td>
<td><xsl:value-of select="vol"/></td>
<td><xsl:value-of select="issue"/></td>
<td><xsl:value-of select="pageFirst"/></td>
<td><xsl:value-of select="pageLast"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
You have tagged the question as XSLT 2.0 and if you really use an XSLT 2.0 processor and use version="2.0" in your stylesheet then <xsl:value-of select="author"/> would output all selected author child elements and you could even use <xsl:value-of select="author" separator=", "/> to have the different authors separated by ,. If you use XSLT 1.0 then use <xsl:appy-templates select="author"/> and <xsl:template match="author"><xsl:if test="position() > 1">, </xsl:if></xsl:value-of select="."/></xsl:template> or use xsl:for-each if you prefer that.
As #Martin suggestion if you really want to use 1.0 then you have to go with for-each author like:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>HTML VIEW</h1>
<table border="1" cellpadding="10px">
<tr>
<th>Authors</th>
<th>Year</th>
<th>Article_Title</th>
<th>Journal_Title</th>
<th>Volume</th>
<th>Issue</th>
<th>First_Page</th>
<th>Last_Page</th>
</tr>
<xsl:for-each select="bibliography/bib/citation">
<tr>
<td>
<span style="background-color:skyblue;">
<xsl:for-each select="author">
<xsl:value-of select="."/>
<xsl:if test="following-sibling::author"><xsl:text>, </xsl:text></xsl:if>
</xsl:for-each>
</span>
</td>
<td><xsl:value-of select="pubYear"/></td>
<td width="75%"><xsl:value-of select="articleTitle"/></td>
<td width="25%"><xsl:value-of select="journalTitle"/></td>
<td><xsl:value-of select="vol"/></td>
<td><xsl:value-of select="issue"/></td>
<td><xsl:value-of select="pageFirst"/></td>
<td><xsl:value-of select="pageLast"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Or it will be done very easy while working with 2.0 like:
Need to change the XSLT version from 1.0 to 2.0
Use #seperator with , in value-of author.
HTML VIEW
Authors
Year
Article_Title
Journal_Title
Volume
Issue
First_Page
Last_Page
Here is a fraction of the XML data I am processing
<?xml version="1.0" encoding="utf-16"?>
<ScorecardSummary>
<DivisionSummary>
<DivisionName>
<string> SYSTEM</string>
</DivisionName>
<ScorecardSummaryByDivision>
<ScorecardSummaryByKPI>
<Header>
<string>Committed Time of Arrival</string>
<string>Goal</string>
<string>1D</string>
<string>7D</string>
<string>QTD</string>
<string>YTD</string>
<string>YTD Event Cars</string>
</Header>
<Data>
<ScorecardContract>
<TypeName>System</TypeName>
<Goal>68</Goal>
<GoalWarning>64.6</GoalWarning>
<TotalCountYear>1234</TotalCountYear>
<Value1D>79</Value1D>
<Value7D>79.2</Value7D>
<ValueQTD>79.1</ValueQTD>
<ValueYTD>73.3</ValueYTD>
</ScorecardContract>
<ScorecardContract>
<TypeName>AG</TypeName>
<Goal>68</Goal>
<GoalWarning>64.6</GoalWarning>
<TotalCountYear>1111</TotalCountYear>
<Value1D>80.9</Value1D>
<Value7D>78.7</Value7D>
<ValueQTD>78.4</ValueQTD>
<ValueYTD>69.7</ValueYTD>
</ScorecardContract>
This is a small part of the XSL that produces the tables:
<xsl:template match="ScorecardSummary/DivisionSummary/DivisionName">
<h1>
<xsl:value-of select="current()/string"/>
</h1>
</xsl:template>
<xsl:template match="ScorecardSummaryByDivision">
<xsl:apply-templates select="current()/ScorecardSummaryByKPI"/>
</xsl:template>
<xsl:template match="ScorecardSummaryByKPI">
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<xsl:choose>
<xsl:when test="count(preceding-sibling::ScorecardSummaryByKPI) mod 6 < 4">
<td>
<table border="1" cellspacing="0" cellpadding="5">
<xsl:apply-templates select="Header"/>
<xsl:apply-templates select="Data"/>
</table>
</td>
</xsl:when>
<xsl:otherwise>
<td>
<table border="1" cellspacing="0" cellpadding="5">
<xsl:apply-templates select="Header"/>
<xsl:apply-templates select="Data"/>
</table>
</td>
</xsl:otherwise>
</xsl:choose>
</tr>
</table>
</xsl:template>
The XSL produces 6 tables repeatedly like this:
1
2
3
4
5
6
1
2
3
4
5
6
But I want to order them like this:
1 4
2 5
3 6
1 4
2 5
3 6
and so on. I tried using this check, but it doesn't work.
count(preceding-sibling::ScorecardSummaryByKPI) mod 6 < 4
Can anyone help?
Explanation
Your table must have two <td> per row (if you want two columns). Your XSLT does generate only one.
Solution is to interate over one half of the list and generate two <td> per iteration.
So first I would define a size of the table. Example:
<xsl:param name="size" select="count(catalog/cd)"/>
Then iterate over only a half of it ($size div 2). The number must be rounded if the input list can contain a non-even number of elements: ceiling($size div 2) (Rounding up to catch last element)
<xsl:for-each select="catalog/cd[ceiling($size div 2) >= position()]">
In each iteration, first render an element itself:
<td><xsl:value-of select="title"/></td>
Then render an appropriate element from the second half of the table (offset is the number defined before: ceiling($size div 2) Half size of the table)
<td><xsl:value-of select="following::cd[ceiling($size div 2)]/title"/></td>
You can wrap element rendering in a separate template to avoid code repeating.
Working example
Check this transformation example with W3C XSL TryIt (http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog):
<?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="/">
<xsl:param name="size" select="count(catalog/cd)"/>
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Title</th>
</tr>
<xsl:for-each select="catalog/cd[ceiling($size div 2) >= position()]">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="following::cd[ceiling($size div 2)]/title"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
It splits CD-catalog (given in example link above) in two columns.
Perhaps something like this is what you are looking for:
(This only shows the idea, you have to adapt it to your input.)
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/" >
<xsl:apply-templates select="//t[count(preceding-sibling::t) < 3]" mode="tables" />
</xsl:template>
<xsl:template match="t" >
{<xsl:value-of select="text()"/>}
</xsl:template>
<xsl:template match="t" mode="tables">
<table border="1">
<tr>
<td>
<table border="1" >
<xsl:apply-templates select="." />
</table>
</td>
<td>
<table border="1">
<xsl:apply-templates select="following-sibling::t[count(preceding-sibling::t) = count(current()/preceding-sibling::t) +3]" />
</table>
</td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
With this short test input xml:
<?xml version="1.0" encoding="utf-8"?>
<xml>
<tables>
<t>1</t>
<t>2</t>
<t>3</t>
<t>4</t>
<t>5</t>
<t>6</t>
</tables>
</xml>
It will generate this output:
<?xml version="1.0"?>
<table xmlns="http://www.w3.org/1999/xhtml" border="1">
<tr>
<td>
<table border="1">
{1}
</table>
</td>
<td>
<table border="1">
{4}
</table>
</td>
</tr>
</table><table xmlns="http://www.w3.org/1999/xhtml" border="1">
<tr>
<td>
<table border="1">
{2}
</table>
</td>
<td>
<table border="1">
{5}
</table>
</td>
</tr>
</table><table xmlns="http://www.w3.org/1999/xhtml" border="1">
<tr>
<td>
<table border="1">
{3}
</table>
</td>
<td>
<table border="1">
{6}
</table>
</td>
</tr>
</table>
Ok, I'm working through some simple tutorials from here:
http://www.cch.kcl.ac.uk/legacy/teaching/7aavdh06/xslt/html/module_06.html
The first exercise involves creating a transformation that produces a certain output. Unfortunately, although I'm close, I get an unwanted element at the start. i.e.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xhtml"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
<xsl:template match="/div/placeName">
<html>
<head />
<body>
<Table>
<tr>
<td>Place Name</td>
<td>
<xsl:value-of select="name" />
</td>
</tr>
<tr>
<td>Place Name (regularised)</td>
<td>
<xsl:value-of select="#reg" />
</td>
</tr>
<tr>
<td>National Grid Reference</td>
<td>
<xsl:value-of select="#key" />
</td>
</tr>
<tr>
<td>Type of building/monument</td>
<td>
<xsl:value-of select="settlement/#type" />
</td>
</tr>
</Table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
but the output I'm getting is:
Location
Place Name Old Warden
Place Name (regularised) Old Warden, St Leonard
National Grid Reference TL 137 443
Type of building/monument Parish church
The rest is fine but the 'Location' is unwanted. The source XML is at the link above. Any idea how I stop the unwanted text appearing? Or, better still, tell me where I'm going wrong! :)
Edit: Here is the output
<?xml version="1.0" encoding="utf-8" ?>
Location
<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
<table>
<tr>
<td>Place Name</td>
<td>Old Warden</td>
</tr>
<tr>
<td>Place Name (regularised)</td>
<td>Old Warden, St Leonard</td>
</tr>
<tr>
<td>National Grid Reference</td>
<td>TL 137 443</td>
</tr>
<tr>
<td>Type of building/monument</td>
<td>Parish church</td>
</tr>
</table>
</body>
</html>
As Stivel mentions, the "Location" text does come from the head element in your XML.
<div type="location">
<head n="I">Location</head>
<placeName reg="Old Warden, St Leonard" key="TL 137 443">
The reason it is appearing is because of XSTL's built-in templates which it uses when you do not specify a match for an element it is looking for in your XSLT.
You can read up on built-in templates at the W3C page but in short, if XSLT can't find a match it will either continue processing the element's children (without copying the element), or in the case of text or attributes, output the value.
XSLT will start by looking for a match for the document element first, and if you have not provided a template, it will continue looking for a template for the root element, and then its children, and so on.
In your case, you have not provided a template to match anything until /div/placeName, this means XSLT will use the built-in template for the div element. This has two children; head and placeName. You have a template it can use for placeName, but not head and so the built-in template ends up outputing the text for head because you have not told it anything otherwise.
The solution is to simply to add a template to ignore the head element
<xsl:template match="/div/head" />
Here is the full XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xhtml"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" indent="yes" />
<xsl:template match="/div/head" />
<xsl:template match="/div/placeName">
<html>
<head />
<body>
<Table>
<tr>
<td>Place Name</td>
<td>
<xsl:value-of select="name" />
</td>
</tr>
<tr>
<td>Place Name (regularised)</td>
<td>
<xsl:value-of select="#reg" />
</td>
</tr>
<tr>
<td>National Grid Reference</td>
<td>
<xsl:value-of select="#key" />
</td>
</tr>
<tr>
<td>Type of building/monument</td>
<td>
<xsl:value-of select="settlement/#type" />
</td>
</tr>
</Table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
When you use this, this should give the output you need.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xhtml" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="div">
<xsl:apply-templates select="placeName"/>
</xsl:template>
<xsl:template match="placeName">
<html>
<head />
<body>
<Table>
<tr>
<td>Place Name</td>
<td>
<xsl:value-of select="name" />
</td>
</tr>
<tr>
<td>Place Name (regularised)</td>
<td>
<xsl:value-of select="#reg" />
</td>
</tr>
<tr>
<td>National Grid Reference</td>
<td>
<xsl:value-of select="#key" />
</td>
</tr>
<tr>
<td>Type of building/monument</td>
<td>
<xsl:value-of select="settlement/#type" />
</td>
</tr>
</Table>
</body>
</html>
</xsl:template>
Probably your <head/> may refer
<head n="I">Location</head>
remove <head/> in xsl and check that.
I need to turn
<question>
<static><![CDATA[Static Data]]></static>
<debit-row />
<debit-row />
<credit-row />
<header><![CDATA[Header HTML 1]]></header>
<debit-row />
<debit-row />
<credit-row />
</question>
into
<p>Static Data</p>
<ul>
<li>
<table>
<tr><td> debit row </td></tr>
<tr><td> credit row </td></tr>
<tr><td> credit row </td></tr>
</table>
</li>
<li> Header HTML 1
<table>
<tr><td> debit row </td></tr>
<tr><td> debit row </td></tr>
<tr><td> credit row </td></tr>
</table>
</li>
</ul>
Essentially, either a header or a debit-row indicates the start of a new chunk. Each chunk is a list item. Each set or rows is a table (as a rule, credit rows always come last so it's easy to tell when to start the table).
XSLT and XPATH seem very difficult and I'm having a very hard time looking up anything that I want to do at all, so if anyone has an excellent reference, I would appreciate that too.
I've started out with this xsl:
<xsl:template match="question">
<xsl:apply-templates select="static|header|debit-row[preceding-sibling::*[1] != header]" />
</xsl:template>
This is not a good start, because the templates are not applied to any debit-row at all, but they should be applied to the very first debit-row (it does not have a header element preceding it). Is that expression wrong?
Even if I get that to work, I need to find a way to say "Open a <ul> if this is the very first header or debit-row," and I'm not sure how to do that when applying the header/debit-row template. debit-row each has its own xml to be applied too (it needs a table row and td). I also have to open and close the table appropriately before the first debit-row and after the last credit-row.
I would seriously appreciate any help as I am stuck even getting the simple xpath expression above to work correctly.
I. XSLT 1.0 solution:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kFollowing"
match="*[not(self::static or self::header)]"
use="generate-id(preceding-sibling::*
[self::static
or
self::header
][1]
)"/>
<xsl:template match="/*[static]">
<p><xsl:value-of select="static"/></p>
<ul>
<xsl:apply-templates select="static|header"/>
</ul>
</xsl:template>
<xsl:template match="static|header">
<li>
<xsl:value-of select=
"concat(self::header, '
')"/>
<table>
<xsl:apply-templates
select="key('kFollowing', generate-id())"/>
</table>
</li>
</xsl:template>
<xsl:template match=
"*/*[not(self::static or self::header)]">
<tr>
<td>
<xsl:value-of select=
"translate(name(),'-', ' ')"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<question>
<static><![CDATA[Static Data]]></static>
<debit-row />
<debit-row />
<credit-row />
<header><![CDATA[Header HTML 1]]></header>
<debit-row />
<debit-row />
<credit-row />
</question>
produces the wanted, correct result:
<p>Static Data</p>
<ul>
<li>
<table>
<tr>
<td>debit row</td>
</tr>
<tr>
<td>debit row</td>
</tr>
<tr>
<td>credit row</td>
</tr>
</table>
</li>
<li>Header HTML 1
<table>
<tr>
<td>debit row</td>
</tr>
<tr>
<td>debit row</td>
</tr>
<tr>
<td>credit row</td>
</tr>
</table>
</li>
</ul>
Explanation: Positional grouping using a key to define all elements that belong to a group.
II. XSLT 2.0 solution:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*[static]">
<p><xsl:value-of select="static"/></p>
<ul>
<xsl:for-each-group select="*"
group-starting-with="static|header">
<li>
<xsl:value-of separator="
" select=
"current-group()[1][self::header], ''"/>
<table>
<xsl:apply-templates
select="current-group()[position() gt 1]"/>
</table>
</li>
</xsl:for-each-group>
</ul>
</xsl:template>
<xsl:template match=
"*/*[not(self::static or self::header)]">
<tr>
<td>
<xsl:value-of select=
"translate(name(),'-', ' ')"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
when this XSLT 2.0 transformation is applied to the same XML document (above), again the same, correct result is produced:
<p>Static Data</p>
<ul>
<li>
<table>
<tr>
<td>debit row</td>
</tr>
<tr>
<td>debit row</td>
</tr>
<tr>
<td>credit row</td>
</tr>
</table>
</li>
<li>Header HTML 1
<table>
<tr>
<td>debit row</td>
</tr>
<tr>
<td>debit row</td>
</tr>
<tr>
<td>credit row</td>
</tr>
</table>
</li>
</ul>
Explanation: Using the XSLT 2.0 <xsl:for-each-group> instruction with a group-starting-with attribute. Also using the standard XSLT 2.0 function current-group().
I'm calling a template:
<table>
<xsl:apply-templates select="data/pics/row"/>
</table>
The template is
<xsl:template match="row">
<tr>
<xsl:for-each select="td">
<td border="0">
<a href="{#referencddePage}">
<img src="{pic/#src}" width="{pic/#width}" height="{pic/#height}"/>
</a>
</td>
</xsl:for-each>
</tr>
</xsl:template>
My XML is:
<?xml version="1.0" encoding="iso-8859-8"?>
<?xml-stylesheet type="text/xsl" href="xslFiles\smallPageBuilder.xsl"?>
<data pageNo="3" referencePage="xxxxxxxxxxxxxxx.xml">
<pics>
<row no="0">
<td col="0">
<pic src="A.jpg" width="150" height="120"></pic>
</td>
</row>
</pics>
</data>
I want the line :a h r e f="{#referencddePage}" to get the input from
the root, :a h r e f= "{#referencddePage}"..., but I'm already in the <td level>.
I want the line :a h r e
f="{#referencddePage}" to get the
input from the root :a h r e f=
"{#referencddePage}"... but I'm
already in the <td level>
In case it is a rule that the #referencePage attribute is always an attribute of the top element, then it can always be accessed as:
/*/#referencePage
Therefore, in your code you'll have:
<a href="{/*/#referencePage}">
I would recommend not to use <xsl:for-each> and to use only and`. In this way the resulting XSLT code is more understandable and can be more easily modified in the future:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="row">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="td">
<td border="0">
<a href="{/*/#referencePage}">
<xsl:apply-templates/>
</a>
</td>
</xsl:template>
<xsl:template match="pic">
<img src="{#src}" width="{#width}" height="{#height}"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document,
<data pageNo="3" referencePage="xxxxxxxxxxxxxxx.xml">
<pics>
<row no="0">
<td col="0">
<pic src="A.jpg" width="150" height="120"></pic>
</td>
</row>
</pics>
</data>
the wanted output is produced:
<tr>
<td border="0">
<a href="xxxxxxxxxxxxxxx.xml">
<img src="A.jpg" width="150" height="120"/>
</a>
</td>
</tr>
See how each template is so very simple. Also, the code is further simplified.
Now, instead of:
<img src="{pic/#src}" width="{pic/#width}" height="{pic/#height}"/>
we have only:
<img src="{#src}" width="{#width}" height="{#height}"/>
Use an XPATH that "jumps" to the top of the document with a leading slash, then walk down the tree:
/data/#referencePage
Applying it to your stylesheet:
<xsl:template match="row">
<tr>
<xsl:for-each select="td">
<td border="0">
<a href="{/data/#referencePage}">
<img src="{pic/#src}" width="{pic/#width}" height="{pic/#height}"/>
</a>
</td>
</xsl:for-each>
</tr>
</xsl:template>