Previous cousin of current element?

Here should go questions about transforming XML with XSLT and FOP.
berkott
Posts: 2
Joined: Thu Apr 22, 2010 4:35 pm

Previous cousin of current element?

Post by berkott »

Where the current element is derived dynamically. The name is not known...

<?xml version="1.0" encoding="UTF-8"?>
<ROWSET xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ROW>
<HIST_CREATE_DATE>2010-04-15</HIST_CREATE_DATE>
<ROWID_OBJECT>66953 </ROWID_OBJECT>
<CREATOR>user1</CREATOR>
<CREATE_DATE>2007-03-03</CREATE_DATE>
<UPDATED_BY>user1</UPDATED_BY>
<LAST_UPDATE_DATE>2010-04-15</LAST_UPDATE_DATE>
<ALTITUDE>1</ALTITUDE>
</ROW>
<ROW>
<HIST_CREATE_DATE>2010-04-16</HIST_CREATE_DATE>
<ROWID_OBJECT>66953 </ROWID_OBJECT>
<CREATOR>user1</CREATOR>
<CREATE_DATE>2007-03-03</CREATE_DATE>
<UPDATED_BY>user2</UPDATED_BY>
<LAST_UPDATE_DATE>2010-04-16</LAST_UPDATE_DATE>
<ALTITUDE>100</ALTITUDE>
</ROW>
<ROW>
<HIST_CREATE_DATE>2010-04-21</HIST_CREATE_DATE>
<ROWID_OBJECT>66953 </ROWID_OBJECT>
<CREATOR>user1</CREATOR>
<CREATE_DATE>2007-03-03</CREATE_DATE>
<UPDATED_BY>user1</UPDATED_BY>
<LAST_UPDATE_DATE>2010-04-21</LAST_UPDATE_DATE>
<ALTITUDE>50</ALTITUDE>
</ROW>
</ROWSET>



<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="cyan">
<xsl:for-each select="/ROWSET/ROW[1]/*">
<th>
<xsl:value-of select="name()"/>
</th>
</xsl:for-each>
</tr>
<xsl:for-each select="/ROWSET/*">
<tr>
<xsl:for-each select="./*">
<td>
<xsl:choose>
<!-- Obviously this below is wrong -->
<xsl:when test="text() != name(../preceding-sibling::*[1]/node())">
<!-- Would like to do something like (or something smarter!!!!)
Get the value of the previous sibling with the same name as the current node
<xsl:when test="text() = EVALUATE(name(../preceding-sibling::*[1]/node()))">
OR
<xsl:when test="text() = EVALUATE(concat('../preceding-sibling::*[1]/' ,name()))">
-->
<strong>
<font color="red"> <xsl:value-of select="."/> </font>
</strong>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="text()"/>
</xsl:otherwise>
</xsl:choose>
</td>
TRY: <xsl:value-of select="../preceding-sibling::*[1]/./text()"/>
<xsl:variable name="xxx"
select="concat('../preceding-sibling::*[1]/' ,name())"/>
Constructed Path to previous cousin of current element: <xsl:value-of select="$xxx"/>

</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Output:
<html>
<body>
<table border="1">
<tr bgcolor="cyan">
<th>HIST_CREATE_DATE</th>
<th>ROWID_OBJECT</th>
<th>CREATOR</th>
<th>CREATE_DATE</th>
<th>UPDATED_BY</th>
<th>LAST_UPDATE_DATE</th>
<th>ALTITUDE</th>
</tr>
<tr>
<td>2010-04-15</td>
TRY:
Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/HIST_CREATE_DATE
<td>66953 </td>
TRY:
Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/ROWID_OBJECT
<td>user1</td>
TRY:
Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/CREATOR
<td>2007-03-03</td>
TRY:
Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/CREATE_DATE
<td>user1</td>
TRY:
Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/UPDATED_BY
<td>2010-04-15</td>
TRY:
Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/LAST_UPDATE_DATE
<td>1</td>
TRY:
Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/ALTITUDE
</tr>
<tr>
<td>2010-04-16</td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/HIST_CREATE_DATE
<td>66953 </td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/ROWID_OBJECT
<td>user1</td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/CREATOR
<td>2007-03-03</td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/CREATE_DATE
<td>user2</td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/UPDATED_BY
<td>2010-04-16</td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/LAST_UPDATE_DATE
<td>100</td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/ALTITUDE
</tr>
<tr>
<td>2010-04-21</td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/HIST_CREATE_DATE
<td>66953 </td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/ROWID_OBJECT
<td>user1</td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/CREATOR
<td>2007-03-03</td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/CREATE_DATE
<td>user1</td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/UPDATED_BY
<td>2010-04-21</td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/LAST_UPDATE_DATE
<td>50</td>
TRY:

Constructed Path to previous cousin of current element: ../preceding-sibling::*[1]/ALTITUDE
</tr>
</table>
</body>
</html>
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Previous cousin of current element?

Post by adrian »

Hi,

Not sure exactly what you want to obtain from that stylesheet.

There's a more than decent XSLT tutorial here that could help you:
http://www.zvon.org/xxl/XSLTutorial/Boo ... tents.html

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
berkott
Posts: 2
Joined: Thu Apr 22, 2010 4:35 pm

Re: Previous cousin of current element?

Post by berkott »

I enlisted the help of an xslt expert. She suggested this and it worked:

Something like:

<xsl:variable name="colName" select="name()" />

and then:

<xsl:when test=". != ../preceding-sibling::ROW[1]/*[name() = $colName]">

which breaks down into:

. this
!= is not equal to
.. my parent (ROW)'s
/preceding-sibling::ROW[1] immediately preceding sibling ROW's
/* child element
[ whose
name() name
= is equal to
$colName $colName (the name of this element)
]

-----

This looks to me like the processor must check each preceding sibling ROW's child elements till it finds the one that matches the name of the current one. Seems like it might be overkill cpu cycles, but it works so I don't care too much. I'd just like to know how it works.
Post Reply