I'm trying to show the following xml data as a table, each point in a row with some of its values as columns. Each point has many more values than the three in the example, but they all have the same number of values and I want to select some of them. In addition, there are several other elements with names other than 'Point[x]' including an element named 'Count' that indicates the number of points.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="report.xsl" ?>
<root>
<body>
<part>
<values>
<item name="Foo" value="123" unit="" />
<item name="Baa" value="abc" unit="" />
<item name="Count" value="3" unit="" />
<item name="Point[1].Value1" value="640" unit="m³" />
<item name="Point[1].Value2" value="20" unit="°C" />
<item name="Point[1].Value3" value="40" unit="%" />
<item name="Point[1].Value4" value="0" unit="" />
<item name="Point[2].Value1" value="641" unit="m³" />
<item name="Point[2].Value2" value="21" unit="°C" />
<item name="Point[2].Value3" value="41" unit="%" />
<item name="Point[2].Value4" value="0" unit="" />
<item name="Point[3].Value1" value="642" unit="m³" />
<item name="Point[3].Value2" value="22" unit="°C" />
<item name="Point[3].Value3" value="42" unit="%" />
<item name="Point[3].Value4" value="0" unit="" />
</values>
</part>
</body>
</root>
This is what I want to get:
No Value1 [m³] Value2 [°C] Value3 [%] 1 640 20 40 2 641 21 41 3 642 22 42
And this is what I get with the stylesheet below:
No Value1 [m³] Value2 [°C] Value3 [%] Col1 Col2 Col3 1 640 20 40 Point[1].Value1 Point[1].Value2 123 2 641 20 40 Point[2].Value1 Point[2].Value2 123 3 642 20 40 Point[3].Value1 Point[3].Value2 123
I loop over all matches of "].Value1" and try to build the other XPaths with concat() and position(). As you can see in Col2 concat() works as expected. However, Col3 shows that my select
expression isn't working.
So what's the correct expression to select the correct point in each row?
<?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>
<head></head>
<body>
<table>
<tr>
<th>No</th>
<th>Value1 [m³]</th>
<th>Value2 [°C]</th>
<th>Value3 [%]</th>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
</tr>
<xsl:for-each select="//*[contains(@name, '].Value1')]">
<tr>
<td>
<xsl:value-of select="position()"/>
</td>
<td>
<xsl:value-of select="@value"/>
</td>
<td>
<xsl:value-of select="//item[@name='Point[1].Value2']/@value"/>
</td>
<td>
<xsl:value-of select="//item[@name='Point[1].Value3']/@value"/>
</td>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="concat('Point[', position(), '].Value2')"/>
</td>
<td>
<xsl:value-of select="//item[concat(@name='Point[', position(), '].Value2')]/@value"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Edit: described the structure of the data in more detail
item
elements form a row I would use positional grouping, so in XSLT 2for-each-group select="item" group-adjacent="(position() - 1) idiv 3"
andcurrent-group()
to populate the columns, in XSLT 1 probablyfor-each select="item[position() mod 3 = 1]"
, then inside you can access process. | following-sibling::item[position() < 3]
to populate your columns. – Martin Honnen Nov 21 '20 at 23:29