Source-XML:
<data>
<item>
<values>
<element1>
<language>EN</language>
<text>text</text>
</element1>
<element2>
<language>DE</language>
<text>Text</text>
</element2>
</values>
</item>
<item>
<values>
<element5>
<language>EN</language>
<text>description</text>
</element5>
<element6>
<language>DE</language>
<text>Beschreibung</text>
</element6>
</values>
</item> </data>
I want to get all the elements in language 'EN'. First I have a loop, where I saved the elment names in a variable. In the next step I want to get only the elements in language "EN". I need in the result of this step only the element-name and text which have the language 'EN' to build a table.
I tried this:
<xsl:param name="element" select="'element1'"/>
<xsl:template match="/">
<xsl:if test="data/item/values[local-name()=$element]/language[text()='EN']">
</xsl:if>
</xsl:template>
And the output XSLT should be something like:
<table id="123">
<tgroup cols="2">
<colspec colname="c1" colnum="1" colwidth="1.0*"/>
<colspec colname="c2" colnum="2" colwidth="1.0*"/>
<thead>
<row>
<entry>Name</entry>
<entry>Values</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<ph>element1</ph>
</entry>
<entry>text</entry>
</row>
<row>
<entry>
<ph>element5</ph>
</entry>
<entry>description</entry>
</row>
</tbody>
</tgroup>
</table>
I know that there are other ways to solve this problem. But for other steps in the transformation it is important to test every element separately.
Thanks in advance!
<xsl:output method="xml" indent="yes"/>
<xsl:template match="data">
<xsl:for-each-group select="item" group-by="values/*[language = 'EN']">
<xsl:for-each select="current-group()">
<xsl:element name="{current-group()/values/*[language = 'EN']/local-name()}">
<language>
<xsl:value-of select="descendant::language[text() = 'EN']"/>
</language>
<txt>
<xsl:value-of select="current-group()/values/*[language = 'EN']/text"/>
</txt>
</xsl:element>
</xsl:for-each>
</xsl:for-each-group>
</xsl:template>
You may do like this
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common" version="1.0">
<xsl:output indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<xsl:variable name="ENelements">
<xsl:for-each select="/data/item/values/*[language='EN']">
<element name="{local-name()}"><xsl:value-of select="text" /></element>
</xsl:for-each>
</xsl:variable>
<table id="123">
<tgroup cols="2">
<colspec colname="c1" colnum="1" colwidth="1.0*"/>
<colspec colname="c2" colnum="2" colwidth="1.0*"/>
<thead>
<row>
<entry>Name</entry>
<entry>Values</entry>
</row>
</thead>
<tbody>
<xsl:for-each select="exsl:node-set($ENelements)/*">
<row>
<entry>
<ph><xsl:value-of select="#name" /></ph>
</entry>
<entry><xsl:value-of select="." /></entry>
</row>
</xsl:for-each>
</tbody>
</tgroup>
</table>
</xsl:template>
</xsl:stylesheet>
http://xsltfiddle.liberty-development.net/jyRYYig
AFAICT, it could be simply:
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:template match="/data">
<table id="123">
<tgroup cols="2">
<colspec colname="c1" colnum="1" colwidth="1.0*"/>
<colspec colname="c2" colnum="2" colwidth="1.0*"/>
<thead>
<row>
<entry>Name</entry>
<entry>Values</entry>
</row>
</thead>
<tbody>
<xsl:for-each select="item">
<xsl:variable name="elem" select="values/*[language='EN']" />
<row>
<entry>
<ph>
<xsl:value-of select="name($elem)"/>
</ph>
</entry>
<entry>
<xsl:value-of select="$elem/text"/>
</entry>
</row>
</xsl:for-each>
</tbody>
</tgroup>
</table>
</xsl:template>
</xsl:stylesheet>
Related
I'm creating an HTML table that is based on dynamic columns(Hostname) and rows(VLAN). I'm running into an issue after the first position data(1 row for all hosts) in the is written to the table; selects the 2nd position data just fine, but the $vCol variable takes it back to the first line of the $vCols variable.
Appreciate any direction you can offer. Thanks
XSLT-2.0 code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:key name="kHostNameByValue" match="Hostname" use="."/>
<xsl:key name="kVLAN" match="Hostname" use="."/>
<xsl:variable name="vCols" select="/*/*/Hostname[generate-id()=generate-id(key('kHostNameByValue',.)[1])]"/>
<xsl:variable name="vMaxRows">
<xsl:for-each select="$vCols">
<xsl:sort data-type="number" order="descending" select="count(key('kVLAN', .))"/>
<xsl:if test="position() = 1">
<xsl:value-of select="count(key('kVLAN', .))"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:template match="DocumentRoot">
<table border="1">
<!-- Print out column headings by Hostname -->
<tr>
<xsl:apply-templates select="$vCols"/>
</tr>
<!-- Print out VLANs by Hostname -->
<xsl:for-each-group select="(//Row)[not(position() > $vMaxRows)]" group-by="VLAN">
<tr>
<xsl:variable name="vPos" select="position()"/>
<!-- Issue on 2nd position when $vCols goes back to 1st hostname at line 3 -->
<xsl:for-each select="$vCols">
<td>
<xsl:value-of select="..[$vPos]/VLAN"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each-group>
</table>
</xsl:template>
<xsl:template match="Hostname">
<td>
<b>
<xsl:value-of select="." />
</b>
</td>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Here's the sample XML data.
<?xml version="1.0" encoding="UTF-8"?>
<DocumentRoot>
<Row>
<Hostname>switch-1</Hostname>
<HostIP>10.29.178.102</HostIP>
<VLAN>10</VLAN>
<VLANName>VLAN-10</VLANName>
</Row>
<Row>
<Hostname>switch-1</Hostname>
<HostIP>10.29.178.102</HostIP>
<VLAN>500</VLAN>
<VLANName>VLAN-500</VLANName>
</Row>
<Row>
<Hostname>switch-2</Hostname>
<HostIP>10.29.178.103</HostIP>
<VLAN>11</VLAN>
<VLANName>VLAN-11</VLANName>
</Row>
<Row>
<Hostname>switch-2</Hostname>
<HostIP>10.29.178.103</HostIP>
<VLAN>501</VLAN>
<VLANName>VLAN-500</VLANName>
</Row>
<Row>
<Hostname>switch-3</Hostname>
<HostIP>10.29.170.1</HostIP>
<VLAN>15</VLAN>
<VLANName>VLAN-15</VLANName>
</Row>
<Row>
<Hostname>switch-3</Hostname>
<HostIP>10.29.170.1</HostIP>
<VLAN>25</VLAN>
<VLANName>VLAN-25</VLANName>
</Row>
<Row>
<Hostname>switch-3</Hostname>
<HostIP>10.29.170.1</HostIP>
<VLAN>35</VLAN>
<VLANName>VLAN-35</VLANName>
</Row>
<Row>
<Hostname>switch-3</Hostname>
<HostIP>10.29.170.1</HostIP>
<VLAN>45</VLAN>
<VLANName>VLAN-45</VLANName>
</Row>
<Row>
<Hostname>switch-3</Hostname>
<HostIP>10.29.170.1</HostIP>
<VLAN>55</VLAN>
<VLANName>VLAN-55</VLANName>
</Row>
</DocumentRoot>
Output (Actual and desired):
Firstly, I think your maxRows variable can be simplified to this
<xsl:variable name="maxRows" select="max(//Row/count(key(hostNameByValue, Hostname)))" />
Where I have defined hostNameByValue key like so:
<xsl:key name="hostNameByValue" match="Row" use="Hostname"/>
You can also use distinct-values to get the distinct column names
<xsl:variable name="cols" select="distinct-values(//Row/Hostname)" />
So, assuming $rowNum is the current number (within a <xsl:for-each select="1 to $maxRows"> block, the code to get the current cell value would be this
<xsl:for-each select="$cols">
<th><xsl:value-of select="key('hostNameByValue', ., $doc)[position() = $rowNum]/VLAN"/></th>
</xsl:for-each>
(Where $doc is a reference to the initial XML document, because within the xsl:for-each is now a sequence of atomic values)
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="hostNameByValue" match="Row" use="Hostname"/>
<xsl:variable name="cols" select="distinct-values(//Row/Hostname)" />
<xsl:variable name="maxRows" select="max(//Row/count(key('hostNameByValue', Hostname)))" />
<xsl:variable name="doc" select="/" />
<xsl:template match="DocumentRoot">
<table>
<tr>
<xsl:for-each select="$cols">
<th><xsl:value-of select="."/></th>
</xsl:for-each>
</tr>
<xsl:for-each select="1 to $maxRows">
<xsl:variable name="rowNum" select="position()"/>
<tr>
<xsl:for-each select="$cols">
<th><xsl:value-of select="key('hostNameByValue', ., $doc)[position() = $rowNum]/VLAN"/></th>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
See it in action at http://xsltfiddle.liberty-development.net/6r5Gh3N
I think using a group by might make this more complicated than it needs to be. Basically for each row you need to iterate over all columns and output the cell, if it exists, or an empty cell otherwise. This means you should iterate over an index rather then row elements:
<xsl:for-each select="1 to $numRows">
<xsl:variable name="rowIndex" select="position()" />
<tr>
<xsl:for-each select="$vCols">
<xsl:variable name="cell" select="//Row[string(Hostname) = .][position() = $rowIndex]" />
<xsl:apply-templates select="$cell">
<xsl:if test="not($cell)">
<td></td>
</xsl:if>
</xsl:for-each>
<tr>
</xsl:for.each>
I don't think in XSLT 2 or 3, once you use for-each-group, you need any of the keys, you can just store the grouping result and then process it, for instance to store the grouping result as XML in XSLT 2 or 3 you could use
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes"
version="3.0">
<xsl:output method="html" indent="yes" html-version="5"/>
<xsl:template match="/">
<html>
<head>
<title>.NET XSLT Fiddle Example</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="DocumentRoot">
<table>
<xsl:variable name="cols" as="element(col)*">
<xsl:for-each-group select="Row" group-by="Hostname">
<col name="{current-grouping-key()}">
<xsl:sequence select="current-group()"/>
</col>
</xsl:for-each-group>
</xsl:variable>
<thead>
<tr>
<xsl:for-each select="$cols">
<th>{#name}</th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:variable name="rows" select="max($cols!count(Row))"/>
<xsl:for-each select="1 to $rows">
<xsl:variable name="row" select="."/>
<tr>
<xsl:for-each select="$cols">
<td>{Row[$row]/VLAN}</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/94rmq6Q/3 is XSLT 3 with the ! map operator and the text value templates but https://xsltfiddle.liberty-development.net/94rmq6Q/4 rewrites that as XSLT 2 with value-of instead.
Or in XSLT 3 you could store the grouping result in a sequence of arrays:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
exclude-result-prefixes="#all"
expand-text="yes"
version="3.0">
<xsl:output method="html" indent="yes" html-version="5"/>
<xsl:template match="/">
<html>
<head>
<title>.NET XSLT Fiddle Example</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="DocumentRoot">
<table>
<xsl:variable name="cols" as="array(element(Row))*">
<xsl:for-each-group select="Row" group-by="Hostname">
<xsl:sequence select="array{ current-group() }"/>
</xsl:for-each-group>
</xsl:variable>
<thead>
<tr>
<xsl:for-each select="$cols">
<th>{?1/Hostname}</th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:variable name="rows" select="max($cols!array:size(.))"/>
<xsl:for-each select="1 to $rows">
<xsl:variable name="row" select="."/>
<tr>
<xsl:for-each select="$cols">
<td>{if ($row le array:size(.))
then .($row)/VLAN
else ()}</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/94rmq6Q/2
Maybe you can help me.
I don't know how to sum some table's cell "Year" which are grouped by "Title". I need that sum cell also would be merge as first cell "Title".
I used sum(), but it returs 0.
XMl Code:
<?xml version="1.0" encoding="UTF-8"?>
<LIST>
<Row>
<TITLE>Empire Burlesque</TITLE>
<YEAR>2001</YEAR>
<artist>Bob Dylan</artist>
<artist1>Bob Dylan1</artist1>
</Row>
<Row>
<TITLE>Empire Burlesque</TITLE>
<YEAR>2002</YEAR>
<artist>Bob Dylan</artist>
<artist1>Bob Dylanas</artist1>
</Row>
<Row>
<TITLE>Empire Burlesque</TITLE>
<YEAR>2003</YEAR>
<artist>Bonnie Tyler</artist>
<artist1>Bob Dylan</artist1>
</Row>
<Row>
<TITLE>Empire Burlesque</TITLE>
<YEAR>2004</YEAR>
<artist>Bonnie Tyler</artist>
<artist1>Bob Dylanas</artist1>
</Row>
<Row>
<TITLE>Empire Burlesque1</TITLE>
<YEAR>2005</YEAR>
<artist>Bonnie Tyler</artist>
<artist1>Bob Dylan</artist1>
</Row>
<Row>
<TITLE>Empire Burlesque1</TITLE>
<YEAR>2006</YEAR>
<artist>Bonnie Tyler</artist>
<artist1>Bob Dylanas</artist1>
</Row>
</LIST>
XSLT code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:key name="cds" match="Row" use="TITLE" />
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Year</th>
<th>Artist</th>
<th>Artist1</th>
</tr>
<xsl:for-each select="LIST/Row[generate-id() = generate-id(key('cds', TITLE)[1])]" >
<tr>
<td>
<xsl:if test="key('cds', TITLE)[1]">
<xsl:attribute name="rowspan">
<xsl:value-of select="count(key('cds', TITLE))" />
</xsl:attribute>
</xsl:if>
<xsl:value-of select="TITLE"/>
</td>
<td>
<xsl:value-of select="YEAR"/>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
<td>
<xsl:value-of select="artist1"/>
</td>
</tr>
<xsl:for-each select="key('cds', TITLE)[position() > 1]">
<tr>
<td>
<xsl:value-of select="YEAR"/>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
<td>
<xsl:value-of select="artist1"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Now result is that:
Try sum(key('cds', TITLE)/YEAR) inside of the for-each.
Excuse my ignorance. I am just beginning XSL and XML transformations.
I receive xml data from a vendor.
I only need to include certain "ids" in my transformation.
I also need to add a "display name" based on the ID to the final output.
I would be able to manual add the ID and Display names necessary into the XSL.
XML ex.
<root>
<DATA>
<ID>rd_bl</ID>
<travel>15</travel<
<delay>7</delay>
</DATA>
<DATA>
<ID>yl_gr</ID>
<travel>18</travel<
<delay>9</delay>
</DATA>
<DATA>
<ID>pu_gr</ID>
<travel>17</travel<
<delay>6</delay>
</DATA>
</root>
I would like to write a list of IDs and "display names" in the xsl - only the records with the listed IDs would be included.
ID - Display Name
rd_bl - Red to Blue
pu_gr - Purple to Green
In this example the data from yl_gr would be ignored and not show up in the transformation.
Any help is greatly appreciated.
Thanks!
Here’s a simple stylesheet that checks whether an ID is within an approved list of IDs and uses a “display name” for it in the output.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:variable name="desired-ids">
<id name="rd_bl">Red to Blue</id>
<id name="pu_gr">Purple to Green</id>
</xsl:variable>
<xsl:template match="root">
<root>
<xsl:apply-templates />
</root>
</xsl:template>
<xsl:template match="DATA">
<xsl:variable name="current-id" select="ID/text()" />
<xsl:if test="$desired-ids/id[#name=$current-id]">
<entry>
<name>
<xsl:value-of select="$desired-ids/id[#name=$current-id]" />
</name>
<travel>
<xsl:value-of select="travel" />
</travel>
<delay>
<xsl:value-of select="delay" />
</delay>
</entry>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output using your example XML after correcting the closing tag errors:
<root>
<entry>
<name>Red to Blue</name>
<travel>15</travel>
<delay>7</delay>
</entry>
<entry>
<name>Purple to Green</name>
<travel>17</travel>
<delay>6</delay>
</entry>
</root>
EDIT: in case you’re stuck with XSL 1.0:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="desired-ids">rd_bl="Red to Blue" pu_gr="Purple to Green"</xsl:variable>
<xsl:template match="root">
<root>
<xsl:apply-templates />
</root>
</xsl:template>
<xsl:template match="DATA">
<xsl:variable name="current-id" select="ID/text()" />
<xsl:variable name="id-with-equals" select="concat($current-id, '=')" />
<xsl:if test="contains($desired-ids, $id-with-equals)">
<xsl:variable name="id-with-open-quote" select="concat($id-with-equals, '"')" />
<xsl:variable name="display-name" select="substring-before(substring-after($desired-ids, $id-with-open-quote), '"')" />
<entry>
<name>
<xsl:value-of select="$display-name" />
</name>
<travel>
<xsl:value-of select="travel" />
</travel>
<delay>
<xsl:value-of select="delay" />
</delay>
</entry>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
You can see this is much less elegant, it uses awkward string-matching to check for a valid ID and extract the display name.
Will this stylesheet help?
<?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="/">
<table>
<thead>
<th>ID</th>
<th>Display Name</th>
</thead>
<tbody>
<xsl:apply-templates select="root/DATA"/>
</tbody>
</table>
</xsl:template>
<xsl:template match="DATA">
<xsl:choose>
<xsl:when test="ID='rd_bl'">
<tr>
<td><xsl:value-of select="ID"/></td>
<td>Red to Blue</td>
</tr>
</xsl:when>
<xsl:when test="ID='pu_gr'">
<tr>
<td><xsl:value-of select="ID"/></td>
<td>Purple to Green</td>
</tr>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
I've seen many posts on this, but none of them have helped me figure out my problem.
Test1.xml
<table>
<row>
<col1>A</col1>
</row>
<row>
<col1>B</col1>
</row>
<row>
<col1>C</col1>
</row>
</table>
Test2.xml
<table>
<row>
<col1>A</col1>
<col2>ABC</col2>
</row>
<row>
<col1>B</col1>
<col2>ABC</col2>
</row>
<row>
<col1>A</col1>
<col2>ABC</col2>
</row>
<row>
<col1>C</col1>
<col2>ABC</col2>
</row>
<row>
<col1>A</col1>
<col2>DEF</col2>
</row>
</table>
Test.xsl (XSLT 1.0)
<xsl:variable name="input" select="document('test1.xml')/>
<xsl:template match="/">
<xsl:apply-templates select="$input" mode="special"/>
</xsl:template>
<xsl:template match="node()|#" mode="special">
<xsl:copy>
<xsl:apply-templates select="node()|#" mode="special"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Row" mode="special">
<xsl:variable name="cols" select="document('test2.xml')/Table/Row[current()/Col1 = Col1]/Col2"/>
<xsl:variable name="unique_cols" select="$cols[not(. = preceding-sibling::*)]"/>
<!-- Debug -->
<xsl:for-each select="$unique_cols">
<xsl:copy-of select="."/>
</xsl:for-each>
----
</xsl:template>
Expected Output:
<col2>ABC</col2>
<col2>DEF</col2>
----
<col2>ABC</col2>
----
<col2>ABC</col2>
Current Output:
<col2>ABC</col2>
<col2>ABC</col2>
<col2>DEF</col2>
----
<col2>ABC</col2>
----
<col2>ABC</col2>
The col2 values in $unique_cols should be distinct per col1 values. If unique col2 values could be selected in $cols, even better.
Just replace:
<xsl:variable name="unique_cols" select="$cols[not(. = preceding-sibling::*)]"/>
with:
<xsl:variable name="unique_cols" select=
"$cols[not(../col1 = ../preceding-sibling::*/col1)]"/>
The full transformation (initially corrrected to fix a multitude of lexical errors) now becomes (also using my own file-Uris):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="input" select=
"document('file:///c:/temp/delete/test1.xml')"/>
<xsl:template match="/">
<xsl:apply-templates select="$input" mode="special"/>
</xsl:template>
<xsl:template match="node()|#*" mode="special">
<xsl:copy>
<xsl:apply-templates select="node()|#*" mode="special"/>
</xsl:copy>
</xsl:template>
<xsl:template match="row" mode="special">
<xsl:variable name="cols" select=
"document('file:///c:/temp/delete/test2.xml')
/table
/row[current()/col1 = col1]
/col2"/>
<xsl:variable name="unique_cols" select=
"$cols[not(../col1 = ../preceding-sibling::*/col1)]"/>
<!-- Debug -->
<xsl:for-each select="$unique_cols">
<xsl:copy-of select="."/>
</xsl:for-each>
----
</xsl:template>
</xsl:stylesheet>
and the files are:
c:/temp/delete/test1.xml:
<table>
<row>
<col1>A</col1>
</row>
<row>
<col1>B</col1>
</row>
<row>
<col1>C</col1>
</row>
</table>
and: c:/temp/delete/test2.xml:
<table>
<row>
<col1>A</col1>
<col2>ABC</col2>
</row>
<row>
<col1>B</col1>
<col2>ABC</col2>
</row>
<row>
<col1>A</col1>
<col2>ABC</col2>
</row>
<row>
<col1>C</col1>
<col2>ABC</col2>
</row>
</table>
When the transformation is performed on any XML document (not used), the wanted, correct result is produced:
<table>
<col2>ABC</col2>
----
<col2>ABC</col2>
----
<col2>ABC</col2>
----
</table>
I think I may have figured this out based on an answer provided on another post (https://groups.google.com/forum/?fromgroups#!topic/microsoft.public.xsl/i8FwJUD0r8U).
<xsl:variable name="cols" select=
"document('test2.xml')/table/row[current()/col1 = col1]/col2[not(. = ../preceding-sibling::*/col2[current()/col1 = ../col1])]"/>
This appears to select unique col2 values into $cols eliminating the need for the second variable.
I have a problem. I get the data from xml then transform it with xslt.
Let us say I have a xml file:
<?xml version="1.0"?>
<root>
<row id="1" fname="Dan" lname="Wahlin">
<address type="home">
<street>1234 Anywhere St.</street>
<city>AnyTown</city>
<zip>85789</zip>
</address>
<address type="business">
<street>1234 LottaWork Ave.</street>
<city>AnyTown</city>
<zip>85786</zip>
</address>
</row>
<row id="2" fname="Elaine" lname="Wahlin">
<address type="home">
<street>1234 Anywhere St.</street>
<city>AnyTown</city>
<zip>85789</zip>
</address>
<address type="business">
<street>1233 Books Way</street>
<city>AnyTown</city>
<zip>85784</zip>
</address>
</row>
</root>
And this stylesheet:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="no"/>
<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="row">
<row>
<xsl:attribute name="id">
<xsl:value-of select="id"/>
</xsl:attribute>
<xsl:attribute name="fname">
<xsl:value-of select="name/fname"/>
</xsl:attribute>
<xsl:attribute name="lname">
<xsl:value-of select="name/lname"/>
</xsl:attribute>
<xsl:for-each select="address">
<xsl:copy-of select="."/>
</xsl:for-each> </row>
</xsl:template>
</xsl:stylesheet
How can limit this to 3 records, then after 3 records it create a tr tag?
For example:
<table>
<tr>
<td>Address1</td>
<td>Address2</td>
<td>Address3</td>
</tr>
<tr>
<td>Address4</td>
<td>Address5</td>
<td>Address6</td>
</tr>
</table
try something like
<xsl:for-each select="PATH">
<xsl:variable name="pos" select="position() mod 3" />
</xsl:for-each>
then you can work with
<xsl:if test="$pos = 0">
</xsl:if>
and
<xsl:if test="$pos != 0">
</xsl:if>
if $pos = 0 means that you reached the 3rd row
Here are some good resources to learn more about XSLT and XPath
http://w3schools.com/xsl/default.asp
http://w3schools.com/xpath/default.asp