[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Return text contents of most recent non-null sibling.

Subject: [xsl] Return text contents of most recent non-null sibling.
From: "Kerry, Richard" <richard.kerry@xxxxxxxx>
Date: Tue, 14 Aug 2012 16:03:41 +0000

I'm sure this should be straightforward but I can't seem to make it work ...

I have Xml corresponding to part of a spreadsheet, like this :

<sheet name="sheet-1" >
<row row="6">
<column column="1" cell="A6">Reference Present</column>
<column column="2" cell="B6">1,2</column>
<column column="3" cell="C6">1=missing</column>

<row row="7">
<column column="1" cell="A7"/>
<column column="2" cell="B7"/>
<column column="3" cell="C7">2=present</column>

<row row="8">
<column column="1" cell="A8">Reference Standard</column>
<column column="2" cell="B8">0-11</column>
<column column="3" cell="C8">0=missing</column>

<row row="9">
<column column="1" cell="A9"/>
<column column="2" cell="B9"/>
<column column="3" cell="C9">1=525</column>

<row row="10">
<column column="1" cell="A10"/>
<column column="2" cell="B10"/>
<column column="3" cell="C10">2=625</column>

The situation is that there are headings that I want to be repeating in my
result that aren't repeated in this source.  In particular the column 1 text,
which is only present in cells A6 and A8 here.

The result I want is as follows:

<result name="sheet-1" >
<item row="6" name="Reference Present" value="1=missing" />
<item row="7" name="Reference Present" value="2=present" />
<item row="8" name="Reference Standard" value="0=missing" />
<item row="9" name="Reference Standard" value="1=525" />
<item row="10" name="Reference Standard" value="2=625" />

The text from cell=A6 has been copied to the new item corresponding to row 7,
and that from cell=A8 copied to the items corresponding to rows 9 and 10.

At the moment I've got the following template in my stylesheet :

<xsl:template match="row">
<xsl:when test="column[1]/text()" >
<test heading="{column[1]}" type="present" value="{column[3]}" />
<test heading="{preceding-sibling::node()[column[1]][1]/column[1]}"
type="absent" value="{column[3]}" />

For each "row", if there is text in column[1] then it generates output with
If there is no text in column[1] then the text from the nearest non-empty
preceding row/column[1] should be used, and type=absent.

But that gives the following output.

<test heading="Reference Present" type="present" value="1=missing"/>
<test heading="Reference Present" type="absent" value="2=present"/>
<test heading="Reference Standard" type="present" value="0=missing"/>
<test heading="Reference Standard" type="absent" value="1=525"/>
<test heading="" type="absent" value="2=625"/>

So, if the heading was present, it's coming through as required.
If it is absent on the current row, it is only coming through once, and I want
it to come through every time.

My selection is preceding-sibling::node()[column[1]][1]/column[1].

By this I mean "find the nearest preceding sibling with something in column1"
- preceding-sibling::node()[column[1]]
return the first item in this seqnence - [1]
return the whole of column 1 of it - /column[1]

Can someone advise what I'm getting wrong here.  I have tried a selection of
simimlar options, with and without various [1]'s for example, but was rather
finding I was trying things randomly rather than understanding what I was


Richard Kerry
BNCS Engineer
T: +44 (0)20 82259063
M: +44 (0)7812 325518
Room EBX 301, BBC Television Centre, Wood Lane, London, W12 7RJ
uk.atos.net <http://uk.atos.net/en-uk/>

This e-mail and the documents attached are confidential and intended solely
for the addressee; it may also be privileged. If you receive this e-mail in
error, please notify the sender immediately and destroy it. As its integrity
cannot be secured on the Internet, the Atos group liability cannot be
triggered for the message content. Although the sender endeavours to maintain
a computer virus-free network, the sender does not warrant that this
transmission is virus-free and will not be liable for any damages resulting
from any virus transmitted

Current Thread