Hi I'm new to XSLT and I'm trying to display the value of a parent node along with the values of my data.
I have this XML..
<?xml-stylesheet type="text/xsl" href="Sample.xsl"?>
<DataView Client="Client1" ID="1000" TimeStamp="12/7/2011 5:35:09 PM">
<Group ID="5000" Name="GroupName1">
<SubGroup ID="7000" Order="0" Name="NameIWant1">
<Data ID="1" Name="DataName1" Order="0">1</Data>
<Data ID="2" Name="DataName2" Order="0">2</Data>
<Data ID="3" Name="DataName3" Order="0">3</Data>
<Data ID="12" Name="DataName4" Order="0">4</Data>
</SubGroup>
<SubGroup ID="8000" Order="0" Name="NameIWant2">
<Data ID="1" Name="DataName1" Order="0">6</Data>
<Data ID="2" Name="DataName2" Order="0">7</Data>
<Data ID="3" Name="DataName3" Order="0">8</Data>
<Data ID="12" Name="DataName4" Order="0">9</Data>
</SubGroup>
</Group>
</DataView>
Ive written the basic XSL to walk the values
<?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>
<h2>My Data</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>DataName1</th>
<th>DataName2</th>
<th>DataName3</th>
<th>DataName4</th>
</tr>
<xsl:for-each select="DataView/Group/SubGroup">
<tr>
<xsl:for-each select="Data">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
How do I retrieve and display the attribute value of Subgroup "Name" so my table looks like this...
MyData
NameIWant1 1 2 3 4
NameIWant2 6 7 8 9
Any help is very much appreciated!!
The simple, short answer is to add the following just before the inner for-each loop:
<td><xsl:value-of select="#Name"/></td>
You're already in the context of a DataView/Group/SubGroup node, so you just need to use the attribute axis specifier (#) to select one of its attributes by name.
However, (as usual) I think this is better expressed using individual templates. The for-each loop is almost never necessary in XSLT. The following stylesheet produces the desired result:
<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:strip-space elements="*"/>
<xsl:template match="/">
<html>
<body>
<h2>My Data</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Subgroup</th>
<th>DataName1</th>
<th>DataName2</th>
<th>DataName3</th>
<th>DataName4</th>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="SubGroup">
<tr>
<td><xsl:value-of select="#Name"/></td>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="Data">
<td><xsl:apply-templates/></td>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<body>
<h2>My Data</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Subgroup</th>
<th>DataName1</th>
<th>DataName2</th>
<th>DataName3</th>
<th>DataName4</th>
</tr>
<tr>
<td>NameIWant1</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>NameIWant2</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
</body>
</html>
<table border="1">
<thead>
<tr bgcolor="#9acd32">
<th>SubGroupName</th>
<th>DataName1</th>
<th>DataName2</th>
<th>DataName3</th>
<th>DataName4</th>
</tr>
</thead>
<tbody>
<xsl:for-each select="DataView/Group/SubGroup">
<tr>
<td>
<xsl:value-of select="#Name"/>
</td>
<xsl:for-each select="Data">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
Related
Find the following line below in the xsl:
var tdElem = document.getElementById('???') <!-- Would like to get td here, if possible -->
Is it possible to generate a unique id for the containing td and concat that into my javascript function?
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="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td style="background-color: 'can_be_any_color_here'">
<xsl:value-of select="title"/>
<script>
var tdElem = document.getElementById('???') <!-- Would like to get td here, if possible -->
var bgColor = tdElem.style.backgroundColor;
var textColor = mycolorcontrastfx(bgcolor);
tdElem.style.color = textColor;
</script>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
xml
<?xml-stylesheet type="text/xsl" href="blackorwhite.xslt"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
</cd>
<cd>
<title>Hide your heart</title>
</cd>
</catalog>
You can use generate-id to generate a unique ID for an XML node
<xsl:variable name="id" select="concat('CDTableCell', generate-id())" />
Or, in this particular case, you make do with using just the position of the cd selected
<xsl:variable name="id" select="concat('CDTableCell', position())" />
To assign the id to the td node, you can use Attribute Value Templates
<td id="{$id}" style="..." />
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<script>
function mycolorcontrastfx(bgColor)
{
return '#000000';
}
</script>
</head>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<xsl:variable name="id" select="concat('CDTableCell', position())" />
<td id="{$id}" style="background-color:#FFFFFF">
<xsl:value-of select="title"/>
<script>
var tdElem = document.getElementById('<xsl:value-of select="$id" />')
var bgColor = tdElem.style.backgroundColor;
var textColor = mycolorcontrastfx(bgColor);
tdElem.style.color = textColor;
</script>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Here is my following xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>price</th>
</tr>
<xsl:for-each select="library/book/authors/author[first='Harry'and last='Potter'] ">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Below is my XSL stylesheet
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>price</th>
</tr>
<xsl:for-each select="library/book/authors/author[first='Harry'and last='Potter'] ">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I am unable to display title and price as they are the parent element and I'm giving condition to my child element. And I need to display information regarding my parents element.
You can get access to a parent node in your XPath expression using "../". For example:
<xsl:value-of select="../../title"/>
<xsl:value-of select="../../price"/>
In this case I'd be inclined to suggest that you use a predicate so the for-each operates over the book elements instead of the author ones:
<xsl:for-each select="library/book[authors/author[first='Harry'and last='Potter']] ">
This says "select all the book elements that have at least one author named Harry Potter".
I have the following xml:
<Activity>
<item>
<task>XXX</task>
<assignto>User1</assignto>
</item>
<item>
<task>YYY</task>
<assignto>User2</assignto>
</item>
<item>
<task>ZZZ</task>
<assignto>User1</assignto>
</item>
<team>
<member>User1</member>
<member>User2</member>
<team>
</Activity>
I want to generate using XSL a count of task per member in the team.
User- Count
user1- 2
user2- 1
so far I have the following XSL:
<?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>
<table>
<tr>
<th>User</th>
<th>Task Count</th>
</tr>
<xsl:for-each select="Activity/team/member">
<tr>
<td><xsl:value-of select="node()" /></td>
<td><xsl:value-of select="count(/Activity/item[assignto='user1'])" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
so far I hardcoded 'user1', I would like to filter based on the current member in the for each loop.
Can someone help, please?
Thanks,
here you go, store the member in a variable and test on that variable. You also have an error in your source XML, you need /team.
<xsl:template match="/">
<html>
<body>
<table>
<tr>
<th>User</th>
<th>Task Count</th>
</tr>
<xsl:for-each select="Activity/team/member">
<xsl:variable name="assignto">
<xsl:value-of select="."/>
</xsl:variable>
<tr>
<td><xsl:value-of select="node()" /></td>
<td><xsl:value-of select="count(/Activity/item[assignto=$assignto])" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
output is:
<html>
<body>
<table>
<tr>
<th>User</th>
<th>Task Count</th>
</tr>
<tr>
<td>User1</td>
<td>2</td>
</tr>
<tr>
<td>User2</td>
<td>1</td>
</tr>
</table>
</body>
</html>
Just replace your reference to 'user1' with a reference to the current node at the start of the XPath address, which is done using the current() function:
t:\ftemp>type activity.xml
<Activity>
<item>
<task>XXX</task>
<assignto>User1</assignto>
</item>
<item>
<task>YYY</task>
<assignto>User2</assignto>
</item>
<item>
<task>ZZZ</task>
<assignto>User1</assignto>
</item>
<team>
<member>User1</member>
<member>User2</member>
</team>
</Activity>
t:\ftemp>call xslt activity.xml activity.xsl
<html>
<body>
<table>
<tr>
<th>User</th>
<th>Task Count</th>
</tr>
<tr>
<td>User1</td>
<td>2</td>
</tr>
<tr>
<td>User2</td>
<td>1</td>
</tr>
</table>
</body>
</html>
t:\ftemp>type activity.xsl
<?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>
<table>
<tr>
<th>User</th>
<th>Task Count</th>
</tr>
<xsl:for-each select="Activity/team/member">
<tr>
<td><xsl:value-of select="node()" /></td>
<td><xsl:value-of select="count(/Activity/item[assignto=current()])" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
t:\ftemp>rem Done!
If you want to filter based on the current member, you can just use the "current()" function:
<xsl:value-of select="count(/Activity/item[assignto=current()])" />
However, you might benefit from using a key here, to make the counting more efficient. First define your key like so:
<xsl:key name="item" match="item" use="assignto" />
Then you can write your count like so:
<xsl:value-of select="count(key('item', .))" />
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="item" match="item" use="assignto" />
<xsl:template match="/">
<html>
<body>
<table>
<tr>
<th>User</th>
<th>Task Count</th>
</tr>
<xsl:for-each select="Activity/team/member">
<tr>
<td><xsl:value-of select="node()" /></td>
<td><xsl:value-of select="count(key('item', .))" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Below is my input xml
<Record>
<Header>
<field1/>
</Header>
<Body>
<firstname>x1</firstname>
<lastname>y1</lastname>
<company>Test1</company>
<Body>
<Body>
<firstname>x2</firstname>
<lastname>y2</lastname>
<company></company>
<Body>
<Body>
<firstname>x3</firstname>
<lastname>y3</lastname>
<company>Test2</company>
<Body>
</Record>
I am trying to loop through body and check if the company value is blank,to output the corresponding first name and last name.This whole output,I am mapping to DATA on the target using xslt mapper.Can someone help me with the below code which is not working
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="Namespace">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="NAME" select="/RecordSet/Body"/>
<xsl:variable name="break"><br></xsl:variable>
<xsl:variable name="tableB"><table Border="1" BorderColor="#000000" cellpadding="4" cellspacing="0" ></xsl:variable>
<xsl:variable name="tableE"></table></xsl:variable>
<xsl:variable name="trB"><tr></xsl:variable>
<xsl:variable name="trE"></tr></xsl:variable>
<xsl:variable name="tdB"><td></xsl:variable>
<xsl:variable name="tdE"></td></xsl:variable>
<xsl:variable name="nbsp"> </xsl:variable>
<xsl:variable name="thB"><tr BGCOLOR="#CCCCCC"></xsl:variable>
<xsl:template match="/">
<DATA>
<xsl:value-of select="$tableB"/>
<xsl:value-of select="$thB"/>
<xsl:value-of select="$tdB"/><B>FirstName</B>
<xsl:value-of select="$nbsp"/>
<xsl:value-of select="$tdE"/>
<xsl:value-of select="$tdB"/><B>LASTNAME </B>
<xsl:value-of select="$nbsp"/>
<xsl:value-of select="$tdE"/>
<xsl:value-of select="$trE"/>
<xsl:value-of select="$trB"/>
<xsl:for-each select="$NAME/Body[string-length(company) > 0]">
<xsl:value-of select="$tdB"/>
<xsl:value-of select="$MT_NAME/firstname"/>
<xsl:value-of select="$nbsp"/>
<xsl:value-of select="$tdE"/>
<xsl:value-of select="$tdB"/>
<xsl:value-of select="$MT_NAME/lastname"/>
<xsl:value-of select="$nbsp"/>
<xsl:value-of select="$tdE"/>
<xsl:if test="position() mod 2 = 0">
<xsl:value-of select="$trE"/>
</xsl:if>
</xsl:for-each>
<xsl:value-of select="$tableE"/>
</DATA>
The output should be
|FIRSTNAME|LASTNAME|
| X2 | Y2 |
The provided code doesn't produce HTML at all -- it produces strings -- one-dimensional text.
Also, AFAIK, DATA isn't an HTML element.
Also, the provided "XML" is severely malformed.
Here is an example how to produce an HTML table with XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<table>
<thead>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Company</td>
</tr>
</thead>
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="Body">
<tr>
<xsl:apply-templates select="*"/>
</tr>
</xsl:template>
<xsl:template match="Body/*">
<td> </td>
</xsl:template>
<xsl:template match="Body/*[normalize-space()]">
<td><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following XML document (the provided severely malformed text -- corrected):
<Record>
<Header>
<field1/>
<Body>
<firstname>x1</firstname>
<lastname>y1</lastname>
<company>Test1</company>
</Body>
<Body>
<firstname>x2</firstname>
<lastname>y2</lastname>
<company></company>
</Body>
<Body>
<firstname>x3</firstname>
<lastname>y3</lastname>
<company>Test2</company>
</Body>
</Header>
</Record>
A meaningful and sensible HTML table is produced:
<table>
<thead>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Company</td>
</tr>
</thead>
<tr>
<td>x1</td>
<td>y1</td>
<td>Test1</td>
</tr>
<tr>
<td>x2</td>
<td>y2</td>
<td> </td>
</tr>
<tr>
<td>x3</td>
<td>y3</td>
<td>Test2</td>
</tr>
</table>
I have to take a guess at your requirements as they are not very well explained.
This XSLT 1.0 style-sheet...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="/">
<DATA>
<table Border="1" BorderColor="#000000" cellpadding="4" cellspacing="0">
<tr BGCOLOR="#CCCCCC">
<th>First name</th>
<th>Last name</th>
<th>Company</th>
</tr>
<xsl:apply-templates select="*/Body" />
</table>
</DATA>
</xsl:template>
<xsl:template match="Body">
<tr>
<td><xsl:value-of select="firstname" /></td>
<td><xsl:value-of select="lastname" /></td>
<td>
<xsl:choose>
<xsl:when test="company!=''" >
<xsl:value-of select="company" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(firstname,' ',lastname)" />
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
... when applied on this document...
<Record>
<Header>
<field1/>
</Header>
<Body>
<firstname>x1</firstname>
<lastname>y1</lastname>
<company>Test1</company>
</Body>
<Body>
<firstname>x2</firstname>
<lastname>y2</lastname>
<company/>
</Body>
<Body>
<firstname>x3</firstname>
<lastname>y3</lastname>
<company>Test2</company>
</Body>
</Record>
...will yield...
<DATA>
<table Border="1" BorderColor="#000000" cellpadding="4" cellspacing="0">
<tr BGCOLOR="#CCCCCC">
<th>First name</th>
<th>Last name</th>
<th>Company</th>
</tr>
<tr>
<td>x1</td>
<td>y1</td>
<td>Test1</td>
</tr>
<tr>
<td>x2</td>
<td>y2</td>
<td>x2 y2</td>
</tr>
<tr>
<td>x3</td>
<td>y3</td>
<td>Test2</td>
</tr>
</table>
</DATA>
Note
Notice that the middle record has a cell value of 'x2 y2' for Company as per stated requirements when the input Company is empty or missing.
Update
The input document is still badly malformed. In consideration of the OP's updated requirements, this XSLT 1.0 style-sheet...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="/">
<DATA>
<table Border="1" BorderColor="#000000" cellpadding="4" cellspacing="0">
<tr BGCOLOR="#CCCCCC">
<th>First name</th>
<th>Last name</th>
</tr>
<xsl:apply-templates select="*/Body[company='']" />
</table>
</DATA>
</xsl:template>
<xsl:template match="Body">
<tr>
<td><xsl:value-of select="firstname" /></td>
<td><xsl:value-of select="lastname" /></td>
</tr>
</xsl:template>
</xsl:stylesheet>
...when applied to the same input document as before, yields...
<DATA>
<table Border="1" BorderColor="#000000" cellpadding="4" cellspacing="0">
<tr BGCOLOR="#CCCCCC">
<th>First name</th>
<th>Last name</th>
</tr>
<tr>
<td>x2</td>
<td>y2</td>
</tr>
</table>
</DATA>
First XML
<?xml version="1.0"?>
<response>
<status>
<code>0</code>
</status>
<newsList>
<news>
<id>1</id>
<title>some</title>
<date>30.11.2011T00:00.00</date>
<shortText>some short text</shortText>
<important>LOW</important>
</news>
Second XML
<?xml version="1.0"?>
<response>
<status>
<code>0</code>
</status>
<newsList>
<news>
<id>1</id>
<text>
Some text here
</text>
</news>
The result should be dysplaing title date and short Text from the first XML and the text from the second XML.
Below the XSLT I got so far.
<xsl:template match="response">
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">shortText</th>
<th align="left">date</th>
<th align="left">text</th>
</tr>
<xsl:for-each select="newsList/news">
<tr>
<td><xsl:value-of select="title" /></td>
<td><xsl:value-of select="shortText" /></td>
<td><xsl:value-of select="date" /></td>
<td><xsl:value-of select="document('news-details.xml')//news[id=$id_news]/text"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
But this will always show the text from the news number 1.
I know is't not possible to update the vaulue but how can get it done?
Here is an example using a key:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" version="5.0"/>
<xsl:param name="url2" select="'news-details.xml'"/>
<xsl:variable name="doc2" select="document($url2, /)"/>
<xsl:key name="k1" match="news" use="id"/>
<xsl:template match="/">
<html>
<head>
<title>Example</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Title</th>
<th>short text</th>
<th>date</th>
<th>text</th>
</tr>
</thead>
<tbody>
<xsl:apply-templates select="//news"/>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="news">
<xsl:variable name="id" select="id"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="shortText"/></td>
<td><xsl:value-of select="date"/></td>
<td>
<xsl:for-each select="$doc2">
<xsl:value-of select="key('k1', $id)/text"/>
</xsl:for-each>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="response">
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">shortText</th>
<th align="left">date</th>
<th align="left">text</th>
</tr>
<xsl:apply-templates select="newsList/news"/>
</table>
</xsl:template>
<xsl:template match="newsList/news">
<xsl:variable name="id_news" select="ID"/>
<tr>
<td><xsl:value-of select="title" /></td>
<td><xsl:value-of select="shortText" /></td>
<td><xsl:value-of select="date" /></td>
<td>
<xsl:apply-templates select="document('news-details.xml')//news[id=$id_news]/text"/>
</td>
</tr>
</xsl:template>
<xsl:template match="text">
<xsl:value-of select="."/>
</xsl:template>